-
Notifications
You must be signed in to change notification settings - Fork 1
/
kmedoid.py
180 lines (142 loc) · 5.51 KB
/
kmedoid.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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeClassifier, export_graphviz
from sklearn.model_selection import StratifiedShuffleSplit
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.ensemble import RandomForestClassifier
from sklearn.decomposition import PCA
from sklearn.preprocessing import scale
from sklearn import tree
from collections import defaultdict
import random
import pydot
from io import StringIO
import pydotplus
from multiprocessing import Process
import tkinter as tk
from tkinter import filedialog
from MovieApp import MovieApp
def kMedoids(data, k, prev_cost, count, clusters=None, medoids=None):
cluster_sum = 0
random.seed(0)
while True:
if medoids is None or not medoids:
medoids = random.sample(data, k)
else:
random.shuffle(medoids)
for _ in range(0, int(k/2)):
medoids.pop()
medoids += random.sample(data, int(k/2))
clusters = defaultdict(list)
for item in data:
temp = []
for i in range(0, len(medoids)):
med = medoids[i]
if med is None or not med:
break
else:
temp.append(np.linalg.norm(
med[0]-item[0])+np.linalg.norm(med[1]-item[1]))
min_index = np.argmin(temp)
clusters[min_index].append(item)
for i in range(0, len(medoids)):
inter_cluster = clusters[i]
for j in range(0, len(inter_cluster)):
item_cluster = inter_cluster[j]
medoid = medoids[i]
cluster_sum += (np.linalg.norm(medoid[0]-item_cluster[0]) +
np.linalg.norm(medoid[1]-item_cluster[1]))
if cluster_sum < prev_cost:
prev_cost = cluster_sum
else:
break
count += 1
return clusters
def plot_graph(clusters):
markers = ['bo', 'go', 'ro', 'c+', 'm+', 'y+']
for i in range(0, len(clusters.keys())):
data = clusters.get(i)
for j in range(0, len(data)):
df = data[j]
plt.plot(df[0], df[1], markers[i])
plt.xlabel('IMDb Scores')
plt.ylabel('Gross')
plt.title('K-medoid clusters')
plt.legend()
plt.show()
def assign_target(row, clusters):
x = row['movie_title']
for i in range(0, len(clusters.keys())):
data = clusters.get(i)
for j in range(0, len(data)):
df = data[j]
if df[2] == x:
row['cluster'] = 'cluster'+str(i)
return row
def open_csv(root):
root.filename = filedialog.askopenfilename(parent=root,filetypes=[("All files","*.csv")])
print(root.filename)
def gui(df):
# start
root = tk.Tk()
editor = EditorApp(root, df)
root.mainloop() # until closes window
def init_app():
columns = ['movie_title','num_user_for_reviews', 'budget', 'num_critic_for_reviews','movie_facebook_likes',
'num_voted_users','duration','gross', 'imdb_score']
#loading dataset
df = pd.read_csv('movie_metadata.csv').dropna(axis=0).reset_index(drop=True)
p = Process(target=gui, args=(df[columns],))
p.start()
#choosing features and running kmediods
dataset = df[['gross', 'imdb_score', 'movie_title']]
dataset = dataset.values.tolist()
clusters = kMedoids(dataset, 5, np.inf, 0)
#Plot Cluster graph
p = Process(target=plot_graph, args=(clusters,))
p.start()
#choosing features for decision tree
columns = ['num_user_for_reviews', 'budget'
, 'num_critic_for_reviews', 'movie_title','movie_facebook_likes','num_voted_users','duration']
df = df[columns]
df = df.reset_index()
df = df.apply(assign_target, args=(clusters,), axis=1)
df.drop(labels = ['movie_title'], axis = 1, inplace = True)
df_before_split = df.copy()
#creating training and test sets
splitSet = StratifiedShuffleSplit(
n_splits=1, test_size=0.2, random_state=0)
for train_index, test_index in splitSet.split(df, df['cluster']):
train_set = df.loc[train_index]
test_set = df.loc[test_index]
Y_train = train_set.cluster
X_train = train_set[train_set.columns.drop('cluster').drop('index')]
Y_test = test_set.cluster
X_test = test_set[test_set.columns.drop('cluster').drop('index')]
#Creating decision tree
decision_tree = DecisionTreeClassifier()
decision_tree.fit(X_train, Y_train)
predictions = decision_tree.predict(X_test)
print('Accuracy of the decision tree=', decision_tree.score(X_test, Y_test))
print(confusion_matrix(Y_test,predictions))
print('\n')
print(classification_report(Y_test,predictions))
print('\n')
#Applying random forest classifier
rfc = RandomForestClassifier(n_estimators=2000)
rfc.fit(X_train, Y_train)
print('Random Forest Statistics\n')
rfc_pred = rfc.predict(X_test)
print(confusion_matrix(Y_test,rfc_pred))
print('\n')
print(classification_report(Y_test,rfc_pred))
#Visualising the decision tree (runs only in Jupyter notebook)
'''dot_data = StringIO()
export_graphviz(decision_tree, out_file=dot_data,
filled=True, rounded=True,
special_characters=True, impurity=False, feature_names=train_set.columns.drop('cluster').drop('index'))
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_png("dtree.png")'''
if __name__=='__main__':
init_app()