<jupyter_start><jupyter_text>K Means Clustering Step 1 - Define ProblemFor this project we will attempt to use KMeans Clustering to cluster Universities into to two groups, Private and Public. 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 Step 2 - Collect & Prepare data  Import Libraries<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
df = pd.read_csv('College_Data.csv',index_col=0)
y = pd.DataFrame(df.Private)
df.head()
df.describe()<jupyter_output><empty_output><jupyter_text>Step 3 - Exploratory Data Analysis ** Create a boxplot of student-faculty ratio based on college type**<jupyter_code>sns.boxplot(x='Private',y='S.F.Ratio',data=df)<jupyter_output><empty_output><jupyter_text>** Create a boxplot of percent of alumni who donate based on college type**<jupyter_code>sns.boxplot(x='Private',y='perc.alumni',data=df)<jupyter_output><empty_output><jupyter_text>** Create a stacked histogram showing Out of State Tuition based on the Private column.**<jupyter_code>sns.violinplot(x='Apps',y='perc.alumni',data=df)
sns.barplot(x='Outstate',y='perc.alumni',data=df)<jupyter_output><empty_output><jupyter_text>Cleaning data **There seems to be a private school with a graduation rate of higher than 100%**<jupyter_code>df[df['Grad.Rate'] > 100]<jupyter_output><empty_output><jupyter_text>** 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.**<jupyter_code>df['Grad.Rate']['Cazenovia College'] = 100
df[df['Grad.Rate'] > 100]<jupyter_output><empty_output><jupyter_text>Step 4 - Modeling** Import KMeans from SciKit Learn.**<jupyter_code>from sklearn.cluster import KMeans<jupyter_output><empty_output><jupyter_text>** Create an instance of a K Means model with 3 clusters.**<jupyter_code># K Means Cluster
model = KMeans(n_clusters=3)<jupyter_output><empty_output><jupyter_text>**Fit the model to all the data except for the Private label.**<jupyter_code>model.fit(df.drop('Private',axis=1))<jupyter_output><empty_output><jupyter_text>** What are the cluster center vectors?**<jupyter_code>clus_cent=model.cluster_centers_
clus_cent<jupyter_output><empty_output><jupyter_text>**Create a data frame with cluster centers and with column names borrowed from the original data frame****Is it clear from this data frame which label corresponds to private college (0 or 1)?**<jupyter_code>df_desc=pd.DataFrame(df.describe())
feat = list(df_desc.columns)
kmclus = pd.DataFrame(clus_cent,columns=feat)
kmclus<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 converter(cluster):
    if cluster=='Yes':
        return 1
    else:
        return 0
df1=df # Create a copy of data frame so that original data frame does not get 'corrupted' with the cluster index
df1['Cluster'] = df['Private'].apply(converter)
df1.head()
predY = np.choose(model.labels_, [1, 0, 2]).astype(np.int64)
print (model.labels_)
print (predY)
from sklearn.metrics import accuracy_score
result = accuracy_score(df1['Cluster'], predY)*100
result<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0953032375740994, 'fraction_numerical': 0.012311901504787962, 'mean_word_length': 6.423011844331641, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '460389', 'license': 'MIT', 'n_tokens_mistral': 1420, 'n_tokens_neox': 1324, 'n_words': 476, 'path': 'Unsupervised K-Means Clustering/.ipynb_checkpoints/Unsupervised Clustering K-Means -checkpoint.ipynb', 'repo_name': 'warmachine0609/Data-Science-Projects', 'size': '4386'}