# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import os
os.chdir(r'D:\D\Edureka\Edureka - 24 June - Python\Class 17')

import pandas as pd
import numpy as np

dataset = pd.read_csv('Churn_Modelling.csv')

x=dataset.iloc[:,3:13].values

y = dataset.iloc[:,13].values

y

x

from sklearn.preprocessing import LabelEncoder, OneHotEncoder

labelencoder_x = LabelEncoder()
x[:,1] = labelencoder_x.fit_transform(x[:,1])

labelencoder_x = LabelEncoder()
x[:,2] = labelencoder_x.fit_transform(x[:,2])

x

onehotencoder = OneHotEncoder(categorical_features=[1])
x = onehotencoder.fit_transform(x).toarray()

pd.DataFrame(x)

from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=0.2)

from sklearn.neighbors import KNeighborsClassifier
classifier_knn = KNeighborsClassifier(n_neighbors=801, p=2)

classifier_knn.fit(x_train, y_train)

y_pred = classifier_knn.predict(x_test)

from sklearn.metrics import confusion_matrix
confusion_matrix(y_test, y_pred)

from sklearn.svm import SVC
classifier_svm = SVC(kernel='rbf')
classifier_svm.fit(x_train, y_train)

y_pred=classifier_svm.predict(x_test)

confusion_matrix(y_test, y_pred)

# !pip install xgboost

from xgboost import XGBClassifier
classifier = XGBClassifier()
classifier.fit(x_train, y_train)

y_pred = classifier.predict(x_test)

confusion_matrix(y_test, y_pred)

from sklearn.metrics import accuracy_score
accuracy_score(y_test, y_pred)

from sklearn.model_selection import cross_val_score
accuracies = cross_val_score(estimator=classifier, X = x_train, y=y_train, cv=10)
accuracies.mean()

accuracies.std()

accuracies

os.chdir(r'C:\Users\Admin\Downloads')

dataset = pd.read_csv('Recommend.csv', names = ['user_id','movie_id','rating','timestamp'])

dataset

n_users = dataset.user_id.unique().shape[0]

n_movies=dataset.movie_id.unique().shape[0]

from sklearn.model_selection import train_test_split
train_data, test_data = train_test_split(dataset, test_size=0.25)

train_data_matrix = np.zeros((n_users, n_movies))

pd.DataFrame(train_data_matrix)

for line in train_data.itertuples():
    train_data_matrix[line[1]-1, line[2]-1] = line[3]

train_data_matrix[line[1]-1,line[2]-1]

train_data_matrix[243,707]

pd.DataFrame(train_data_matrix)

list(train_data.itertuples())

for line in test_data.itertuples():
    tes[line[1]-1, line[2]-1] = line[3]

test_data_matrix = np.zeros((n_users, n_movies))
for line in test_data.itertuples():
    test_data_matrix[line[1]-1, line[2]-1] = line[3]

pd.DataFrame(test_data_matrix)

from sklearn.metrics import pairwise_distances

user_similarity = pairwise_distances(train_data_matrix, metric='cosine')
mean_user_rating = train_data_matrix.mean(axis=1)[:,np.newaxis]

rating_diff = (train_data_matrix-mean_user_rating)
user_pred = mean_user_rating + user_similarity.dot(rating_diff)/np.array([np.abs(user_similarity).sum(axis=1)]).T

df_out = pd.DataFrame(user_pred)

df_out.to_csv('output.csv')

StarcoderdataJupyterScriptsDedupFiltered