forked from sameshl/billboard-predictor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ann.py
127 lines (86 loc) · 3.46 KB
/
ann.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# Artificial Neural Network
# Installing Theano
# pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git
# Installing Tensorflow
# Install Tensorflow from the website: https://www.tensorflow.org/versions/r0.12/get_started/os_setup.html
# Installing Keras
# pip install --upgrade keras
# Part 1 - Data Preprocessing
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the dataset
dataset = pd.read_csv('ready_for_model.csv',sep='\t')
dataset.drop_duplicates(inplace = True)
'''
X=dataset.drop(dataset.columns[0:5],axis=1)
X.drop('BillboardHit',axis=1,inplace=True)
X.drop('artist_score',axis=1,inplace=True)
y = dataset['BillboardHit']
#X.drop('BillboardHit',axis=1,inplace=True)
dataset.drop(dataset.columns[0], axis=1, inplace=True)
# Encoding categorical data
mode=pd.get_dummies(X['Mode'],drop_first=True)
key=pd.get_dummies(X['Key'],drop_first=True)
X.drop(['Mode','Key'],inplace=True,axis=1)
X=pd.concat([X,mode,key],axis=1)
'''
bill_1=dataset[dataset['BillboardHit']==1]
bill_0=dataset[dataset['BillboardHit']==0]
print((len(bill_1)/(len(bill_1)+len(bill_0)))*100,"% of values are 1")
subset=len(bill_1)//2
bill_0=bill_0.loc[:subset]
print("chat 0 : ", len(bill_0))
print("chat 1 : ", len(bill_1))
'''
bill_0=bill_0.drop(bill_0.columns[0:5],axis=1)
bill_0.drop('BillboardHit',axis=1,inplace=True)
X.drop('artist_score',axis=1,inplace=True)
y = dataset['BillboardHit']
'''
# Encoding categorical data
new_data = pd.concat([bill_0, bill_1])
X = new_data.drop('BillboardHit', axis=1)
y = new_data['BillboardHit']
X.drop(X.columns[:5],axis =1, inplace =True)
mode=pd.get_dummies(X['Mode'],drop_first=True)
key=pd.get_dummies(X['Key'],drop_first=True)
X.drop(['Mode','Key'],inplace=True,axis=1)
X=pd.concat([X,mode,key],axis=1)
#X.drop('artist_score', axis = 1, inplace = True)
# Splitting the dataset into the Training set and Test set
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, random_state = 0)
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
# Part 2 - Now let's make the ANN!
# Importing the Keras libraries and packages
from keras.models import Sequential
from keras.layers import Dense
from keras import regularizers
# Initialising the ANN
classifier = Sequential()
# Adding the input layer and the first hidden layer
classifier.add(Dense(output_dim = 6, init = 'uniform', activation = 'relu',kernel_regularizer=regularizers.l2(0.01), input_dim = 22))
# Adding the second hidden layer
classifier.add(Dense(output_dim = 6, init = 'uniform', activation = 'relu'))
# Adding the output layer
classifier.add(Dense(output_dim = 1, init = 'uniform', activation = 'sigmoid'))
# Compiling the ANN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
# Fitting the ANN to the Training set
classifier.fit(X_train, y_train, batch_size = 100, nb_epoch = 1000)
# Part 3 - Making the predictions and evaluating the model
# Predicting the Test set results
y_pred = classifier.predict(X_test)
y_pred = (y_pred > 0.5)
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix, classification_report
cm = confusion_matrix(y_test, y_pred)
print(classification_report(y_test, y_pred))
acc = (cm[0][0] + cm[1][1])/(cm[0][0] + cm[0][1] + cm[1][0] + cm[1][1])*100
print("Accuracy is ", acc)