【问题标题】:ValueError: x and y must have same first dimension, but have shapes (10, 1) and (90,)ValueError: x 和 y 必须具有相同的第一维,但具有形状 (10, 1) 和 (90,)
【发布时间】:2020-05-02 14:13:05
【问题描述】:

我正在向 Udemy 学习机器学习。来自多项式回归的讲座之一,以下代码如下:

# importing libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# importing dataset
dataset = pd.read_csv("Position_Salaries.csv")
x = dataset.iloc[:, 1:2].values
y = dataset.iloc[:, 2].values
print(x.shape)
print(y.shape)
# fitting LR to the dataset
from sklearn.linear_model import LinearRegression
linreg = LinearRegression()
linreg.fit(x, y)

# fitting PR to the dataset
from sklearn.preprocessing import PolynomialFeatures
polreg = PolynomialFeatures(degree=2)
x_poly = polreg.fit_transform(x)
linreg2 = LinearRegression()
linreg2.fit(x_poly, y)


# visualising the polynomial regression results
x_grid = np.arange(min(x), max(x), 0.1)
x_grid = x_grid.reshape((len(x_grid), 1))
plt.scatter(x, y, color = "red")
plt.plot(x, linreg2.predict(polreg.fit_transform(x_grid)), color = "blue" )
plt.title("Truth or Bluff PR")
plt.xlabel("Position")
plt.ylabel("Salary")
plt.show()

我收到如下错误:

Traceback (most recent call last):
  File "/home/ashutosh/Machine Learning A-Z Template Folder/Part 2 - Regression/Section 6 - Polynomial Regression/P14-Polynomial-Regression/Polynomial_Regression/plr.py", line 29, in <module>
    plt.plot(x, linreg2.predict(polreg.fit_transform(x_grid)), color = "blue" )
  File "/usr/local/lib/python3.7/dist-packages/matplotlib/pyplot.py", line 2795, in plot
    is not None else {}), **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/matplotlib/axes/_axes.py", line 1666, in plot
    lines = [*self._get_lines(*args, data=data, **kwargs)]
  File "/usr/local/lib/python3.7/dist-packages/matplotlib/axes/_base.py", line 225, in __call__
    yield from self._plot_args(this, kwargs)
  File "/usr/local/lib/python3.7/dist-packages/matplotlib/axes/_base.py", line 391, in _plot_args
    x, y = self._xy_from_xy(x, y)
  File "/usr/local/lib/python3.7/dist-packages/matplotlib/axes/_base.py", line 270, in _xy_from_xy
    "have shapes {} and {}".format(x.shape, y.shape))
ValueError: x and y must have same first dimension, but have shapes (10, 1) and (90,)

数据集可以从这里下载: https://sds-platform-private.s3-us-east-2.amazonaws.com/uploads/P14-Polynomial-Regression.zip

我能做什么?

【问题讨论】:

  • 我的第一步是将linreg2.predict(polreg.fit_transform(x_grid)) 从绘图函数中取出,并将其作为自己的变量进行调查。是你想象中的形状吗?它是否包含您希望它包含的值?
  • plot 认为 2 个输入数组(xy)在形状上不兼容。对于正常绘图,x 变量的每个值都应该有一个 y 值。应该如何针对 10 绘制 90 个值?
  • 您已经拥有print(x.shape)。该图的y 值是由fit_transform 使用x_grid 生成的,它可能有90 个元素?无论如何,您可以测试和纠正数组大小,我们不能。
  • @hpaulj x 和 y 在数据集中具有相同的形状
  • 我指的不是原来的y,而是你传递给plot的第二个参数

标签: python pandas numpy matplotlib machine-learning


【解决方案1】:

在第 29 行,写 x_grid 而不是 x 即, 来自

plt.plot(x, linreg2.predict(polreg.fit_transform(x_grid)), color = "blue" )

plt.plot(x_grid, linreg2.predict(polreg.fit_transform(x_grid)), color = "blue" )

【讨论】:

    猜你喜欢
    • 2018-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 2020-05-21
    • 2021-07-29
    相关资源
    最近更新 更多