You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

sklearn调用make_regression设n_features>1时报x与y尺寸需一致错误

问题原因
  • 你调用make_regression设置n_features=2时,返回的x是形状为(150, 2)的二维数组,而y是形状为(150,)的一维数组
  • plt.scatter()要求传入的前两个参数(x轴、y轴坐标序列)必须是长度相同的一维数组,你直接传入二维的x时,matplotlib无法对齐维度,才会抛出尺寸不匹配的报错,并非你生成的x和y的样本总量不一致。
解决方法

场景1:单独绘制每个特征和目标值y的散点图

from sklearn.datasets import make_regression
import matplotlib.pyplot as plt

x,y=make_regression(n_samples=150, n_features=2,noise=20)
# 绘制第一个特征和y的散点
plt.scatter(x[:,0], y, label='特征1')
# 绘制第二个特征和y的散点
plt.scatter(x[:,1], y, label='特征2')
plt.legend()
plt.show()

场景2:在3D坐标系中同时展示两个特征和y的关系

from sklearn.datasets import make_regression
import matplotlib.pyplot as plt

x,y=make_regression(n_samples=150, n_features=2,noise=20)
ax = plt.axes(projection='3d')
ax.scatter3D(x[:,0], x[:,1], y)
ax.set_xlabel('特征1')
ax.set_ylabel('特征2')
ax.set_zlabel('目标值y')
plt.show()

内容的提问来源于stack exchange,提问作者Taresh Gupta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 14:06:05