在jupyter笔记本的numpy数组中尝试使用np.dot时,矩阵未对齐



下面的代码我正试图在jupyter笔记本上做,在这里面我不可能做两个矩阵的点积

# 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笔记本中执行上述代码后它显示为错误:矩阵没有对齐

在此处输入图像描述

解决方案

np.dot(butter_prices, weekly_sales.T)作品

解释

阅读本线程的第一个答案,解释为什么会发生这种情况。

最新更新