Algorithm: KNN for Regression Problem

KNN算法不仅适用于分类问题,还可用于回归问题中的数值预测。例如,预测薪资,选取最近的3个样本计算平均值。特征包括品牌、发动机类型、颜色、生产年份、里程数、保养间隔天数和马力等。
摘要由CSDN通过智能技术生成

The KNN algorithm can not only used in the classify problem, it also can be used for the value prediction as regression problem.

Predict for the salary:

such as K = 3

calculate the distance between all of the sample, and choose the nearest K=3 sample. and calculate the mean value.

Example:

Feature Means:

这里给出了对于数据的简单描述。
Ask Price字段是我们要预测的值,即二手车的估价。

Brand为车辆的牌子。

Type指的是它的发动机类型。

Color字段为车辆外观颜色。

Construction Year字段为车子生产年份。

Odometer为仪表盘已经行驶的里程数。

DaysUntilMOT指的是自从上一次的保养过了多久

HP字段代表的是马力。 "

Using the KNN for Regression example:

import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

# read data
df = pd.read_csv('data.csv')
df # data frame

Output:

# feature processing
# one hot endocding for 'Color
df_colors = df['Color'].str.get_dummies().add_prefix('Color: ')
# one hot encoding for 'Type'
df_type = df['Type'].apply(str).str.get_dummies().add_prefix('Type: ')
# add on hot encoding column
df = pd.concat([df, df_colors, df_type], axis = 1)
# remove the original column before the one hot encoding.
df = df.drop(['Brand', 'Type', 'Color'], axis = 1)

df

 

# data convert
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

# get input data X and lablels Y
X = df[['Construction Year', 'Days Until MOT', 'Odometer']]
y = df['Ask Price'].values.reshape(-1, 1)
# split train, test data set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 41)

# data normalization
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_heighbors = 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)
knn

 

 

[1199. 1199.  700.  899.]
KNeighborsRegressor(algorithm='auto', leaf_size=30, metric='minkowski',
                    metric_params=None, n_jobs=None, n_neighbors=2, p=2,
                    weights='uniform')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值