<jupyter_start><jupyter_text>IMPORT LIBRARIES<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import altair as alt
import seaborn as sns
from sklearn import preprocessing
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import cross_val_score
from sklearn import preprocessing
from sklearn.feature_selection import RFE
# Model  Building
from sklearn.neighbors import KNeighborsClassifier
# Model Validation
from sklearn.model_selection import KFold
from sklearn.model_selection import LeaveOneOut
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
df = pd.read_csv('E:\ExcelR Assignment\Assignment 13 - KNN\Zoo.csv')
df.head()
df.info()
df.isnull().sum()
df.describe()
df.shape
df.columns
df['type'].value_counts().plot(kind='bar')<jupyter_output><empty_output><jupyter_text>1. Data Analysis & Data Visualization<jupyter_code>df.head()
df1 = df.copy()
df1 = df1.drop(['animal name'],axis=1)
df1.head()<jupyter_output><empty_output><jupyter_text>Splitting the variables<jupyter_code>X = df1.iloc[0:91,0:-1]
Y = df1.iloc[0:91:,-1]<jupyter_output><empty_output><jupyter_text>Up-sampling as the Target Variable is not balanced<jupyter_code>import smote_variants as sv
oversampler= sv.MulticlassOversampling(sv.distance_SMOTE())
X_samp, y_samp= oversampler.sample(X, Y)<jupyter_output>2021-04-09 19:33:06,166:INFO:MulticlassOversampling: Running multiclass oversampling with strategy eq_1_vs_many_successive
2021-04-09 19:33:06,166:INFO:MulticlassOversampling: Sampling minority class with label: 2
2021-04-09 19:33:06,181:INFO:distance_SMOTE: Running sampling via ('distance_SMOTE', "{'proportion': 1.0, 'n_neighbors': 5, 'n_jobs': 1, 'random_state': None}")
2021-04-09 19:33:06,181:INFO:MulticlassOversampling: Sampling minority class with label: 4
2021-04-09 19:33:06,181:INFO:distance_SMOTE: Running sampling via ('distance_SMOTE', "{'proportion': 0.4032258064516129, 'n_neighbors': 5, 'n_jobs': 1, 'random_state': None}")
2021-04-09 19:33:06,197:INFO:MulticlassOversampling: Sampling minority class with label: 7
2021-04-09 19:33:06,197:INFO:distance_SMOTE: Running sampling via ('distance_SMOTE', "{'proportion': 0.27450980392156865, 'n_neighbors': 5, 'n_jobs': 1, 'random_state': None}")
2021-04-09 19:33:06,197:INFO:MulticlassOversampling: Sampling minority class with label: 6[...]<jupyter_text>Train-Test Split Model Validation Technique<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(X,Y,test_size=0.2,random_state=42,stratify=Y)<jupyter_output><empty_output><jupyter_text>Transforming Variables<jupyter_code>from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
X_train = scaler.fit_transform(X_train)
X_test  = scaler.fit_transform(X_test)
from yellowbrick.features import Rank1D
visualizer = Rank1D(algorithm='shapiro')
visualizer.fit(X_train, y_train)                  # Fit the data to the visualizer
visualizer.transform(X_train)                     # Transform the data
visualizer.show()<jupyter_output><empty_output><jupyter_text>Build KNN model<jupyter_code>kmodel = KNeighborsClassifier()
param_grid = [{'n_neighbors':range(2,20)}]
gsv = GridSearchCV(kmodel,param_grid)
gsv.fit(X_train,y_train)
gsv.best_params_,gsv.best_score_
result1 = []
result2 = []
for n in range(2,20):
    model = KNeighborsClassifier(n_neighbors=n,metric='euclidean')
    model.fit(X_train,y_train)
    result1.append(model.score(X_train,y_train))
    result2.append(model.score(X_test,y_test))
frame = pd.DataFrame({'n_neighbors':range(2,20),'Train Accuracy':result1,'Test Accuracy':result2})
frame
plt.plot(frame['n_neighbors'],frame['Train Accuracy'],marker='o',linestyle='dashed')
plt.plot(frame['n_neighbors'],frame['Test Accuracy'],marker='o')
model = KNeighborsClassifier(n_neighbors=13,metric='euclidean')
model.fit(X_train,y_train)
result1 = model.score(X_train,y_train)
result2 = model.score(X_test,y_test)
result1,result2
x_val = df.iloc[-10:,1:-1]
y_val = df.iloc[-10:,-1]
model.score(x_val,y_val)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12643959813771136, 'fraction_numerical': 0.058074001470227885, 'mean_word_length': 8.235294117647058, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '492711', 'license': 'CC0-1.0', 'n_tokens_mistral': 1693, 'n_tokens_neox': 1505, 'n_words': 268, 'path': 'KNN/Assignment 13 Part 2.ipynb', 'repo_name': 'amit0902/Classification', 'size': '4081'}