<jupyter_start><jupyter_text>K-Means - Unsupervised<jupyter_code>from sklearn.preprocessing import StandardScaler
sc=StandardScaler()
x_impute_scaled=sc.fit_transform(x_impute.drop(columns=['Genotype','Treatment','Behavior']))
x_impute_scaled[:,72:78]
x_impute_scaled.shape
x_impute.columns
x_impute_scaled_df=pd.DataFrame(x_impute_scaled,columns=x_impute.columns[0:77])
x_impute_scaled_df['Genotype']=x_impute['Genotype']
x_impute_scaled_df['Genotype']=x_impute['Genotype']
x_impute_scaled_df['Treatment']=x_impute['Treatment']
x_impute_scaled_df['Behavior']=x_impute['Behavior']<jupyter_output><empty_output><jupyter_text>Final X values<jupyter_code>x_impute_scaled_df<jupyter_output><empty_output><jupyter_text>KMEANS Clustering<jupyter_code>from sklearn.cluster import KMeans
cluster_errors = []

for i in range(1,21):
    clusters = KMeans(i)
    clusters.fit(x_impute_scaled_df)
    cluster_errors.append(clusters.inertia_)
#WSS
clusters_df = pd.DataFrame({"Num_clusters":range(1,21),"cluster_errors":cluster_errors})
clusters_df[0:12]
sns.pointplot(clusters_df.Num_clusters,clusters_df.cluster_errors,marker=".")<jupyter_output><empty_output><jupyter_text>Finally we found K=4<jupyter_code>kmeans=KMeans(n_clusters=4).fit(x_impute_scaled_df)
k_means_y=kmeans.labels_
x_impute_scaled_df['k_means_y']=k_means_y
x_impute_scaled_df.head()
x_impute_scaled_df=x_impute_scaled_df.drop(columns=['Genotype','Treatment','Behavior'])
# Final X    x_impute_scaled_df  
# Final Y    k_means_y<jupyter_output><empty_output><jupyter_text>Now Train Test Split<jupyter_code>from sklearn.model_selection import train_test_split
xtrain,xtest,ytrain,ytest=train_test_split(x_impute_scaled_df,k_means_y,test_size=0.3,random_state=0)<jupyter_output><empty_output><jupyter_text>PCA<jupyter_code>from sklearn.decomposition import PCA

pca=PCA(n_components=4)
xtrain_pca=pca.fit_transform(xtrain)
xtest_pca=pca.transform(xtest)
from matplotlib import pyplot as plt
plt.plot(pca.explained_variance_ratio_)
plt.xlabel('No of components')
plt.ylabel('Cumulative explained variance')
plt.show()<jupyter_output><empty_output><jupyter_text>From PCA We found the number of features to be 4  Instatiating various models<jupyter_code>from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
knn=KNeighborsClassifier()
RF=RandomForestClassifier()
DT=DecisionTreeClassifier()
LR=LogisticRegression()
NB=GaussianNB()
SVM=SVC()
from sklearn import metrics
from sklearn.metrics import accuracy_score
import warnings
warnings.filterwarnings("ignore")
unsup_accuracy_pca=[]

for name, model in zip(['KNN','RandomForest','Logistic_Reg','Naive Bayes','SVM','DecisonTree'],
                      [knn,RF,LR,NB,SVM,DT]):
    model.fit(xtrain_pca,ytrain)
    ypred_unsup_pca=model.predict(xtest_pca)
    accuracy =metrics.accuracy_score(ypred_unsup_pca,ytest)
    unsup_accuracy_pca.append(accuracy)
    print("Accuracy of unsupervised model with PCA %s: %0.02f"%(name,accuracy))
unsup_accuracy=[]

