代码实现:KNN做分类
#读取相应的库
from sklearn import datasets#使用sklearn自带的数据集
from sklearn.model_selection import train_test_split#把数据分成训练集和测试集
from sklearn.neighbors import KNeighborsClassifier
import numpy as np
#读取数据
iris=datasets.load_iris()#UCI dataset 3分类问题
x=iris.data #特征,矩阵大小为N*D。N是样本的个数,D是样本的纬度
y=iris.target #标签(0,1,2)是一个向量,针对每一个样本都有一个标签
print (x,y)
#把数据分成训练数据和测试数据,默认情况下,测试数据占比25%
x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=2003)#2003是一个随机的种子
#构建knn模型,K值为3,并做训练
clf=KNeighborsClassifier(n_neighbors=4)
clf.fit(x_train,y_train)#训练的过程
#计算准确率
from sklearn.metrics import accuracy_score
correct=np.count_nonzero((clf.predict(x_test)==y_test)==True)
print('Accuracy is:%.3f'%(correct/len(x_test)))
代码实现:KNN做回归:二手车价格估计
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns#用来可视化的
#读取数据
df=pd.read_csv('/home/kesci/data.csv')
#df#dataframe,是pandas里面的一种数据结构,预测的是二手车的价格
#数据清洗
#把颜色标成独热编码one-hot
df_colors=df['Color'].str.get_dummies().add_prefix('Color:')
#把类型独热编码
df_type=df['Type'].apply(str).str.get_dummies().add_prefix('Type:')#把type当成一个字符串类型对待
#添加独热编码到数据列
df=pd.concat([df,df_colors,df_type],axis=1)
#去除独热编码对应的原始列
df=df.drop(['Brand','Type','Color'],axis=1)
#用heatmap来看一下每一维特征和价格之间的关系
#数据转化
matrix=df.corr()
f,ax=plt.subplots(figsize=(8,6))
sns.heatmap(matrix,square=True)
plt.title('Car Price Variables')
from sklearn.neighbors import KNeighborsRegressor
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
from sklearn.preprocessing import StandardScaler
import numpy as np
X = df[['Construction Year', 'Days Until MOT', 'Odometer']]
y = df['Ask Price'].values.reshape(-1, 1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=41)
X_normalizer = StandardScaler() # N(0,1)
X_train = X_normalizer.fit_transform(X_train)
X_test = X_normalizer.transform(X_test)
y_normalizer = StandardScaler()
y_train = y_normalizer.fit_transform(y_train)
y_test = y_normalizer.transform(y_test)
knn = KNeighborsRegressor(n_neighbors=2)
knn.fit(X_train, y_train.ravel())
#Now we can predict prices:
y_pred = knn.predict(X_test)
y_pred_inv = y_normalizer.inverse_transform(y_pred)
y_test_inv = y_normalizer.inverse_transform(y_test)
# Build a plot
plt.scatter(y_pred_inv, y_test_inv)
plt.xlabel('Prediction')
plt.ylabel('Real value')
# Now add the perfect prediction line
diagonal = np.linspace(500, 1500, 100)
plt.plot(diagonal, diagonal, '-r')
plt.xlabel('Predicted ask price')
plt.ylabel('Ask price')
plt.show()
print(y_pred_inv)