<jupyter_start><jupyter_text>College Application Data Analysis using K Means ClusteringFor this project we will attempt to use KMeans Clustering to cluster Universities into to two groups, Private and Public.___It is **very important to note, we actually have the labels for this data set, but we will NOT use them for the KMeans clustering algorithm, since that is an unsupervised learning algorithm.** When using the Kmeans algorithm under normal circumstances, it is because you don't have labels. In this case we will use the labels to try to get an idea of how well the algorithm performed, but you won't usually do this for Kmeans, so the classification report and confusion matrix at the end of this project, don't truly make sense in a real world setting!.___ The DataWe 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<jupyter_code># Science imports
import pandas as pd
import numpy as np

# Viz imports
import matplotlib.pyplot as plt
import seaborn as sns

# Config matplotlib
%matplotlib inline
plt.rcParams["patch.force_edgecolor"] = True # in matplotlib, edge borders are turned off by default. 
sns.set_style("darkgrid") # set a grey grid as a background

# ML imports
import sklearn
from sklearn import metrics
# Linear Regression 
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
# Logistic Regression
#
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
# KMeans clustering
from sklearn.cluster import KMeans

# turn off warnings
import warnings
warnings.filterwarnings('ignore')<jupyter_output><empty_output><jupyter_text>Get the DataRead the data and use the first column as the index<jupyter_code>data = pd.read_csv('College_Data', index_col=0)<jupyter_output><empty_output><jupyter_text>**Check the head of the data**<jupyter_code>data.head()
data.info()
data.describe()<jupyter_output><empty_output><jupyter_text>Exploratory Data Analysis**Creating a scatterplot of Grad.Rate versus Room.Board where the points are colored by the Private column.**<jupyter_code># Using lmplot to scatter, fit_reg removes the fit lines
sns.lmplot(x='Room.Board',y='Grad.Rate',data=data,hue='Private',
           palette='coolwarm',size=6, fit_reg=False,aspect=1)<jupyter_output><empty_output><jupyter_text>**Creating a scatterplot of F.Undergrad versus Outstate where the points are colored by the Private column.**<jupyter_code>sns.lmplot(y='F.Undergrad',x='Outstate',data=data,hue='Private',
           palette='coolwarm',size=6, fit_reg=False,aspect=1)<jupyter_output><empty_output><jupyter_text>Creating a stacked histogram showing Out of State Tuition based on the Private column.<jupyter_code># Notes - Larger aspect creates a wider scale,  Size is size of image. Easier that matplotlib to extend
# Also note legend_out and add_legend(). the .add_legend() is attached to the g.map() function
g = sns.FacetGrid(df,hue="Private",palette='coolwarm',size=6,aspect=2, legend_out=True)
g = g.map(plt.hist,'Outstate',bins=30,alpha=0.5).add_legend()<jupyter_output><empty_output><jupyter_text>**Create a similar histogram for the Grad.Rate column.**<jupyter_code># Notice the grad rate for a school is somehow over 100% (x-axis)
g = sns.FacetGrid(data,hue="Private",palette='coolwarm',size=6,aspect=2, legend_out=True)
g = g.map(plt.hist,'Grad.Rate',bins=30,alpha=0.5).add_legend()<jupyter_output><empty_output><jupyter_text>**Notice how there seems to be a private school with a graduation rate of higher than 100%.**The name of the school is Cazenovia College<jupyter_code>data[data['Grad.Rate'] > 100]<jupyter_output><empty_output><jupyter_text>**Setting Cazenovia College's graduation rate to 100 so it makes sense.**<jupyter_code># Return to actual grad rate
data[data['Grad.Rate'] > 100]['Grad.Rate']
# set Grad.Rate to 0
data['Grad.Rate']['Cazenovia College'] = 100
# Validate the change
data['Grad.Rate']['Cazenovia College']
# Validate the change
# This should be empty
data[data['Grad.Rate'] > 100]
g = sns.FacetGrid(data,hue="Private",palette='coolwarm',size=6,aspect=2, legend_out=True)
g = g.map(plt.hist,'Grad.Rate',bins=20,alpha=0.5).add_legend()<jupyter_output><empty_output><jupyter_text>K Means Cluster CreationCreating the Cluster labels<jupyter_code>kmeans = KMeans(n_clusters=2)<jupyter_output><empty_output><jupyter_text>**Fitting the model to all the data except for the Private label.**<jupyter_code>kmeans.fit(data.drop('Private',axis=1))<jupyter_output><empty_output><jupyter_text>**Showing the cluster center vectors**<jupyter_code>kmeans.cluster_centers_<jupyter_output><empty_output><jupyter_text>EvaluationThere 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.**<jupyter_code>def iscluster(c):
    if c == 'Yes':
        return 1
    else:
        return 0
    
data['Cluster'] = data['Private'].apply(iscluster)
data.head()<jupyter_output><empty_output><jupyter_text>**Adding the confusion matrix and classification report to see how well the Kmeans clustering worked without being given any labels.**<jupyter_code>print(confusion_matrix(data['Cluster'],kmeans.labels_))
print(classification_report(data['Cluster'],kmeans.labels_))<jupyter_output>[[ 74 138]
 [ 34 531]]
              precision    recall  f1-score   support

           0       0.69      0.35      0.46       212
           1       0.79      0.94      0.86       565

   micro avg       0.78      0.78      0.78       777
   macro avg       0.74      0.64      0.66       777
weighted avg       0.76      0.78      0.75       777
{'fraction_non_alphanumeric': 0.09215686274509804, 'fraction_numerical': 0.02066365007541478, 'mean_word_length': 5.273415326395459, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '538949', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 2152, 'n_tokens_neox': 1985, 'n_words': 693, 'path': 'College Application Data Analysis using K Means Clustering.ipynb', 'repo_name': 'jsutch/K_Means_Clustering', 'size': '6630'}