-
Notifications
You must be signed in to change notification settings - Fork 0
/
DTALG.py
285 lines (233 loc) · 8.2 KB
/
DTALG.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import pandas as pd
import matplotlib as plt
import numpy as np
from sklearn import linear_model
#from sklearn.model_selection cross_validation
from scipy.stats import norm
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from random import seed
from random import randrange
from csv import reader
import csv
import numpy as np
import pandas as pd
from pandas import read_csv
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import r2_score
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer
def process(path):
msev=[]
maev=[]
rsqv=[]
rmsev=[]
acyv=[]
df = pd.read_csv(path,encoding="latin-1")
x1=np.array(df['Lyrics'].values.astype('U'))
y1=np.array(df['MoodValue'])
print(x1)
print(y1)
print(x1)
print(y1)
X_train, X_test, y_train, y_test = train_test_split(x1, y1,test_size=0.20)
count_vectorizer = CountVectorizer(stop_words='english')
count_train = count_vectorizer.fit_transform(X_train) # Learn the vocabulary dictionary and return term-document matrix.
count_test = count_vectorizer.transform(X_test)
tfidf_vectorizer = TfidfVectorizer(stop_words='english', max_df=0.7) # This removes words which appear in more than 70% of the articles
tfidf_train = tfidf_vectorizer.fit_transform(X_train)
tfidf_test = tfidf_vectorizer.transform(X_test)
model2=DecisionTreeClassifier()
model2.fit(count_train, y_train)
y_pred = model2.predict(count_test)
print("predicted")
print(y_pred)
print("test")
print(y_test)
result2=open("results/resultCOUNTDT.csv","w")
result2.write("ID,Predicted Value" + "\n")
for j in range(len(y_pred)):
result2.write(str(j+1) + "," + str(y_pred[j]) + "\n")
result2.close()
mse=mean_squared_error(y_test, y_pred)
mae=mean_absolute_error(y_test, y_pred)
r2=abs(r2_score(y_test, y_pred))
print("---------------------------------------------------------")
print("MSE VALUE FOR DecisionTree COUNT IS %f " % mse)
print("MAE VALUE FOR DecisionTree COUNT IS %f " % mae)
print("R-SQUARED VALUE FOR DecisionTree COUNT IS %f " % r2)
rms = np.sqrt(mean_squared_error(y_test, y_pred))
print("RMSE VALUE FOR DecisionTree COUNT IS %f " % rms)
ac=accuracy_score(y_test,y_pred)
print ("ACCURACY VALUE DecisionTree COUNT IS %f" % ac)
print("---------------------------------------------------------")
msev.append(mse)
maev.append(mae)
rsqv.append(r2)
rmsev.append(rms)
acyv.append(ac*100)
result2=open('results/COUNTDTMetrics.csv', 'w')
result2.write("Parameter,Value" + "\n")
result2.write("MSE" + "," +str(mse) + "\n")
result2.write("MAE" + "," +str(mae) + "\n")
result2.write("R-SQUARED" + "," +str(r2) + "\n")
result2.write("RMSE" + "," +str(rms) + "\n")
result2.write("ACCURACY" + "," +str(ac) + "\n")
result2.close()
df = pd.read_csv('results/COUNTDTMetrics.csv')
acc = df["Value"]
alc = df["Parameter"]
colors = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#8c564b"]
explode = (0.1, 0, 0, 0, 0)
fig = plt.figure()
plt.bar(alc,acc,color=colors)
plt.xlabel('Parameter')
plt.ylabel('Value')
plt.title(' COUNT DecisionTree Metrics Value')
fig.savefig('results/COUNTDTMetricsValue.png')
plt.pause(5)
plt.show(block=False)
plt.close()
model2=DecisionTreeClassifier()
model2.fit(tfidf_train, y_train)
y_pred = model2.predict(tfidf_test)
print("predicted")
print(y_pred)
print("test")
print(y_test)
result2=open("results/resultTFIDFDT.csv","w")
result2.write("ID,Predicted Value" + "\n")
for j in range(len(y_pred)):
result2.write(str(j+1) + "," + str(y_pred[j]) + "\n")
result2.close()
mse=mean_squared_error(y_test, y_pred)
mae=mean_absolute_error(y_test, y_pred)
r2=abs(r2_score(y_test, y_pred))
print("---------------------------------------------------------")
print("MSE VALUE FOR DecisionTree TFIDF IS %f " % mse)
print("MAE VALUE FOR DecisionTree TFIDF IS %f " % mae)
print("R-SQUARED VALUE FOR DecisionTree TFIDF IS %f " % r2)
rms = np.sqrt(mean_squared_error(y_test, y_pred))
print("RMSE VALUE FOR DecisionTree TFIDF IS %f " % rms)
ac=accuracy_score(y_test,y_pred)
print ("ACCURACY VALUE DecisionTree TFIDF IS %f" % ac)
print("---------------------------------------------------------")
msev.append(mse)
maev.append(mae)
rsqv.append(r2)
rmsev.append(rms)
acyv.append(ac*100)
result2=open('results/TFIDFDTMetrics.csv', 'w')
result2.write("Parameter,Value" + "\n")
result2.write("MSE" + "," +str(mse) + "\n")
result2.write("MAE" + "," +str(mae) + "\n")
result2.write("R-SQUARED" + "," +str(r2) + "\n")
result2.write("RMSE" + "," +str(rms) + "\n")
result2.write("ACCURACY" + "," +str(ac) + "\n")
result2.close()
df = pd.read_csv('results/TFIDFDTMetrics.csv')
acc = df["Value"]
alc = df["Parameter"]
colors = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#8c564b"]
explode = (0.1, 0, 0, 0, 0)
fig = plt.figure()
plt.bar(alc,acc,color=colors)
plt.xlabel('Parameter')
plt.ylabel('Value')
plt.title(' TFIDF DecisionTree Metrics Value')
fig.savefig('results/TFIDFCOUNTDTMetricsValue.png')
plt.pause(5)
plt.show(block=False)
plt.close()
al = ['COUNT','TFIDF']
result2=open('results/DTMSE.csv', 'w')
result2.write("Vectorization,MSE" + "\n")
for i in range(0,len(msev)):
result2.write(al[i] + "," +str(msev[i]) + "\n")
result2.close()
colors = ["#1f77b4", "#ff7f0e", "#2ca02c"]
explode = (0.1, 0, 0, 0, 0)
#Barplot for the dependent variable
fig = plt.figure(0)
df = pd.read_csv('results/DTMSE.csv')
acc = df["MSE"]
alc = df["Vectorization"]
plt.bar(alc,acc,color=colors)
plt.xlabel('Vectorization')
plt.ylabel('MSE')
plt.title("DecisionTree MSE Value");
fig.savefig('results/DTMSE.png')
plt.pause(5)
plt.show(block=False)
plt.close()
result2=open('results/DTMAE.csv', 'w')
result2.write("Vectorization,MAE" + "\n")
for i in range(0,len(maev)):
result2.write(al[i] + "," +str(maev[i]) + "\n")
result2.close()
fig = plt.figure(0)
df = pd.read_csv('results/DTMAE.csv')
acc = df["MAE"]
alc = df["Vectorization"]
plt.bar(alc,acc,color=colors)
plt.xlabel('Vectorization')
plt.ylabel('MAE')
plt.title('DecisionTree MAE Value')
fig.savefig('results/DTMAE.png')
plt.pause(5)
plt.show(block=False)
plt.close()
result2=open('results/DTR-SQUARED.csv', 'w')
result2.write("Vectorization,R-SQUARED" + "\n")
for i in range(0,len(rsqv)):
result2.write(al[i] + "," +str(rsqv[i]) + "\n")
result2.close()
fig = plt.figure(0)
df = pd.read_csv('results/DTR-SQUARED.csv')
acc = df["R-SQUARED"]
alc = df["Vectorization"]
plt.bar(alc,acc,color=colors)
plt.xlabel('Vectorization')
plt.ylabel('R-SQUARED')
plt.title('DecisionTree R-SQUARED Value')
fig.savefig('results/DTR-SQUARED.png')
plt.pause(5)
plt.show(block=False)
plt.close()
result2=open('results/DTRMSE.csv', 'w')
result2.write("Vectorization,RMSE" + "\n")
for i in range(0,len(rmsev)):
result2.write(al[i] + "," +str(rmsev[i]) + "\n")
result2.close()
fig = plt.figure(0)
df = pd.read_csv('results/DTRMSE.csv')
acc = df["RMSE"]
alc = df["Vectorization"]
plt.bar(alc,acc,color=colors)
plt.xlabel('Vectorization')
plt.ylabel('RMSE')
plt.title('DecisionTree RMSE Value')
fig.savefig('results/DTRMSE.png')
plt.pause(5)
plt.show(block=False)
plt.close()
result2=open('results/DTAccuracy.csv', 'w')
result2.write("Vectorization,Accuracy" + "\n")
for i in range(0,len(acyv)):
result2.write(al[i] + "," +str(acyv[i]) + "\n")
result2.close()
fig = plt.figure(0)
df = pd.read_csv('results/DTAccuracy.csv')
acc = df["Accuracy"]
alc = df["Vectorization"]
plt.bar(alc,acc,color=colors)
plt.xlabel('Vectorization')
plt.ylabel('Accuracy')
plt.title('DecisionTree Accuracy Value')
fig.savefig('results/DTAccuracy.png')
plt.pause(5)
plt.show(block=False)
plt.close()