【问题标题】:ValueError: non-broadcastable output operand with shape (1,) doesn't match the broadcast shape (1,15)ValueError:形状为 (1,) 的不可广播输出操作数与广播形状 (1,15) 不匹配
【发布时间】:2022-12-03 13:02:49
【问题描述】:

运行此代码后,我不断收到相同的错误:

注意:(数据在 excel 文件中(高度:16 列)和(权重:16 列)

我试图改变 epochs_num 但它一直出现同样的问题......

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# Load the dataset
data = pd.read_csv('heights_weights.csv')
# Plot the data distribution
plt.scatter(data['Height'], data['Weight'], color='b')
plt.xlabel('Height')
plt.ylabel('Weight')
plt.title('Height vs. Weight')
plt.show()
# Define the linear regression model
def linearRegression_model(X, weights):
   y_pred = np.dot(X, weights)
   return y_pred
# Define the update weights function
def linearRegression_update_weights(X, y, weights, learning_rate):
   y_pred = linearRegression_model(X, weights)
   weights_delta = np.dot(X.T, y_pred - y)
   m = len(y)
   weights -= (learning_rate/m) * weights_delta
   return weights
# Define the train function
def linearRegression_train(X, y, learning_rate, num_epochs):
   # Initialize weights and bias
   weights = np.zeros(X.shape[1])
   for epoch in range(num_epochs):
       weights = linearRegression_update_weights(X, y, weights, learning_rate)
       if (epoch % 100 == 0):
           print('epoch: %s, weights: %s' % (epoch, weights))
   return weights
# Define the predict function
def linearRegression_predict(X, weights):
   y_pred = linearRegression_model(X, weights)
   return y_pred
# Define the mean squared error function
def mean_squared_error(y_true, y_pred):
   mse = np.mean(np.power(y_true-y_pred, 2))
   return mse
# Prepare the data
X = data['Height'].values.reshape(-1, 1)
y = data['Weight'].values.reshape(-1, 1)
# Train the model
lr = 0.01
n_epochs = 1000
weights = linearRegression_train(X, y, lr, n_epochs)
# Predict
y_pred = linearRegression_predict(X, weights)
# Evaluate the model
mse = mean_squared_error(y, y_pred)
print('Mean Squared Error: %s' % mse)
# Plot the regression line
plt.scatter(data['Height'], data['Weight'], color='b')
plt.plot(X, y_pred, color='k')
plt.xlabel('Height')
plt.ylabel('Weight')
plt.title('Height vs. Weight')
plt.show()
# Plot the predicted and actual values
plt.scatter(data['Height'], y, color='b', label='Actual')
plt.scatter(data['Height'], y_pred, color='r', label='Predicted')
plt.xlabel('Height')
plt.ylabel('Weight')
plt.title('Actual vs. Predicted')
plt.legend()
plt.show()

我尝试使用相同的代码在 google colab 中逐步运行,我还将纪元更改为 62 并运行多次但仍然相同:

ValueError                                Traceback (most recent call last)
<ipython-input-23-98703406a0a3> in <module>
      2 learning_rate = 0.01
      3 num_epochs = 62
----> 4 weights = linearRegression_train(X, y, learning_rate, num_epochs)

1 frames
<ipython-input-12-8f66dacdd5fc> in linearRegression_update_weights(X, y, weights, learning_rate)
      4    weights_delta = np.dot(X.T, y_pred - y)
      5    m = len(y)
----> 6    weights -= (learning_rate/m) * weights_delta
      7    return weights

ValueError: non-broadcastable output operand with shape (1,) doesn't match the broadcast shape (1,15)

【问题讨论】:

    标签: python pandas numpy linear-regression


    【解决方案1】:

    我可以重现错误消息

    In [5]: x=np.array([1])
    
    In [6]: x+=np.ones((1,5),int)
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    Input In [6], in <cell line: 1>()
    ----> 1 x+=np.ones((1,5),int)
    
    ValueError: non-broadcastable output operand with shape (1,) doesn't match the broadcast shape (1,5)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-09
      • 2020-03-07
      • 2020-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多