import pandas
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split

games = pandas.read_csv("games.csv")
print(games.columns)
print(games.shape)

# Many games have a rating of zero because it was not enterred properly in the CSV
# The .iloc[...] takes the array generated by accessing all games with a rating of zero and returns the value at its associated index of the new array
print(games[games["average_rating"] == 0].iloc[0])
print(games[games["average_rating"] >= 0].iloc[0])

# Drop any data with 0 user ratings, common sense
games = games[games["users_rated"] > 0]
games = games.dropna(axis=0)

# Seaborn is good at finding correlations
corrmat = games.corr()
fig = plt.figure(figsize = (10, 10))
sns.heatmap(corrmat, vmax = .8, square="true")
# plt.show()

# Drop a few variables since they can affect machine learing with how much they correlate
columns = games.columns.tolist()
columns.remove("bayes_average_rating")
columns.remove("average_rating")
columns.remove("type")
columns.remove("name")
columns.remove("id")

# Machine Learning we do is going to be compared with the "target"
target = "average_rating"

train = games.sample(frac=.8, random_state = 1)
# test is finding the .2 not selected for training
test = games.loc[~games.index.isin(train.index)]
print(train.shape)
print(test.shape)

# Linear Regression
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

LR = LinearRegression()
LR.fit(train[columns], train[target])
prediction = LR.predict(test[columns])
print(mean_squared_error(prediction, test[target]))

'''
# Random Forest
from sklearn.ensemble import RandomForestRegressor

RFR = RandomForestRegressor(n_estimators=100, min_samples_leaf=10, random_state = 1)
RFR.fit(train[columns], train[target])
prediction = RFR.predict(test[columns])
print(mean_squared_error(prediction, test[target]))
'''

# Now test your model on individual examples
print(test[columns].iloc[0])
rating = LR.predict(test[columns].iloc[0].values.reshape(1, -1))
print(rating)
print(test[target].iloc[0])
