-
Notifications
You must be signed in to change notification settings - Fork 0
/
PersonalityPredictionModel.py
271 lines (204 loc) · 8.55 KB
/
PersonalityPredictionModel.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
# -*- coding: utf-8 -*-
"""ML miniproject.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1v_hKXjINJ6A-eFTfxsLE45wZFzOvMeWH
**Personality Prediction Model using ML**
**Group members:**
1404 - UCE2021404 Bhairavi Bhuyar
1419 - UCE2021419 Bhakti Girase
Importing Libraries
"""
import numpy as np
import pandas as pd
"""Loading the Data Set"""
data_raw=pd.read_csv("/content/data-final.csv",sep='\t')
data_raw.head()
data = data_raw.copy()
pd.options.display.max_columns = 150
data.drop(data.columns[50:107], axis=1, inplace=True)
data.drop(data.columns[51:], axis=1, inplace=True)
data.drop(data.index[701:],axis=0,inplace=True)
print('Number of participants: ', len(data))
data.head()
"""Cleaning the Data Set"""
print('Any missing value? \n ', data.isnull().values.any())
print('No. of missing values? \n ', data.isnull().values.sum())
data.dropna(inplace=True)
print('Number of participants after eliminating missing values? \n ', len(data))
"""Groups and Questions
---
EXT - Questions to measure Extroversion.
EST - Questions to measure Neuroticism
AGR - Questions to measure Agreeableness
CSN - Questions to measure Conscientiousness
OPN - Questions to measure Openness to Experience
"""
ext_questions = {'EXT1' : 'I am the life of the party',
'EXT2' : 'I dont talk a lot',
'EXT3' : 'I feel comfortable around people',
'EXT4' : 'I keep in the background',
'EXT5' : 'I start conversations',
'EXT6' : 'I have little to say',
'EXT7' : 'I talk to a lot of different people at parties',
'EXT8' : 'I dont like to draw attention to myself',
'EXT9' : 'I dont mind being the center of attention',
'EXT10' : 'I am quiet around strangers'}
est_questions = {'EST1' : 'I get stressed out easily',
'EST2' : 'I am relaxed most of the time',
'EST3' : 'I worry about things',
'EST4' : 'I seldom feel blue',
'EST5' : 'I am easily disturbed',
'EST6' : 'I get upset easily',
'EST7' : 'I change my mood a lot',
'EST8' : 'I have frequent mood swings',
'EST9' : 'I get irritated easily',
'EST10': 'I often feel blue'}
agr_questions = {'AGR1' : 'I feel little concern for others',
'AGR2' : 'I am interested in people',
'AGR3' : 'I insult people',
'AGR4' : 'I sympathize with others feelings',
'AGR5' : 'I am not interested in other peoples problems',
'AGR6' : 'I have a soft heart',
'AGR7' : 'I am not really interested in others',
'AGR8' : 'I take time out for others',
'AGR9' : 'I feel others emotions',
'AGR10': 'I make people feel at ease'}
csn_questions = {'CSN1' : 'I am always prepared',
'CSN2' : 'I leave my belongings around',
'CSN3' : 'I pay attention to details',
'CSN4' : 'I make a mess of things',
'CSN5' : 'I get chores done right away',
'CSN6' : 'I often forget to put things back in their proper place',
'CSN7' : 'I like order',
'CSN8' : 'I shirk my duties',
'CSN9' : 'I follow a schedule',
'CSN10' : 'I am exacting in my work'}
opn_questions = {'OPN1' : 'I have a rich vocabulary',
'OPN2' : 'I have difficulty understanding abstract ideas',
'OPN3' : 'I have a vivid imagination',
'OPN4' : 'I am not interested in abstract ideas',
'OPN5' : 'I have excellent ideas',
'OPN6' : 'I do not have a good imagination',
'OPN7' : 'I am quick to understand things',
'OPN8' : 'I use difficult words',
'OPN9' : 'I spend time reflecting on things',
'OPN10': 'I am full of ideas'}
# Group Names and Columns
EXT = [column for column in data if column.startswith('EXT')]
EST = [column for column in data if column.startswith('EST')]
AGR = [column for column in data if column.startswith('AGR')]
CSN = [column for column in data if column.startswith('CSN')]
OPN = [column for column in data if column.startswith('OPN')]
"""To visualize the questions and answers"""
from matplotlib import pyplot as plt
def vis_questions(groupname, questions, color):
plt.figure(figsize=(60,60))
for i in range(0,9):
plt.subplot(10,3,i+1)
plt.hist(data[groupname[i]], bins=20, color= color, alpha=.5)
plt.title(questions[groupname[i]], fontsize=38)
print('Q&As Related to Extroversion Personality \n')
vis_questions(EXT, ext_questions, '#00447C')
print('Q&As Related to Neuroticism Personality \n')
vis_questions(EST, est_questions, 'blue')
print('Q&As Related to Agreeableness Personality \n')
vis_questions(AGR, agr_questions, 'red')
print('Q&As Related to Conscientious Personality \n')
vis_questions(CSN, csn_questions, '#7C2D00')
print('Q&As Related to Openness to Experience Personality \n')
vis_questions(OPN, opn_questions, 'purple')
"""Scaling data using MinMax Scaler"""
# For ease of calculation lets scale all the values between 0-1
from sklearn.preprocessing import MinMaxScaler
df = data.drop('country', axis=1)
columns = list(df.columns)
scaler = MinMaxScaler(feature_range=(0,1))
df = scaler.fit_transform(df)
df = pd.DataFrame(df, columns=columns)
df_sample = df[:5000]
"""Finding the value of K using elbow method"""
# Visualize the elbow
from sklearn.cluster import KMeans
from yellowbrick.cluster import KElbowVisualizer
kmeans = KMeans()
visualizer = KElbowVisualizer(kmeans, k=(2,15))
visualizer.fit(df_sample)
visualizer.poof()
"""Training the Model using KMeans Clustering"""
# Creating K-means Cluster Model
from sklearn.cluster import KMeans
df_model = data.drop('country', axis=1)
kmeans = KMeans(n_clusters=5)
k_fit = kmeans.fit(df_model)
# Predicting the Clusters
pd.options.display.max_columns = 10
#labels_ is used to identify Labels of each point
predictions = k_fit.labels_
df_model['Clusters'] = predictions
df_model.head(10)
df_model.Clusters.value_counts()
"""Grouping results into clusters"""
pd.options.display.max_columns = 150
df_model.groupby('Clusters').mean()
col_list = list(df_model)
ext = col_list[0:10]
est = col_list[10:20]
agr = col_list[20:30]
csn = col_list[30:40]
opn = col_list[40:50]
data_sums = pd.DataFrame()
data_sums['extroversion'] = df_model[ext].sum(axis=1)/10
data_sums['neurotic'] = df_model[est].sum(axis=1)/10
data_sums['agreeable'] = df_model[agr].sum(axis=1)/10
data_sums['conscientious'] = df_model[csn].sum(axis=1)/10
data_sums['openness'] = df_model[opn].sum(axis=1)/10
data_sums['clusters'] = predictions
data_sums.groupby('clusters').mean()
dataclusters = data_sums.groupby('clusters').mean()
plt.figure(figsize=(22,3))
for i in range(0, 5):
plt.subplot(1,5,i+1)
plt.bar(dataclusters.columns, dataclusters.iloc[:, i], color='green', alpha=0.2)
plt.plot(dataclusters.columns, dataclusters.iloc[:, i], color='red')
plt.title('Cluster ' + str(i))
plt.xticks(rotation=45)
plt.ylim(0,4);
# In order to visualize in 2D graph I will use PCA
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
pca_fit = pca.fit_transform(df_model)
df_pca = pd.DataFrame(data=pca_fit, columns=['PCA1', 'PCA2'])
df_pca['Clusters'] = predictions
df_pca.head()
import seaborn as sns
plt.figure(figsize=(10,10))
sns.scatterplot(data=df_pca, x='PCA1', y='PCA2', hue='Clusters', palette='Set2', alpha=0.9)
plt.title('Personality Clusters after PCA')
"""Testing data"""
my_data = pd.read_excel('/content/test_data.xlsx')
my_data
my_personality = k_fit.predict(my_data)
print('My Personality Cluster: ', my_personality)
# Summing up the my question groups
col_list = list(my_data)
ext = col_list[0:10]
est = col_list[10:20]
agr = col_list[20:30]
csn = col_list[30:40]
opn = col_list[40:50]
my_sums = pd.DataFrame()
my_sums['extroversion'] = my_data[ext].sum(axis=1)/10
my_sums['neurotic'] = my_data[est].sum(axis=1)/10
my_sums['agreeable'] = my_data[agr].sum(axis=1)/10
my_sums['conscientious'] = my_data[csn].sum(axis=1)/10
my_sums['open'] = my_data[opn].sum(axis=1)/10
my_sums['cluster'] = my_personality
my_sums
"""Visualizing the Prediction"""
my_sum = my_sums.drop('cluster', axis=1)
plt.bar(my_sum.columns, my_sum.iloc[0,:], color='green', alpha=0.2)
plt.plot(my_sum.columns, my_sum.iloc[0,:], color='red')
plt.title('Cluster %d'%(my_personality))
plt.xticks(rotation=45)
plt.ylim(0,4);