<jupyter_start><jupyter_text>Project -  K Means Clustering...!<jupyter_code>import numpy as np
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
df= pd.read_csv('Sample',index_col='Unnamed: 0')
df.head()
df.columns
df.info()
df.describe()
df.columns
#sns.jointplot(x=df['Grad.Rate'],y=df['Room.Board'],data=df,color=df['Private'])
df.head(1)
#plt.scatter(x=df['Grad.Rate'],y=df['Room.Board'],c=df1)
#df1=pd.get_dummies(df['Private'])
sns.set_style('whitegrid')
sns.lmplot(x='Grad.Rate',y='Room.Board',data=df,hue='Private',fit_reg=False)
sns.lmplot(y='P.Undergrad',x='Outstate',data=df,hue='Private',fit_reg=False)
df.head(1)
df.columns
#pd.crosstab(df['Private'],df['Outstate']).plot(kind='hist')
#pd.crosstab(df['Outstate'],df['Private']).plot(kind='bar',stacked='True')
plt.figure(figsize=(12,7))
sns.set_style('darkgrid')
plt.xlabel('Outstate')
plt.title('Out of State Tution based on Private data')
plt.legend(labels=df['Private'])
df.groupby('Private')['Outstate'].hist(stacked=True,alpha=0.6,bins=20)<jupyter_output><empty_output><jupyter_text>In order to increase the size of the FacetGrid. size & aspect params are used.<jupyter_code># To combine two plots. FacetGrid is a good technique.

g = sns.FacetGrid(df, hue='Private',size=6,aspect=1.6)
g.map(plt.hist,'Outstate',alpha=.7,bins=20)
plt.figure(figsize=(12,7))
df.columns
plt.figure(figsize=(12,7))
sns.set_style('darkgrid')
plt.xlabel('Grad.Rate')
plt.title('Graduation Rate based on Private data')
plt.legend(labels=df['Private'])
df.groupby('Private')['Grad.Rate'].hist(stacked=True,alpha=0.6,bins=20)
df.head(1)
g1=sns.FacetGrid(df,hue='Private',size=6,aspect=1.6)
g1.map(plt.hist,'Grad.Rate',alpha=.6,bins=26)
df[df['Grad.Rate']> 100][['Grad.Rate','Private']]
df[df['Grad.Rate']> 100]
df[df['Grad.Rate']> 100]['Grad.Rate'][0]=100
df[df['Grad.Rate']> 100]['Grad.Rate']
df1=df[df['Grad.Rate']> 100][['Grad.Rate','Private']]
df1['Grad.Rate']=100
df1
df[df['Grad.Rate']> 100]['Grad.Rate']=100
df[df['Grad.Rate']> 100]['Grad.Rate']
df.loc[df['Grad.Rate']> 100]['Grad.Rate']
df['Grad.Rate']['Cazenovia College']=100
plt.figure(figsize=(12,7))
sns.set_style('darkgrid')
plt.xlabel('Grad.Rate')
plt.title('Graduation Rate based on Private data')
plt.legend(labels=df['Private'])
df.groupby('Private')['Grad.Rate'].hist(stacked=True,alpha=0.6,bins=20)
from sklearn.cluster import KMeans
km = KMeans(n_clusters=2)
km.fit(df.drop('Private',axis=1))
km.cluster_centers_
km.labels_
cluster=pd.get_dummies(df['Private'],drop_first=True)
# A sample for reference. Not related to this dataframes
df2 = pd.concat([df2,pd.get_dummies(df2['kind'].apply(lambda x: 'P' if x=='P' else 'N')).rename(columns={'P':'Close','N':'Open'},inplace=False)],axis=1)<jupyter_output><empty_output><jupyter_text>Or, we could directly use apply method over the existing dataframe to change the values according to 'Private' True/False<jupyter_code>df.head(1)
def convert_to_cluster_col(private):
    
    if private == 'Yes':
        return 1
    else:
        return 0<jupyter_output><empty_output><jupyter_text>Now, Create a new col called cluster & fill in these values in dataframe using private column.<jupyter_code>df['Cluster']=df['Private'].apply(convert_to_cluster_col)
df.head(1)
pd.get_dummies(df['Private'].rename({'No':'Cluster'}),drop_first=True).head(1)
# A sample for reference. Not related to this dataframes
df2 = pd.concat([df2,pd.get_dummies(df2['kind'].apply(lambda x: 'P' if x=='P' else 'N')).rename(columns={'P':'Close','N':'Open'},inplace=False)],axis=1)
df2=pd.concat([df,cluster],axis=1).rename(columns={'Yes':'Cluster'})
df2.head()
df.head(1)
pd.concat([df,cluster],axis=1).rename(columns={'Yes':'Cluster'}).drop('Private',axis=1,inplace=True)
df.iloc[0:0]
df.index
df.columns
df2.drop('Private',axis=1,inplace=True)
df2.head()<jupyter_output><empty_output><jupyter_text>We do not need train or test data. As labels were already predicted by Kmeans. So, we just need to check against it by comparing the newly created cluster column<jupyter_code>#from sklearn.cross_validation import train_test_split
#X = df2.drop('Cluster',axis=1)
#y=df2['Cluster']
#X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=101)
from sklearn.metrics import classification_report,confusion_matrix
#check df['Cluster'] column
#df['Cluster'].head()
df['Cluster'].tail()
#Not needed, just compare the cluster column with Kmeans_cluster
#pred=km.predict(X_test)
print(confusion_matrix(df['Cluster'],km.labels_))
print('\n')
print(classification_report(df['Cluster'],km.labels_))
#********************************************************************************#<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20969800085070184, 'fraction_numerical': 0.025095703955763504, 'mean_word_length': 8.880252100840336, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 29, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '632787', 'license': 'MIT', 'n_tokens_mistral': 1841, 'n_tokens_neox': 1755, 'n_words': 290, 'path': 'Project_K-Means_Clustering(Identify University).ipynb', 'repo_name': 'sahil-sahay/Machine_Learning_Projects', 'size': '4702'}