-
Notifications
You must be signed in to change notification settings - Fork 8
/
clustering.py
85 lines (69 loc) · 1.9 KB
/
clustering.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
import pandas as pd
import numpy as np
import json
import matplotlib
import matplotlib.pyplot as plt
from matplotlib import cm
import os
import plotly.offline as py
import plotly.graph_objs as go
from plotly.graph_objs import *
import seaborn as sns
df = pd.read_csv('INvideos.csv')
df.head()
df.dtypes
cluster = df[['likes','dislikes', 'views', 'comment_count']]
from sklearn.cluster import KMeans
wcss = []
for i in range(1,11):
kmeans = KMeans(n_clusters=i,init='k-means++',max_iter=300,n_init=10,random_state=0)
kmeans.fit(cluster)
wcss.append(kmeans.inertia_)
plt.plot(range(1,11),wcss)
plt.title('The Elbow Method')
plt.xlabel('Number of clusters')
plt.ylabel('WCSS')
plt.show()
df2 = cluster.apply(lambda x:(x.astype(float) - min(x))/(max(x)-min(x)), axis = 0)
from sklearn.cluster import KMeans
wcss = []
for i in range(1,11):
kmeans = KMeans(n_clusters=i,init='k-means++',max_iter=300,n_init=10,random_state=0)
kmeans.fit(df2)
wcss.append(kmeans.inertia_)
plt.plot(range(1,11),wcss)
plt.title('The Elbow Method')
plt.xlabel('Number of clusters')
plt.ylabel('WCSS')
plt.show()
kmeans = KMeans(n_clusters=5,init='k-means++',max_iter=300,n_init=10,random_state=0)
y_kmeans = kmeans.fit_predict(df2)
df2['cluster']=y_kmeans
trace1 = go.Scatter3d(
x = df2['likes'].values,
y = df2['comment_count'].values,
z = df2['views'].values,
mode='markers',
marker=dict(
size=12,
color=df2['cluster'].values,# set color to an array/list of desired values
colorscale='Viridis', # choose a colorscale
opacity=0.8
)
)
data = [trace1]
layout = go.Layout(
scene=Scene(
xaxis=XAxis(title='Likes'),
yaxis=YAxis(title='Comment'),
zaxis=ZAxis(title='Views')
),
margin=dict(
l=0,
r=0,
b=0,
t=0
),
)
fig = go.Figure(data=data, layout=layout)
py.plot(fig, filename='3d-scatter-colorscale')