1 import numpy as np 2 import matplotlib.pyplot as plt 3 from sklearn.datasets import make_blobs 4 from sklearn.neighbors import KNeighborsRegressor 5 from sklearn.datasets import make_regression 6 from sklearn.datasets import load_wine 7 from sklearn.model_selection import train_test_split 8 9 wine_dataset = load_wine() 10 X_train,X_test,y_train,y_test = train_test_split(wine_dataset[‘data‘],wine_dataset[‘target‘],random_state=0) 11 #将random_state = 0是因为tarin_test_split函数会生成一个为随机函数,并且会根据这个伪随机数对数据集进行拆分 12 knn = KNeighborsRegressor(n_neighbors=1) 13 14 #查看参数设定 15 knn.fit(X_train,y_train) 16 print(knn) 17 print(‘模型得分:{:,.2f}‘.format(knn.score(X_test,y_test))) 18 19 #预测新红酒的分类 20 X_new = np.array([[13.2, 2.77, 2.51, 18.5, 96.6, 1.04, 2.55, 0.57, 1.47, 6.21, 1.05, 3.33, 820]]) 21 prediction = knn.predict(X_new) 22 print("预测新红酒的分类为:{}".format(wine_dataset[‘target_names‘][prediction])) 23 #print(‘X-_train shape:{}‘.format(X_train.shape)) 24 # print("红酒数据集中的键:\n{}".format(wine_dataset.keys())) 25 # 26 # print("数据概况:{}".format(wine_dataset[‘data‘].shape)) 27 # 28 # print(wine_dataset[‘DESCR‘])
以上代码是一个关于酒分类的问题
具体的后面还会继续做
原文地址:https://www.cnblogs.com/weiyang2/p/11959344.html
时间: 2024-10-15 06:28:49