【问题标题】:matrices are not aligned while trying to use np.dot in numpy arrays in jupyter notebook尝试在 jupyter notebook 的 numpy 数组中使用 np.dot 时,矩阵未对齐
【发布时间】:2021-11-12 10:47:17
【问题描述】:

我正在尝试在 jupyter notebook 中执行以下代码,在此我无法执行两个矩阵的点积

# creating random array
 
np.random.seed(0)
sales_amounts = np.random.randint(20 , size=(5,3))
sales_amounts


# creating weekly sales dataframe

weekly_sales = pd.DataFrame(sales_amounts, index =["Mon","Tues","Wed","Thur","Fri"],
                          columns =["Almond Butter","Peanut Butter","Cashew Butter"])
weekly_sales

# Create the price array
    
    prices = np.array([10,8,12])
    prices

    prices.shape


#Create butter prices dataframe

    butter_prices = pd.DataFrame(prices.reshape(1,3), index=["price"],columns= ["Almond  Butter","Peanut_Butter","Cashew Butter"])
    butter_prices


# shapes not aligned lets transpose

    total_sales = prices.dot(sales_amounts.T)
    total_sales


    #creating daily sales
  
butter_prices.shape,weekly_sales.shape


daily_sales = butter_prices.dot(weekly_sales.T)

在jupyter notebook中执行上述代码后 它显示为错误:矩阵未对齐

【问题讨论】:

    标签: arrays numpy


    【解决方案1】:

    解决方案np.dot(butter_prices, weekly_sales.T) 工作

    说明: 阅读thread 中的第一个答案,该答案解释了为什么会发生这种情况。

    【讨论】:

      猜你喜欢
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      • 2012-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-15
      相关资源
      最近更新 更多