x和y必须具有相同的第一维度numpy
IM试图绘制将日期转换为浮子以在线性回归算法中使用的图形,然后将原始日期用作X轴标签的字符串。当我从CSV文件中绘制实际值时,程序运行正常,但是当我绘制回归值时,我会得到错误提高值(f“ x和y必须具有相同的第一维,但是” valueerror:x和y必须具有相同的第一维,但具有形状(60)和(1),
这是我的代码:
scaler = StandardScaler()
data = pd.read_csv('food.csv')
X = data['Date'].values
X = pd.to_datetime(X, errors="coerce")
X = X.values.astype("float64").reshape(-1,1)
Y = data['TOTAL'].values.reshape(-1,1)
mean_x = np.mean(X)
mean_y = np.mean(Y)
m = len(X)
numer = 0
denom = 0
for i in range(m):
numer += (X[i] - mean_x) * (Y[i] - mean_y)
denom += (X[i] - mean_x) ** 2
m = numer / denom
c = mean_y - (m * mean_x)
print (f'm = {m} \nc = {c}')
max_x = np.max(X) + 100
min_x = np.min(X) - 100
x = np.linspace (min_x, max_x, 100)
y = c + m * x
X= data['Date'].astype('str')
x= data['Date'].astype('str')
print(X.shape)
print(y.shape)
newY = Y.transpose()[0]
newy = y.transpose()[0]
plt.scatter(X, newY, c='#ef5423', label='data points')
plt.plot(x, newy, color='#58b970', label='Regression Line')
plt.show()
Im trying to plot a graph which converts dates into floats to be used in linear regression algorithm and then uses the original dates as strings for the x axis labels. When I plot the actual values from the csv file the program runs ok however when I plot the regression values i get the error raise ValueError(f"x and y must have same first dimension, but "
ValueError: x and y must have same first dimension, but have shapes (60,) and (1,)
here is my code:
scaler = StandardScaler()
data = pd.read_csv('food.csv')
X = data['Date'].values
X = pd.to_datetime(X, errors="coerce")
X = X.values.astype("float64").reshape(-1,1)
Y = data['TOTAL'].values.reshape(-1,1)
mean_x = np.mean(X)
mean_y = np.mean(Y)
m = len(X)
numer = 0
denom = 0
for i in range(m):
numer += (X[i] - mean_x) * (Y[i] - mean_y)
denom += (X[i] - mean_x) ** 2
m = numer / denom
c = mean_y - (m * mean_x)
print (f'm = {m} \nc = {c}')
max_x = np.max(X) + 100
min_x = np.min(X) - 100
x = np.linspace (min_x, max_x, 100)
y = c + m * x
X= data['Date'].astype('str')
x= data['Date'].astype('str')
print(X.shape)
print(y.shape)
newY = Y.transpose()[0]
newy = y.transpose()[0]
plt.scatter(X, newY, c='#ef5423', label='data points')
plt.plot(x, newy, color='#58b970', label='Regression Line')
plt.show()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有数据很难回答,但是通过您的代码
x
的外观,因此y
具有Shape(100,)
(来自<<代码> x = np.linspace(...)命令)。因此,您可能不想仅在行中选择
0
TH元素,因为
newy
只是标量值。如果您省略[0]
,就会发生什么?It's difficult to answer without data, but by the looks of your code
x
, and thereforey
have shape(100,)
(from thex = np.linspace(...)
command).So you probably don't want to pick just the
0
th element in the lineBecause then
newy
is only a scalar value. What happens if you omit[0]
and just do this?