# -*- coding: utf-8 -*-
"""Movielens-100k-predictive-analysis.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1iZizcby5PQ8fBWDV3PL9cNxUaQbHeMQ6
"""

import numpy as np
import pandas as pd
from pandas import Series, DataFrame
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
import matplotlib.pyplot as plt
from matplotlib import style
import seaborn as sns
import csv

df_user = pd.read_csv("u.user", engine='python', sep='|', names=["userID", "Age", "Gender", "occupation", "Zip-Code"])

df_user.head()

df_movie = pd.read_csv("u.item", engine='python', sep='|', names=["movieID", "Movie title", "release date", "video release date",
              "IMDb URL", "unknown", "Action", "Adventure", "Animation",
              "Children's", "Comedy", "Crime", "Documentary", "Drama", "Fantasy",
              "Film-Noir", "Horror", "Musical", "Mystery", "Romance", "Sci-Fi",
              "Thriller", "War", "Western"])

df_movie.head()

df_ratings = pd.read_csv("u.data", engine='python', sep='\t', names=["userID", "movieID", "Rating", "Timestamp"])

df_ratings.head()

df_merged1 = df_movie.merge(df_ratings, how='outer')

df_merged1.head()

#Merging Users and Ratings
df_merged2 = df_user.merge(df_ratings, how='inner')

df_merged2.head()

#Merging Users/Ratings/Movies 
df_merged3 = df_merged1.merge(df_merged2, how='inner')

df_merged3.head()

df_merged3 = df_merged3.fillna(0) 
df_merged3.UserID = df_merged3.userID.astype(int)
df_merged3.Rating = df_merged3.Rating.astype(int)

df_merged3.shape

df_merged3.sort_values(by=['userID'], ascending=True)

#Rearranging merged3 columns into suitable format
master_data = df_merged3[['userID', 'movieID', 'Movie title', 'Rating', 'unknown', 'Action', 'Adventure', 'Animation', "Children's", 'Comedy', 'Crime', 'Documentary', 'Drama', 'Fantasy', 'Film-Noir', 'Horror', 'Musical', 'Mystery', 'Romance', 'Sci-Fi', 'Thriller', 'War', 'Western', 'Zip-Code', 'Gender', 'Age', 'occupation', 'Timestamp']]

master_data.head()

master_data.sort_values(by=['userID'], ascending=True)

#PERFORMING EDAs
bins_list = [1, 18, 25, 35, 45, 50, 56]
master_data.hist(column='Age', bins = bins_list)

#Checking ratings on Jurassic Park
master_data[master_data['movieID'] == 82].hist(column='Rating')

#Avg rating of Jurassic park
master_data[master_data['movieID'] == 82].Rating.mean()

#Predictive analysis
#Analysis factors affecting movie rating
master_data.head()
master_data['Gender'].replace(['F','M'],[0,1],inplace=True)
md_small = master_data.iloc[:, [1, 2, 3, 23, 24, 25, 26]]

md_small.head()

#Convert as many dtypes into int to get better coef insights
md_small.dtypes

#Finding coorelation coef
md_small[md_small.columns[1:]].corr()['Rating'][:]

temp_genre = master_data.iloc[:, [4, 5, 6, 7, 8, 9 , 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]]

temp_genre.head()

master_features = pd.merge(md_small, temp_genre, left_index=True, right_index=True)

master_features.head()

master_features.dtypes

#Preparing data for linear regression
#Drop all obj dtype
X_feature = md_small.drop(['Zip-Code', 'Movie title', 'occupation'], axis=1)

X_feature.head()

#Preparing to train on first 40 movies
X_feature_small = X_feature[X_feature['movieID'] < 40]

X_feature_small_trimmed = X_feature_small.drop(['movieID','Rating'], axis=1)
X_feature_small_trimmed.head()

Y_target = master_features['Rating'][master_features['movieID']< 40]

x_train, x_test, y_train, y_test = train_test_split(X_feature_small_trimmed,Y_target,random_state=1)

logreg = LogisticRegression(max_iter=100000)

logreg.fit(x_train,y_train)

y_pred = logreg.predict(x_test)

metrics.accuracy_score(y_test,y_pred)

print ('actual:    ', y_test.values[0:30])
print ('predicted: ', y_pred[0:30])
# -*- coding: utf-8 -*-
"""Movielens-100k-predictive-analysis.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1iZizcby5PQ8fBWDV3PL9cNxUaQbHeMQ6
"""

import numpy as np
import pandas as pd
from pandas import Series, DataFrame
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
import matplotlib.pyplot as plt
from matplotlib import style
import seaborn as sns
import csv

