from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import seaborn as sns
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
from sklearn import datasets
iris= sns.load_dataset('iris')
iris
x_train, x_test, y_train, y_test= train_test_split(
iris.iloc[:,:-1],
iris.iloc[:,-1],
test_size= 0.15
)
def init_model(k, x_train, y_train, x_test):
model = KNeighborsClassifier(n_neighbors=k)
print(model)
model.fit(x_train,y_train)
y_pred = model.predict(x_test)
return y_pred
def grath(y_test,x_test,y_pred):
plt.figure(figsize=(10, 7))
sns.scatterplot(x='petal_width', y='petal_lenght',data=iris,hue = 'species', s=70)
plt.xlabel('Длина лепестка,см')
plt.ylabel('Ширина лепестка,см')
plt.legend(loc=2)
plt.grid()
for i in range(len(y_test)):
if np.array(y_test)[i] != y_pred[i]:
plt.scatter(x_test.iloc[i,3],x_test.iloc[i,2],color = 'red', s = 150)
x_train.shape, x_test.shape, y_train.shape, y_test.shape
x_train.head()
y_train.head()
k = int(input('Чему равно k? '))
y_pred = init_model(k,x_train,y_train,x_test)
print((f'accuracy:{accuracy_score(y_perd,y_test) :.3}'))
grath(y_test,x_test,y_pred)
print((f'accuracy:{accuracy_score(y_perd,y_test) :.3}'))
вот в этой строчке, ты не смог подправить не правильно написанное имя переменной, y_pred должно быть.sns.scatterplot(x='petal_width', y='petal_lenght',data=iris,hue = 'species', s=70)
должно быть y='petal_length'from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import seaborn as sns
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
from sklearn import datasets
iris= sns.load_dataset('iris')
#iris
x_train, x_test, y_train, y_test= train_test_split(
iris.iloc[:,:-1],
iris.iloc[:,-1],
test_size= 0.15
)
def init_model(k, x_train, y_train, x_test):
model = KNeighborsClassifier(n_neighbors=k)
# print(model)
model.fit(x_train,y_train)
y_pred = model.predict(x_test)
return y_pred
def grath(y_test,x_test,y_pred):
plt.figure(figsize=(10, 7))
sns.scatterplot(x='petal_width', y='petal_length',data=iris,hue = 'species', s=70)
plt.xlabel('Длина лепестка,см')
plt.ylabel('Ширина лепестка,см')
plt.legend(loc=2)
plt.grid()
for i in range(len(y_test)):
if np.array(y_test)[i] != y_pred[i]:
plt.scatter(x_test.iloc[i,3],x_test.iloc[i,2],color = 'red', s = 150)
# x_train.shape, x_test.shape, y_train.shape, y_test.shape
# x_train.head()
# y_train.head()
k = int(input('Чему равно k? '))
y_pred = init_model(k,x_train,y_train,x_test)
print((f'accuracy:{accuracy_score(y_pred,y_test) :.3}'))
grath(y_test,x_test,y_pred)