x和y必须具有相同的第一维度numpy

发布于 2025-01-27 08:56:13 字数 1147 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吝吻 2025-02-03 08:56:13

没有数据很难回答,但是通过您的代码x的外观,因此y具有Shape (100,)(来自<<代码> x = np.linspace(...)命令)。

因此,您可能不想仅在行中选择0 TH元素,

newy = y.transpose()[0]

因为newy只是标量值。如果您省略[0],就会发生什么?

newy = y.transpose()

It's difficult to answer without data, but by the looks of your code x, and therefore y have shape (100,) (from the x = np.linspace(...) command).

So you probably don't want to pick just the 0th element in the line

newy = y.transpose()[0]

Because then newy is only a scalar value. What happens if you omit [0] and just do this?

newy = y.transpose()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文