NEURAL NETWORK

import pandas as pd
df = pd.read_csv(“movies.csv”)
df.drop(labels = [“Unnamed: 2”, “Unnamed: 3”, “Unnamed: 4”, “Unnamed: 5”, “Unnamed: 8”, “runtime”], axis =1 ,inplace =True)
df.dropna(inplace = True)

Let us binary classification

Movies less than a score of 6.5 are given a label of zero and movies greater than 6.5 are given a score of 1

label = []
for i in df[‘score’]:
if i < 6.5:
i = 0
label.append(i)
else:
i = 1
label.append(i)

df[‘label’] = label
df[‘label’].value_counts()

df.drop(labels = [‘score’] , axis =1 , inplace =True)
Y= df.loc[:,[‘label’]]
X = df.iloc[:,0:3]

from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
X_scaled = scaler.fit_transform(X)
X_scaled = pd.DataFrame(X_scaled)

from tensorflow import keras
from keras.layers import Dense
from keras.models import Sequential

model = Sequential()
model.add(Dense(1, input_dim =3, activation =”sigmoid”))
model.summary()

model.compile(loss =”binary_crossentropy” , optimizer =’rmsprop’, metrics = [‘accuracy’])

history = model.fit(X_scaled,Y, epochs = 500 , verbose =1)

model1 = Sequential()
model1.add(Dense(6, input_dim=3, activation=’relu’))
model1.add(Dense(6, activation=’relu’))
model1.add(Dense(1, activation=’sigmoid’))
model1.summary()

model1.compile(loss =”binary_crossentropy” , optimizer =’rmsprop’, metrics = [‘accuracy’])

history1 = model1.fit(X_scaled, Y, epochs=500, batch_size=30, verbose=1)

loss1, acc1 = model1.evaluate(X_test, y_test)
print(f”Loss is {loss1},\nAccuracy is {acc1*100}”)

import matplotlib.pyplot as plt
plt.title(‘model loss’)
plt.ylabel(‘loss’)
plt.xlabel(‘epochs’)
plt.plot(history1.history[‘loss’])

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X_scaled, Y , test_size =0.25) #splitting dataset into 2 halves

model2 = Sequential()
model2.add(Dense(6, input_dim=3, activation=’relu’))
model2.add(Dense(6, activation=’relu’))
model2.add(Dense(1, activation=’sigmoid’))
model2.summary()

model2.compile(optimizer=’rmsprop’,loss =”binary_crossentropy”, metrics=[‘acc’, ‘mse’])
hist = model2.fit(X_train, y_train, epochs=500, batch_size=50, validation_data=(X_test,y_test))

plt.plot(hist.history[‘loss’], label = ‘loss’)
plt.plot(hist.history[‘val_loss’], label=’val loss’)
plt.title(“Loss vs Val_Loss”)
plt.xlabel(“Epochs”)
plt.ylabel(“Loss”)
plt.legend()
plt.show()

plt.plot(hist.history[‘acc’], label = ‘acc’)
plt.plot(hist.history[‘val_acc’], label=’val acc’)
plt.title(“acc vs Val_acc”)
plt.xlabel(“Epochs”)
plt.ylabel(“acc”)
plt.legend()
plt.show()

model2_predictions = model2.predict(X_test)
y_pred = model2_predictions.round()

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

import seaborn as sns
sns.heatmap(confusion_matrix, annot =True,fmt=’g’)

model3 = Sequential()
model3.add(Dense(8, input_dim=3, activation=’relu’))
model3.add(Dense(4, activation=’relu’))
model3.add(Dense(1, activation=’sigmoid’))
model3.summary()

model3.compile(loss =”binary_crossentropy” , optimizer =’rmsprop’, metrics = [‘accuracy’])
history3 = model3.fit(X_scaled, Y, epochs=200, batch_size=30, verbose=1)

import matplotlib.pyplot as plt
plt.title(‘model loss’)
plt.ylabel(‘loss’)
plt.xlabel(‘epochs’)
plt.plot(history3.history[‘loss’])

OUTPUT: LINK