概念很高级,原理较简单
一,代码
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
fruits = pd.DataFrame({'数值特征': [5, 6, 7, 8, 9],
'类型特征': ['西瓜','香蕉','橘子','苹果','葡萄']})
fruits['数值特征'] = fruits['数值特征'].astype(str)
fruits_dum = pd.get_dummies(fruits)
print(fruits)
print(fruits_dum)
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)
print('装箱后的数据形态:{}'.format(X_in_bin.shape))
print('装箱后的前十个数据点:{}'.format(X_in_bin[:10]))
line = np.linspace(-5, 5, 1000, endpoint=False).reshape(-1, 1)
new_line = one_hot.transform(np.digitize(line,bins=bins))
mlpr = MLPRegressor().fit(X_in_bin, y)
knr = KNeighborsRegressor().fit(X_in_bin, y)
plt.plot(line, mlpr.predict(new_line), label='MLP')
plt.plot(line, knr.predict(new_line), label='KNN')
plt.plot(X, y, 'o', c='r')
plt.legend(loc='best')
plt.show()
二,效果
2022-04-27 13_43_24-Metis-Org – ai_test.py.png











网友评论