Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
codebasics
GitHub Repository: codebasics/deep-learning-keras-tf-tutorial
Path: blob/master/4_matrix_math/4_matrix_math.ipynb
1141 views
Kernel: Python 3
import numpy as np

Calculate profit/loss from revenue and expenses

revenue = np.array([[180,200,220],[24,36,40],[12,18,20]]) expenses = np.array([[80,90,100],[10,16,20],[8,10,10]])
profit = revenue - expenses profit
array([[100, 110, 120], [ 14, 20, 20], [ 4, 8, 10]])

Calculate total sales from units and price per unit using matrix multiplication

price_per_unit = np.array([1000,400,1200]) units = np.array([[30,40,50],[5,10,15],[2,5,7]])
price_per_unit*units
array([[30000, 16000, 60000], [ 5000, 4000, 18000], [ 2000, 2000, 8400]])

In above case numpy is using broadcasting so it expands price_per_unit array from 1 row, 3 columns to 3 row and 3 columns. Correct way to do matrix multiplication is to use dot product as shown below

np.dot(price_per_unit,units)
array([34400, 50000, 64400])