机器学习-数据表达与特征工程
一、介绍
二、编程实战
1、使用MLP算法和KNN算法对这个数据集进行回归分析
import numpy as np
import matplotlib.pyplot as plt
from sklearn.neural_network import MLPRegressor
from sklearn.neighbors import KNeighborsRegressor
rnd = np.random.RandomState(38)
x = rnd.uniform(-5,5,size=50)
y_no_noise = (np.cos(6*x)+x)
X = x.reshape(-1,1)
y = (y_no_noise + rnd.normal(size=len(x)))/2
line = np.linspace(-5,5,1000,endpoint=False).reshape(-1,1)
mlpr = MLPRegressor().fit(X,y)
knr = KNeighborsRegressor().fit(X,y)
plt.plot(line, mlpr.predict(line),label='MLP')
plt.plot(line, knr.predict(line),label='KNN')
plt.plot(X,y,'o',c='r')
plt.legend(loc='best')
plt.show()
2、使用装箱处理MLP算法和KNN算法
from sklearn.preprocessing import OneHotEncoder
bins = np.linspace(-5,5,11)
target_bin = np.digitize(X, bins=bins)
print('装箱数据范围: \n{}'.format(bins))
onehot = OneHotEncoder(sparse = False)
onehot.fit(target_bin)
X_in_bin = onehot.transform(target_bin)
print('\n装箱后的数据形态: {}'.format(X_in_bin.shape))
print('装箱后的前十个数据点: \n{}'.format(X_in_bin[:10]))
#装箱处理
new_line = onehot.transform(np.digitize(line,bins=bins))
new_mlpr = MLPRegressor().fit(X_in_bin, y)
new_knr = KNeighborsRegressor().fit(X_in_bin,y)
plt.plot(line, new_mlpr.predict(new_line),label='NEW MLP')
plt.plot(line, new_knr.predict(new_line),label='NEW KNN')
plt.plot(X,y, 'o',c='r')
plt.legend(loc='best')
plt.show()
3、