多项式转换,metis里就用上了。。。
一,代码
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import OneHotEncoder
from sklearn.neural_network import MLPRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
'''
array_1 = [1, 2, 3, 4, 5]
array_2 = [6, 7, 8, 9, 0]
array_3 = np.hstack((array_1, array_2))
print('将数组2添加到数据1中后得到:{}'.format(array_3))
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
# plt.plot(X, y, 'o', c='r')
# plt.show()
bins = np.linspace(-5, 5, 11)
target_bin = np.digitize(X, bins=bins)
print('装箱数据范围:{}'.format(bins))
print('前十个数据点特征值:{}'.format(X[:10]))
print('前十个数据点所在的箱子:'.format(target_bin[:10]))
one_hot = OneHotEncoder(sparse=False)
one_hot.fit(target_bin)
X_in_bin = one_hot.transform(target_bin)
X_stack = np.hstack([X, X_in_bin])
print(X_stack.shape)
X_multi = np.hstack([X_in_bin, X*X_in_bin])
print(X_multi.shape)
print(X_multi[0])
line = np.linspace(-5, 5, 1000, endpoint=False).reshape(-1, 1)
new_line = one_hot.transform(np.digitize(line,bins=bins))
line_stack = np.hstack([line, new_line])
line_multi = np.hstack([new_line, line*new_line])
mlpr = MLPRegressor().fit(X_multi, y)
plt.plot(line, mlpr.predict(line_multi), label='MLP for interaction')
plt.ylim(-4, 4)
for vline in bins:
plt.plot([vline, vline], [-5, 5], ':', c='k')
plt.legend(loc='lower right')
plt.plot(X, y, 'o', c='r')
plt.show()
'''
line = np.linspace(-5, 5, 1000, endpoint=False).reshape(-1, 1)
# new_line = one_hot.transform(np.digitize(line,bins=bins))
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
poly = PolynomialFeatures(degree=20, include_bias=False)
X_poly = poly.fit_transform(X)
print(X_poly.shape)
print('原始数据集中的第一个样本特征:{}'.format(X[0]))
print('处理后的数据集中的第一个样本特征:{}'.format(X_poly[0]))
print('PolynomialFeatures对原始数据的处理:{}'.format(poly.get_feature_names()))
LNR_poly = LinearRegression().fit(X_poly, y)
line_poly = poly.transform(line)
plt.plot(line, LNR_poly.predict(line_poly), label='Linear Regressor')
plt.xlim(np.min(X) - 0.5, np.max(X) + 0.5)
plt.ylim(np.min(y) - 0.5, np.max(y) + 0.5)
plt.plot(X, y, 'o', c='r')
plt.legend(loc='lower right')
plt.show()
二,效果


网友评论