<jupyter_start><jupyter_text>Spotify Hit Predictor Model for 60's Dataset  Importing Libraries<jupyter_code>import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import tensorflow as tf
from tensorflow import keras

%matplotlib inline
import matplotlib as mpl #add'l plotting functionality<jupyter_output><empty_output><jupyter_text>Loading Dataset<jupyter_code>df=pd.read_csv("dataset-of-60s.csv")
df.head()
df.info()
df.shape<jupyter_output><empty_output><jupyter_text>Shuffling of Data<jupyter_code>df=df.sample(frac=1)
df.head()<jupyter_output><empty_output><jupyter_text>Balancing of Data<jupyter_code>#Dropping Categorical features
X=df.drop(['target',"track","artist","uri"],axis=1)
X.shape    #modified dataframe for X
Y=df["target"]
hit_flop_count=Y.value_counts()
hit_flop_count
Y=Y.values<jupyter_output><empty_output><jupyter_text>Standardizing the Inputs<jupyter_code>scaler=StandardScaler()
scaled_X=scaler.fit_transform(X)<jupyter_output><empty_output><jupyter_text>Split the dataset into Train, Test, Validation set<jupyter_code>#train-test split in 90%-10%
x_train, x_test, y_train, y_test = train_test_split(scaled_X, Y, 
                                test_size=0.1, random_state=1)
#Train-Validation split 90%-10%
x_train, x_val, y_train, y_val = train_test_split(x_train,y_train, 
                                test_size=(1/9), random_state=1)
#1/9 of 90% is 10% as we are using train if we use Scaled X then we use test size as 0.1
x_train.shape,x_test.shape,x_val.shape<jupyter_output><empty_output><jupyter_text>Creating Deep Learning Algorithm<jupyter_code>nn = 200 #number of neurons in hidden layers
target_count = 2              #ouput i.e. 1 and 0
model_60 = keras.Sequential([

keras.layers.Flatten(), #to flaten our data for better results 
keras.layers.Dense(nn, activation=tf.nn.relu),# first hidden  layer
keras.layers.Dense(nn, activation=tf.nn.relu),# second hidden layer
keras.layers.Dense(nn, activation=tf.nn.relu),# third hidden layer

keras.layers.Dense(target_count, activation=tf.nn.softmax)# output layer
    
    ])
model_60.compile(optimizer='adam', loss='sparse_categorical_crossentropy', 
                 metrics=['accuracy'])
#fitting the model 
model_fit = model_60.fit(x_train, y_train, epochs=20,
                         validation_data=(x_val, y_val), batch_size=100)
#Storing the predictions
predictions = model_60.predict(x_test)<jupyter_output><empty_output><jupyter_text>Visualize Neural Network Loss History  Loss Variation Plot<jupyter_code>training_loss = model_fit.history['loss']
validation_loss = model_fit.history['val_loss']
epoch_count1 = range(1, len(training_loss) + 1)

#-------------plotting--------------------------

mpl.rcParams['figure.dpi'] = 400 #high res figures
plt.subplot(2,1,2)
plt.title('Loss Variation Plot')
plt.plot(epoch_count1, training_loss, color='violet', label='Training Loss')
plt.plot(epoch_count1, validation_loss, color='indigo', label='Validation Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()<jupyter_output><empty_output><jupyter_text>Accuracy Variation Plot<jupyter_code>training_acc = model_fit.history['accuracy']
validation_acc = model_fit.history['val_accuracy']
epoch_count2 = range(1, len(training_acc) + 1)

#plotting 
plt.subplot(2,1,2)
plt.title('Accuracy Variation Plot')
plt.plot(epoch_count2, training_acc, color='red', label='Training Accuracy')
plt.plot(epoch_count2, validation_acc, color='green', label='Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()<jupyter_output><empty_output><jupyter_text>Testing Model<jupyter_code>print(predictions[215])
print('Predicted:', np.argmax(predictions[215]))
print('Original:', y_test[215])<jupyter_output>[0.03258432 0.96741563]
Predicted: 1
Original: 1<jupyter_text>Saving model and Verifying<jupyter_code>model_60.save('Trained_model_60')    #saved as protobuf (.pb)
model_60.summary()
model = tf.keras.models.load_model('Trained_model_60')
print(x_test[1])
print(x_test[1].reshape( 1,-1))
v = model.predict(x_test[1].reshape( 1,-1))
print(v)<jupyter_output>[[0.9435685  0.05643148]]
{'fraction_non_alphanumeric': 0.13307984790874525, 'fraction_numerical': 0.030180608365019013, 'mean_word_length': 6.32, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '27072', 'license': 'MIT', 'n_tokens_mistral': 1620, 'n_tokens_neox': 1508, 'n_words': 276, 'path': "trained_model_60's .ipynb", 'repo_name': 'Rahulraj31/Spotify-Hit-Flop-Predictor-1960-2019', 'size': '4208'}