【机器学习、python】KNN

监督式,提升精度

# 2d类别划分
from sklearn.metrics import accuracy_score
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np


data = pd.read_csv('data.csv')
x = data.drop(['labels'], axis=1)
y = data.loc[:, 'labels']  # 原始数据
pd.value_counts(y)
#KNN开始
from sklearn.neighbors import KNeighborsClassifier
KNN=KNeighborsClassifier(n_neighbors=3)
KNN.fit(x,y)

#预测
y_predict_knn_test=KNN.predict([])
y_predict_knn=KNN.predict(x)
print('knn accuracy:',accuracy_score(y,y_predict_knn))

你可能感兴趣的:(机器学习,python,人工智能)