image_output_1.png
from mlxtend.plotting import plot_decision_regions

from sklearn.neighbors import KNeighborsClassifier

from sklearn.preprocessing import LabelEncoder

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

import pandas as pd

import pandas as pd

import seaborn as sns

import seaborn as sns

import seaborn as sns

import seaborn as sns

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
train = pd.read_excel('/kaggle/input/products/ABBREV_with_CLASS.xlsx')
train

train.isnull().sum()

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
data = train

Q1 = train['Protein_(g)'].quantile(0.25)
Q3 = train['Protein_(g)'].quantile(0.75)
IQR = Q3 - Q1
train = train[(train['Protein_(g)'] >= Q1 - 1.5 * IQR) & (train['Protein_(g)'] <= Q3 + 1.5 * IQR)]

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
data = train

from sklearn.preprocessing import LabelEncoder
labelencoder_Shrt_Desc = LabelEncoder()
train['Shrt_Desc'] = labelencoder_Shrt_Desc.fit_transform(train['Shrt_Desc'])
labelencoder_GmWt_Desc1 = LabelEncoder()
train['GmWt_Desc1'] = labelencoder_GmWt_Desc1.fit_transform(train['GmWt_Desc1'])
labelencoder_GmWt_Desc2 = LabelEncoder()
train['GmWt_Desc2'] = labelencoder_GmWt_Desc2.fit_transform(train['GmWt_Desc2'])

train.isnull().sum()

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

centers = [[1, 1], [-1, -1]]
X = train.drop('CLASS', axis=1)
y = train['CLASS']

clf = KNeighborsClassifier()
clf.fit(X_train, y_train)
clf.score(X_test, y_test)

from mlxtend.plotting import plot_decision_regions

fig, ax = plt.subplots(figsize=(10, 8))
plot_decision_regions(X_test, y_test, clf=clf, legend=2);

clf = KNeighborsClassifier(2)
clf.fit(X_train, y_train)
clf.score(X_test, y_test)

fig, ax = plt.subplots(figsize=(10, 8))
plot_decision_regions(X_test, y_test, clf=clf, legend=2);

centers = [[1, 1], [-1, -1], [1, -1]]
X = train.drop('CLASS', axis=1)
y = train['CLASS']

clf = KNeighborsClassifier()
clf.fit(X_train, y_train)
clf.score(X_test, y_test)

fig, ax = plt.subplots(figsize=(10, 8))
plot_decision_regions(X_test, y_test, clf=clf, legend=2);

scores = []
for k in range(1, 11):
    clf = KNeighborsClassifier(k)
    clf.fit(X_train, y_train)
    score = clf.score(X_test, y_test)
    scores.append(score)

plt.figure(figsize=(12, 3))
sns.lineplot(x=map(str, range(1, 11)), y=scores, marker='o', markersize=10)