赞
踩
链接:https://pan.baidu.com/s/1RzZyXsaiJB3e611itF466Q?pwd=j484
提取码:j484
--来自百度网盘超级会员V1的分享
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
from sklearn import metrics
iris_data=pd.read_csv('iris.csv', usecols=[ 1, 2, 3, 4,5])
iris_data.shape
# (150, 5)
iris_data.head()
X = iris_data[['sepal_length', 'sepal_width', 'petal_length', 'petal_width']]
y = iris_data['species']
from sklearn.model_selection import train_test_split
X_train, X_test, r_train, r_test = train_test_split(X, r, random_state=0)
print("X_train shape: {}".format(X_train.shape)) # X_train #shape: (112, 4)
print("r_train shape: {}".format(r_train.shape)) # r_train #shape: (112,)
print("X_test shape: {}".format(X_test.shape))
# X_test shape: (38, 4)
print("r_test shape: {}".format(r_test.shape))
# r_test shape: (38,)
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=1)
knn.fit(X_train, r_train)
r_pred = knn.predict(X_test)
print("Test set predictions: \n {}".format(r_pred))
Test set predictions:
['virginica' 'versicolor' 'setosa' 'virginica' 'setosa' 'virginica'
'setosa' 'versicolor' 'versicolor' 'versicolor' 'virginica' 'versicolor'
'versicolor' 'versicolor' 'versicolor' 'setosa' 'versicolor' 'versicolor'
'setosa' 'setosa' 'virginica' 'versicolor' 'setosa' 'setosa' 'virginica'
'setosa' 'setosa' 'versicolor' 'versicolor' 'setosa' 'virginica'
'versicolor' 'setosa' 'virginica' 'virginica' 'versicolor' 'setosa'
'virginica']
print("Test set score: {:.2f}".format(np.mean(y_pred == y_test)))
# Test set score: 0.97
print('Test set score: {:.2f}'.format(metrics.accuracy_score(r_pred, r_test)))
# Test set score: 0.97
print("Test set score: {:.2f}".format(knn.score(X_test, r_test)))
# Test set score: 0.97
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。