'matrices are not aligned while trying to use np.dot in numpy arrays in jupyter notebook
The below code I am trying to do in jupyter notebook and in this i am not possible to do dot product of two matrices
# 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)
After executing the above code in jupyter notebook it shows as error: matrices are not aligned
Solution 1:[1]
Solution:
np.dot(butter_prices, weekly_sales.T) works
Explanation:
Read the first answer from this thread that explains why this is happening.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nick Vu |
