-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathK Means Clustering Project.py
More file actions
226 lines (122 loc) · 5.09 KB
/
K Means Clustering Project.py
File metadata and controls
226 lines (122 loc) · 5.09 KB
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
#!/usr/bin/env python
# coding: utf-8
# # K Means Clustering Project -
#
#
# ## The Data
#
# We will use a data frame with 777 observations on the following 18 variables.
# * Private A factor with levels No and Yes indicating private or public university
# * Apps Number of applications received
# * Accept Number of applications accepted
# * Enroll Number of new students enrolled
# * Top10perc Pct. new students from top 10% of H.S. class
# * Top25perc Pct. new students from top 25% of H.S. class
# * F.Undergrad Number of fulltime undergraduates
# * P.Undergrad Number of parttime undergraduates
# * Outstate Out-of-state tuition
# * Room.Board Room and board costs
# * Books Estimated book costs
# * Personal Estimated personal spending
# * PhD Pct. of faculty with Ph.D.’s
# * Terminal Pct. of faculty with terminal degree
# * S.F.Ratio Student/faculty ratio
# * perc.alumni Pct. alumni who donate
# * Expend Instructional expenditure per student
# * Grad.Rate Graduation rate
# ## Import Libraries
#
# ** Import the libraries you usually use for data analysis.**
# In[7]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
get_ipython().run_line_magic('matplotlib', 'inline')
# ## Get the Data
# ** Read in the College_Data file using read_csv. Figure out how to set the first column as the index.**
# In[8]:
df = pd.read_csv('College_Data',index_col=0)
# **Check the head of the data**
# In[105]:
df.head()
# ** Check the info() and describe() methods on the data.**
# In[106]:
df.info()
# In[107]:
df.describe()
# ## EDA
#
# It's time to create some data visualizations!
#
# ** Create a scatterplot of Grad.Rate versus Room.Board where the points are colored by the Private column. **
# In[111]:
sns.set_style('whitegrid')
sns.lmplot('Room.Board','Grad.Rate',data=df, hue='Private',
palette='coolwarm',size=6,aspect=1,fit_reg=False)
# **Create a scatterplot of F.Undergrad versus Outstate where the points are colored by the Private column.**
# In[112]:
sns.set_style('whitegrid')
sns.lmplot('Outstate','F.Undergrad',data=df, hue='Private',
palette='coolwarm',size=6,aspect=1,fit_reg=False)
# ** Create a stacked histogram showing Out of State Tuition based on the Private column. Try doing this using [sns.FacetGrid](https://stanford.edu/~mwaskom/software/seaborn/generated/seaborn.FacetGrid.html). If that is too tricky, see if you can do it just by using two instances of pandas.plot(kind='hist'). **
# In[109]:
sns.set_style('darkgrid')
g = sns.FacetGrid(df,hue="Private",palette='coolwarm',size=6,aspect=2)
g = g.map(plt.hist,'Outstate',bins=20,alpha=0.7)
# **Create a similar histogram for the Grad.Rate column.**
# In[110]:
sns.set_style('darkgrid')
g = sns.FacetGrid(df,hue="Private",palette='coolwarm',size=6,aspect=2)
g = g.map(plt.hist,'Grad.Rate',bins=20,alpha=0.7)
# ** Notice how there seems to be a private school with a graduation rate of higher than 100%.What is the name of that school?**
# In[113]:
df[df['Grad.Rate'] > 100]
# ** Set that school's graduation rate to 100 so it makes sense. You may get a warning not an error) when doing this operation, so use dataframe operations or just re-do the histogram visualization to make sure it actually went through.**
# In[93]:
df['Grad.Rate']['Cazenovia College'] = 100
# In[94]:
df[df['Grad.Rate'] > 100]
# In[95]:
sns.set_style('darkgrid')
g = sns.FacetGrid(df,hue="Private",palette='coolwarm',size=6,aspect=2)
g = g.map(plt.hist,'Grad.Rate',bins=20,alpha=0.7)
# ## K Means Cluster Creation
#
# Now it is time to create the Cluster labels!
#
# ** Import KMeans from SciKit Learn.**
# In[3]:
from sklearn.cluster import KMeans
# ** Create an instance of a K Means model with 2 clusters.**
# In[4]:
kmeans = KMeans(n_clusters=2)
# **Fit the model to all the data except for the Private label.**
# In[9]:
kmeans.fit(df.drop('Private',axis=1))
# ** What are the cluster center vectors?**
# In[117]:
kmeans.cluster_centers_
# ## Evaluation
#
# There is no perfect way to evaluate clustering if you don't have the labels, however since this is just an exercise, we do have the labels, so we take advantage of this to evaluate our clusters, keep in mind, you usually won't have this luxury in the real world.
#
# ** Create a new column for df called 'Cluster', which is a 1 for a Private school, and a 0 for a public school.**
# In[118]:
def converter(cluster):
if cluster=='Yes':
return 1
else:
return 0
# In[119]:
df['Cluster'] = df['Private'].apply(converter)
# In[122]:
df.head()
# ** Create a confusion matrix and classification report to see how well the Kmeans clustering worked without being given any labels.**
# In[123]:
from sklearn.metrics import confusion_matrix,classification_report
print(confusion_matrix(df['Cluster'],kmeans.labels_))
print(classification_report(df['Cluster'],kmeans.labels_))
# Not so bad considering the algorithm is purely using the features to cluster the universities into 2 distinct groups! Hopefully you can begin to see how K Means is useful for clustering un-labeled data!
#
# ## Great Job!