我想为具有多个特征的预测创建轮廓图。其余值应固定以绘制两个有趣的值。不幸的是,我得到的矩阵在所有位置上都具有相同的值,而不是期望值。
我认为我的矩阵有问题,但是我没有找到错误。
[...]
f_learn = [x_1,x_2,x_3,x_4]
r_lear = [r_1]
clf = svm.MLPRegressor(...)
clf.fit(f_learn,r_learn)
[...]
x_1 = np.linspace(1, 100, 100)
x_2 = np.linspace(1, 100, 100)
X_1, X_2 = np.meshgrid(x_1, x_2)
x_3 = np.full( (100,100), 5).ravel()
x_4 = np.full( (100,100), 15).ravel()
predict_matrix = np.vstack([X_1.ravel(), X_2.ravel(), x_3,x_4])
prediction = clf.predict(predict_matrix.T)
prediction_plot = prediction.reshape(X_1.shape)
plt.figure()
cp = plt.contourf(X_1, X_2, prediction_plot, 10)
plt.colorbar(cp)
plt.show()
如果我逐行手动测试矩阵,我将获得正确的结果。但是,如果我以这种方式将它们放在一起,那是行不通的。
编辑:复制代码时出错
数据示例。所有答案均为7.5,无差异;(
import matplotlib.pyplot as plt
import numpy as np
from sklearn import linear_model
f_learn = np.array([[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]])
r_learn = np.array([6,7,8,9])
reg = linear_model.LinearRegression()
reg.fit (f_learn, r_learn)
x_1 = np.linspace(0, 20, 10)
x_2 = np.linspace(0, 20, 10)
X_1, X_2 = np.meshgrid(x_1, x_2)
x_3 = np.full( (10,10), 5).ravel()
x_4 = np.full( (10,10), 2).ravel()
predict_matrix = np.vstack([X_1.ravel(), X_2.ravel(), x_3, x_4])
prediction = reg.predict(predict_matrix.T)
prediction_plot = prediction.reshape(X_1.shape)
plt.figure()
cp = plt.contourf(X_1, X_2, prediction_plot, 10)
plt.colorbar(cp)
plt.show()
答案 0 :(得分:0)
尝试类似这样的方法。代码中的一些注释
x_1 = np.linspace(1, 100, 100)
x_2 = np.linspace(1, 100, 100)
X_1, X_2 = np.meshgrid(x_1, x_2)
# Why the shape was (1000, 100)?
x_3 = np.full((100, 100), 5).ravel()
x_4 = np.full((100, 100), 15).ravel()
# you should use X_1.ravel() to make it column vector (it is one feature)
# there was x_3 insted of x_4
predict_matrix = np.vstack([X_1.ravel(), X_2.ravel(), x_3, x_4])
prediction = clf.predict(predict_matrix.T)
prediction_plot = prediction.reshape(X_1.shape)
plt.figure()
cp = plt.contourf(X_1, X_2, prediction_plot, 10)
plt.colorbar(cp)
plt.show()
答案 1 :(得分:0)
以下代码应为您提供所需的轮廓图。
from sklearn.datasets import make_regression
f_learn, r_learn = make_regression(20,4)
reg = linear_model.LinearRegression()
reg.fit (f_learn, r_learn)
x_1 = np.linspace(-2, 2, 10)
x_2 = np.linspace(-2, 2, 10)
X_1, X_2 = np.meshgrid(x_1, x_2)
x_3 = np.full( (10,10), 0.33).ravel()
x_4 = np.full( (10,10), 0.99).ravel()
predict_matrix = np.vstack([X_1.ravel(), X_2.ravel(), x_3, x_4])
prediction = reg.predict(predict_matrix.T)
prediction_plot = prediction.reshape(X_1.shape)
plt.figure()
cp = plt.contourf(X_1, X_2, prediction_plot, 10)
plt.colorbar(cp)
plt.show()
答案 2 :(得分:0)
在您的玩具数据中,有4个具有相同特征值和不同标签的示例。 LinearRegression不会从中学到任何东西。您可以通过以下方式进行检查:
>>> reg.coef_
[0. 0. 0. 0.]
在您的真实数据中也许也是如此。功能x_1,x_2无关紧要。如果功能x_1,x_2的值不太小,请检查reg.coef_
。
我更改了玩具数据,情节正在发挥作用。
import matplotlib.pyplot as plt
import numpy as np
from sklearn import linear_model
# f_learn = np.array([[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]])
# r_learn = np.array([6,7,8,9])
f_learn = np.arange(20.).reshape(5, 4)
f_learn += np.random.randn(5, 4)
r_learn = f_learn[:, 0] + 2 * f_learn[:, 1] + 3 * f_learn[:, 2] + 4 * f_learn[:, 3]
reg = linear_model.LinearRegression()
reg.fit(f_learn, r_learn)
print(reg.coef_)
x_1 = np.linspace(0, 20, 10)
x_2 = np.linspace(0, 20, 10)
X_1, X_2 = np.meshgrid(x_1, x_2)
x_3 = np.full( (10,10), 5).ravel()
x_4 = np.full( (10,10), 2).ravel()
predict_matrix = np.vstack([X_1.ravel(), X_2.ravel(), x_3, x_4])
prediction = reg.predict(predict_matrix.T)
prediction_plot = prediction.reshape(X_1.shape)
plt.figure()
cp = plt.contourf(X_1, X_2, prediction_plot, 10)
plt.colorbar(cp)
plt.show()