for name, model in zip(['KNN','RandomForest','Logistic_Reg','Naive Bayes','SVM','DecisonTree'],
                      [knn,RF,LR,NB,SVM,DT]):
    model.fit(xtrain,ytrain)
    ypred_unsup_no_pca=model.predict(xtest)
    accuracy =metrics.accuracy_score(ypred_unsup_no_pca,ytest)
    unsup_accuracy.append(accuracy)
    print("Accuracy of unsupervised model without PCA %s: %0.02f"%(name,accuracy))<jupyter_output>Accuracy of unsupervised model without PCA KNN: 0.86
Accuracy of unsupervised model without PCA RandomForest: 0.92
Accuracy of unsupervised model without PCA Logistic_Reg: 0.89
Accuracy of unsupervised model without PCA Naive Bayes: 0.94
Accuracy of unsupervised model without PCA SVM: 0.93
Accuracy of unsupervised model without PCA DecisonTree: 0.87<jupyter_text>SUPERVISED<jupyter_code>xtrain_sup,xtest_sup,ytrain_sup,ytest_sup=train_test_split(x_impute_scaled_df,y,test_size=0.3,random_state=42)
sup_accuracy=[]

for name, model in zip(['KNN','RandomForest','Logistic_Reg','Naive Bayes','SVM','DecisonTree'],
                      [knn,RF,LR,NB,SVM,DT]):
    model.fit(xtrain_sup,ytrain_sup)
    ypred_sup_pca=model.predict(xtest_sup)
    accuracy =metrics.accuracy_score(ypred_sup_pca,ytest_sup)
    sup_accuracy.append(accuracy)
    print("Accuracy of supervised model without PCA %s: %0.02f"%(name,accuracy))<jupyter_output>Accuracy of supervised model without PCA KNN: 0.95
Accuracy of supervised model without PCA RandomForest: 0.95
Accuracy of supervised model without PCA Logistic_Reg: 0.98
Accuracy of supervised model without PCA Naive Bayes: 0.77
Accuracy of supervised model without PCA SVM: 0.99
Accuracy of supervised model without PCA DecisonTree: 0.85<jupyter_text>INFERENCE:<jupyter_code>pd.DataFrame({'MODEL':['Unsupervised model with PCA KNN',
                       'Unsupervised model with PCA RandomForest',
                        'Unsupervised model with PCA Logistic_Reg',
                      'Unsupervised model with PCA Naive Bayes',
                       'Unsupervised model with PCA SVM',
                       'Unsupervised model with PCA DecisonTree',
                       'Unsupervised model without PCA KNN',
                      'Unsupervised model without PCA RandomForest',
                      'Unsupervised model without PCA Logistic_Reg',
                      'Unsupervised model without PCA Naive Bayes',
                      'Unsupervised model without PCA SVM',
                      'Unsupervised model without PCA DecisonTree',
                       'Supervised model without PCA KNN',
                       'Supervised model without PCA RandomForest',
                       'Supervised model without PCA Logistic_Reg',
                       'Supervised model without PCA Naive Bayes',
                       'Supervised model without PCA SVM',
                       'Supervised model without PCA DecisonTree'
                       
                       
                      
                      
                      ],
              'ACCURACY':[0.96,0.93,0.94,0.95,0.94,0.92,0.86,0.91,0.89,0.94,0.93,0.87,0.95,0.94,0.98,0.77,0.99,0.85]         
})
pd.DataFrame({'Accuracy of unsupervised model with PCA':unsup_accuracy_pca,
             'Accuracy of unsupervised model without PCA':unsup_accuracy,
             'Accuracy of supervised model without PCA':sup_accuracy}, 
             index=['KNN','RandomForest','Logistic_Reg','Naive Bayes','SVM','DecisonTree'])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10042092603728202, 'fraction_numerical': 0.01894167167769092, 'mean_word_length': 4.480230642504119, 'pattern_counts': {'":': 2, '<': 35, '<?xml version=': 0, '>': 35, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '648866', 'license': 'MIT', 'n_tokens_mistral': 2431, 'n_tokens_neox': 2234, 'n_words': 400, 'path': 'Mice Protein Expression Data Set .ipynb', 'repo_name': 'SANDEEP-I/Mice', 'size': '6652'}