-
Notifications
You must be signed in to change notification settings - Fork 0
/
Best_cluster_number.py
132 lines (107 loc) · 4.76 KB
/
Best_cluster_number.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
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 21 21:09:54 2021
@author: godwi
"""
import pandas as pd
import numpy as np
import json
from pandas.io.json import json_normalize
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_samples, silhouette_score
import matplotlib.pyplot as plt
import numpy as np
import pickle
import re
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.decomposition import PCA
def Mentor_training(Mentor_json,file_location=''):
def data_preprocessing(data):
data=data.drop(labels=['branch','id','name','rollNo','peerID','post','hosteller'],axis=1)
def cleaning_language_data(values):
v=str(values)
v=v.strip()
s=re.sub(pattern="[^\w\s\,\/\+]",repl="",string=v)
s=s.lower()
res = re.split(', |/', s)
count=0
# for i in res:
# res[count]=i.replace('c++','chigh')
# res[count]=i.replace('c','clow')
# res[count]=i.replace('c#','cmedium')
# count+=1
x=' '.join(res)
x=x.replace('c++','chigh')
x=x.replace('c','clow')
x=x.replace('c#','cmedium')
x=x.replace('clowhigh','chigh')
x=x.replace('clow#','cmedium')
x=x.replace('no preferenclowe','no preference')
return x
def cleaning_domain_data(values):
v=str(values)
v=v.strip()
s=re.sub(pattern="[^\w\s\,\/\+]",repl="",string=v)
s=s.lower()
res = re.split(', |/', s)
count=0
# for i in res:
# res[count]=i.replace('c++','chigh')
# res[count]=i.replace('c','clow')
# res[count]=i.replace('c#','cmedium')
# count+=1
x=' '.join(res)
x=x.replace('no prefence','no preference')
return x
for name,values in data.items():
if name=='domains':
if pd.api.types.is_string_dtype(values):
count=0
for i in values:
data.iloc[count,0]=cleaning_domain_data(i)
count+=1
elif name=='languages':
if pd.api.types.is_string_dtype(values):
count=0
for i in values:
data.iloc[count,1]=cleaning_language_data(i)
count+=1
return data
def find_best_cluster(X):
sil_avg=[]
best_cluster=[]
range_clusters=[2,3,4,5,6,7,8,9]
for n_clusters in range_clusters:
clusterer = KMeans(n_clusters=n_clusters, random_state=10)
cluster_labels = clusterer.fit_predict(X)
silhouette_avg = silhouette_score(X, cluster_labels)
sample_silhouette_values = silhouette_samples(X, cluster_labels)
sample_silhouette_values.sort()
if sample_silhouette_values[0]>0:
sil_avg.append(silhouette_avg)
best_cluster.append(n_clusters)
max_score=np.argmax(sil_avg)
return best_cluster[max_score]
def cluster_model(Mentor_json):
Mentor_df_json=pd.read_json(Mentor_json)
Mentor_df=pd.DataFrame(Mentor_df_json.users.values.tolist())
data=Mentor_df.copy()
df=pd.DataFrame({'domains':['[Web Development, App Development, Machine Learning, IOT, BlockChain, AR/VR, Game Development, Cloud Engineering, Competitive Programming, Cyber Security, Open Source]'],'languages':['[Java, Python, C/C++, No Preference]']})
data=data.append(df,ignore_index=True)
mentor_df_pre=data_preprocessing(data)
np.random.seed(10)
domains_vector=CountVectorizer()
domains_vec_val=domains_vector.fit_transform(mentor_df_pre['domains'])
df_domain_wrds=pd.DataFrame(domains_vec_val.toarray(),columns=domains_vector.get_feature_names())
np.random.seed(11)
languages_vector=CountVectorizer()
languages_vec_val=languages_vector.fit_transform(mentor_df_pre['languages'])
df_lang_wrds=pd.DataFrame(languages_vec_val.toarray(),columns=languages_vector.get_feature_names())
final_df=pd.concat([df_domain_wrds,df_lang_wrds],axis=1)
final_df=final_df.drop(index=final_df.shape[0]-1,axis=0)
pca=PCA(n_components=2,random_state=30)
X=pca.fit_transform(final_df)
cluster_num=find_best_cluster(X)
model=KMeans(n_clusters=cluster_num, random_state=25)
cluster_labels = model.fit_predict(X)
return pickle.dump(model,open(file_location+'cluster_on_trained_mentor.pkl','wb'))