简体   繁体   English

ValueError: x 和 y 必须具有相同的第一维,但具有形状 (10, 1) 和 (90,)

[英]ValueError: x and y must have same first dimension, but have shapes (10, 1) and (90,)

I'm learning ML from Udemy.我正在从 Udemy 学习机器学习。 From one of the lectures of Polynomial Regression, the following code is as follows:从Polynomial Regression的一节课中,下面的代码如下:

# 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()

I got error as follows:我得到如下错误:

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,)

the data set can be downloaded from here: https://sds-platform-private.s3-us-east-2.amazonaws.com/uploads/P14-Polynomial-Regression.zip数据集可以从这里下载: https : //sds-platform-private.s3-us-east-2.amazonaws.com/uploads/P14-Polynomial-Regression.zip

What can I do?我能做什么?

int the line 29, write x_grid instead of x ie, from在第 29 行,写x_grid而不是x即,从

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

to

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:x 和 y 必须具有相同的第一维,但具有形状 (1, 2) 和 (2,) - ValueError: x and y must have same first dimension, but have shapes (1, 2) and (2,) ValueError:x 和 y 必须具有相同的第一维,但具有形状 - ValueError: x and y must have same first dimension, but have shapes ValueError:x 和 y 必须具有相同的第一维,但具有形状 (6,) 和 (8,) - ValueError: x and y must have same first dimension, but have shapes (6,) and (8,) 谁能帮我? ValueError:x 和 y 必须具有相同的第一维,但具有形状 (10,) 和 (0,) - Can anyone help me? ValueError: x and y must have same first dimension, but have shapes (10,) and (0,) 线性回归:ValueError:x 和 y 必须具有相同的第一维,但具有形状 (10, 1) 和 (1, 1) - Linear Regression : ValueError: x and y must have same first dimension, but have shapes (10, 1) and (1, 1) Matplotlib 中的 Plot K-Means:ValueError:x 和 y 必须具有相同的第一个维度,但具有形状 (10,) 和 (1,) - Plot K-Means in Matplotlib: ValueError: x and y must have same first dimension, but have shapes (10,) and (1,) 线性回归模型形状 - ValueError:x 和 y 必须具有相同的第一维,但具有形状 (5,) 和 (1, 5) - Linear regression model shapes - ValueError: x and y must have same first dimension, but have shapes (5,) and (1, 5) ValueError: x 和 y 必须具有相同的第一维,但具有形状 (101,) 和 (100,) - ValueError: x and y must have same first dimension, but have shapes (101,) and (100,) ValueError: x 和 y 必须具有相同的第一维,但具有形状 (1,) 和 (224, 224, 3) - ValueError: x and y must have same first dimension, but have shapes (1,) and (224, 224, 3) ValueError: x 和 y 必须具有相同的第一维,但具有形状 (50,) 和 (1, 50)/ 多处理 - ValueError: x and y must have same first dimension, but have shapes (50,) and (1, 50)/ Multiprocessing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM