<jupyter_start><jupyter_text>IMPORTING LIBRARIES<jupyter_code>import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix
from sklearn.metrics import f1_score
from sklearn.metrics import accuracy_score
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
df = pd.read_csv('wine_data.csv')
#used to read csv files and perform operations on it

df.head()<jupyter_output><empty_output><jupyter_text>HANDLING CATEGORICAL DATA<jupyter_code>x = df.iloc[:, :-1].values
y = df.iloc[:, -1].values
import pandas as pd 
from sklearn.preprocessing import LabelEncoder
x = df.iloc[:, :-1].values 
y = df.iloc[:, -1].values 
labelencoder_X1 = LabelEncoder()
x[:,0] = labelencoder_X1.fit_transform(x[:,0])
x[:,1] = labelencoder_X1.fit_transform(x[:,1])
print(x)
#Here we are using LabelEncoder<jupyter_output>[[  0.   118.     1.71 ...   5.64   1.04   3.92]
 [  0.    65.     1.78 ...   4.38   1.05   3.4 ]
 [  0.    63.     2.36 ...   5.68   1.03   3.17]
 ...
 [  2.    68.     4.28 ...  10.2    0.59   1.56]
 [  2.    64.     2.59 ...   9.3    0.6    1.62]
 [  2.   112.     4.1  ...   9.2    0.61   1.6 ]]<jupyter_text>SPLITTING OF DATA INTO TRAINING AND TEST<jupyter_code>from sklearn.model_selection import train_test_split

#import model selection train test split for splitting the data into test and train for model validation.
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.4, random_state=101)<jupyter_output><empty_output><jupyter_text>NORMALIZING DATA<jupyter_code># data normalization with sklearn
from sklearn.preprocessing import MinMaxScaler

# fit scaler on training data
norm = MinMaxScaler().fit(x_train)

# transform training data
X_train_norm = norm.transform(x_train)

# transform testing data
X_test_norm = norm.transform(x_test)

print(X_train_norm)
print(X_test_norm)<jupyter_output>[[0.5        0.128      0.0712831  ... 0.14920273 0.38461538 0.70188679]
 [0.5        0.16       0.22199593 ... 0.05694761 0.46153846 0.37735849]
 [1.         0.504      0.57637475 ... 0.23917995 0.05128205 0.15471698]
 ...
 [0.5        0.36       0.21792261 ... 0.22779043 0.52991453 0.70566038]
 [0.         0.888      0.15071283 ... 0.35307517 0.53846154 0.58490566]
 [0.5        0.28       0.15885947 ... 0.07972665 0.52991453 0.51320755]]
[[ 0.          0.472       0.18533605  0.79333333  0.3814433   0.23809524
   0.49090909  0.56647399  0.30188679  0.32492114  0.26765376  0.4957265
   0.46792453]
 [ 0.          0.72        0.22199593  0.66666667  0.34020619  0.30952381
   0.48        0.6416185   0.54716981  0.49211356  0.22095672  0.58974359
   0.60377358]
 [ 1.          0.576       0.50916497  0.68        0.56185567  0.16666667
   0.30181818  0.08381503  0.60377358  0.26498423  0.74259681  0.00854701
   0.13207547]
 [ 0.          0.84        0.23421589  0.82        0.22680412  0.273[...]<jupyter_text>GRAPHS<jupyter_code>sns.heatmap(df.corr())<jupyter_output><empty_output><jupyter_text>ALGORITHM<jupyter_code>knn = KNeighborsClassifier(n_neighbors=11,p=2,metric='euclidean')<jupyter_output><empty_output><jupyter_text>FITTING OF TRAINING DATA<jupyter_code>knn.fit(x_train,y_train)
#training or fitting the train data into the model<jupyter_output><empty_output><jupyter_text>PREDICTIONS FOR OUR PROBLEM STATEMENT WITH THE RELATED GRAPHS<jupyter_code>predictions = knn.predict(x_test)
plt.scatter(y_test,predictions)<jupyter_output><empty_output><jupyter_text>EVALUATION<jupyter_code>from sklearn import metrics
cutoff = 0.7                              # decide on a cutoff limit
y_pred_classes = np.zeros_like(predictions)    # initialise a matrix full with zeros
y_pred_classes[predictions > cutoff] = 1       # add a 1 if the cutoff was breached

y_test_classes = np.zeros_like(predictions)
y_test_classes[y_test > cutoff] = 1
print(metrics.accuracy_score(y_test_classes, y_pred_classes))<jupyter_output>1.0<jupyter_text>SAVING THE MODEL USING PICKLE LIBRARY<jupyter_code>import pickle 

# Save the trained model as a pickle string. 
saved_model = pickle.dumps(knn) 

# Load the pickled model 
lm_from_pickle = pickle.loads(saved_model) 

# Use the loaded pickled model to make predictions 
lm_from_pickle.predict(x_test)<jupyter_output><empty_output><jupyter_text>ACCURACY w.r.t TRAINED DATA Confusion Matrix<jupyter_code>from sklearn.metrics import confusion_matrix
y_train=y_train[0:72]
results =confusion_matrix(y_train, predictions)
print(results)<jupyter_output>[[0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 ...
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]]<jupyter_text>Precision, Recall, Support, Fscore<jupyter_code>import numpy
from sklearn.metrics import precision_recall_fscore_support

precision_recall_fscore_support(y_train, predictions, average='macro')<jupyter_output>/usr/local/lib/python3.6/dist-packages/sklearn/metrics/_classification.py:1272: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
  _warn_prf(average, modifier, msg_start, len(result))
/usr/local/lib/python3.6/dist-packages/sklearn/metrics/_classification.py:1272: UndefinedMetricWarning: Recall and F-score are ill-defined and being set to 0.0 in labels with no true samples. Use `zero_division` parameter to control this behavior.
  _warn_prf(average, modifier, msg_start, len(result))<jupyter_text>ACCURACY w.r.t TEST DATA Confusion Matrix<jupyter_code>from sklearn.metrics import confusion_matrix

results =confusion_matrix(y_test, predictions)
print(results)<jupyter_output>[[0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 ...
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]]<jupyter_text>Precision, Recall, Support, Fscore<jupyter_code>import numpy
from sklearn.metrics import precision_recall_fscore_support

precision_recall_fscore_support(y_test, predictions, average='macro')<jupyter_output>/usr/local/lib/python3.6/dist-packages/sklearn/metrics/_classification.py:1272: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
  _warn_prf(average, modifier, msg_start, len(result))
/usr/local/lib/python3.6/dist-packages/sklearn/metrics/_classification.py:1272: UndefinedMetricWarning: Recall and F-score are ill-defined and being set to 0.0 in labels with no true samples. Use `zero_division` parameter to control this behavior.
  _warn_prf(average, modifier, msg_start, len(result))
{'fraction_non_alphanumeric': 0.10807154667067488, 'fraction_numerical': 0.11889373215090937, 'mean_word_length': 3.8854625550660793, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '313761', 'license': 'MIT', 'n_tokens_mistral': 2949, 'n_tokens_neox': 2487, 'n_words': 585, 'path': 'K nearest neighbours/ML-KNN/assignment/ML-ASGN-KNN-05/CODE/wine_classification_knn.ipynb', 'repo_name': 'RASHI3004/Project-Internship---ML-DL', 'size': '6653'}