df_user = pd.read_csv("u.user", engine='python', sep='|', names=["userID", "Age", "Gender", "occupation", "Zip-Code"])

df_user.head()

df_movie = pd.read_csv("u.item", engine='python', sep='|', names=["movieID", "Movie title", "release date", "video release date",
              "IMDb URL", "unknown", "Action", "Adventure", "Animation",
              "Children's", "Comedy", "Crime", "Documentary", "Drama", "Fantasy",
              "Film-Noir", "Horror", "Musical", "Mystery", "Romance", "Sci-Fi",
              "Thriller", "War", "Western"])

df_movie.head()

df_ratings = pd.read_csv("u.data", engine='python', sep='\t', names=["userID", "movieID", "Rating", "Timestamp"])

df_ratings.head()

df_merged1 = df_movie.merge(df_ratings, how='outer')

df_merged1.head()

#Merging Users and Ratings
df_merged2 = df_user.merge(df_ratings, how='inner')

df_merged2.head()

#Merging Users/Ratings/Movies 
df_merged3 = df_merged1.merge(df_merged2, how='inner')

df_merged3.head()

df_merged3 = df_merged3.fillna(0) 
df_merged3.UserID = df_merged3.userID.astype(int)
df_merged3.Rating = df_merged3.Rating.astype(int)

df_merged3.shape

df_merged3.sort_values(by=['userID'], ascending=True)

#Rearranging merged3 columns into suitable format
master_data = df_merged3[['userID', 'movieID', 'Movie title', 'Rating', 'unknown', 'Action', 'Adventure', 'Animation', "Children's", 'Comedy', 'Crime', 'Documentary', 'Drama', 'Fantasy', 'Film-Noir', 'Horror', 'Musical', 'Mystery', 'Romance', 'Sci-Fi', 'Thriller', 'War', 'Western', 'Zip-Code', 'Gender', 'Age', 'occupation', 'Timestamp']]

master_data.head()

master_data.sort_values(by=['userID'], ascending=True)

#PERFORMING EDAs
bins_list = [1, 18, 25, 35, 45, 50, 56]
master_data.hist(column='Age', bins = bins_list)

#Checking ratings on Jurassic Park
master_data[master_data['movieID'] == 82].hist(column='Rating')

#Avg rating of Jurassic park
master_data[master_data['movieID'] == 82].Rating.mean()

#Predictive analysis
#Analysis factors affecting movie rating
master_data.head()
master_data['Gender'].replace(['F','M'],[0,1],inplace=True)
md_small = master_data.iloc[:, [1, 2, 3, 23, 24, 25, 26]]

md_small.head()

#Convert as many dtypes into int to get better coef insights
md_small.dtypes

#Finding coorelation coef
md_small[md_small.columns[1:]].corr()['Rating'][:]

temp_genre = master_data.iloc[:, [4, 5, 6, 7, 8, 9 , 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]]

temp_genre.head()

master_features = pd.merge(md_small, temp_genre, left_index=True, right_index=True)

master_features.head()

master_features.dtypes

#Preparing data for linear regression
#Drop all obj dtype
X_feature = md_small.drop(['Zip-Code', 'Movie title', 'occupation'], axis=1)

X_feature.head()

#Preparing to train on first 40 movies
X_feature_small = X_feature[X_feature['movieID'] < 40]

X_feature_small_trimmed = X_feature_small.drop(['movieID','Rating'], axis=1)
X_feature_small_trimmed.head()

Y_target = master_features['Rating'][master_features['movieID']< 40]

x_train, x_test, y_train, y_test = train_test_split(X_feature_small_trimmed,Y_target,random_state=1)

logreg = LogisticRegression(max_iter=100000)

logreg.fit(x_train,y_train)

y_pred = logreg.predict(x_test)

metrics.accuracy_score(y_test,y_pred)

print ('actual:    ', y_test.values[0:30])
print ('predicted: ', y_pred[0:30])
# -*- coding: utf-8 -*- Movielens-100k-predictive-analysis.ipynb  Automatically generated by Colaboratory.  Original file is located at     https://colab.research.google.com/drive/1iZizcby5PQ8fBWDV3PL9cNxUaQbHeMQ6 #Merging Users and Ratings #Merging Users/Ratings/Movies #Rearranging merged3 columns into suitable format #PERFORMING EDAs #Checking ratings on Jurassic Park #Avg rating of Jurassic park #Predictive analysis #Analysis factors affecting movie rating #Convert as many dtypes into int to get better coef insights #Finding coorelation coef #Preparing data for linear regression #Drop all obj dtype #Preparing to train on first 40 movies