Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c00f3f7

Browse files
committedAug 3, 2020
matrix math
1 parent 0fb27a5 commit c00f3f7

7 files changed

+534
-0
lines changed
 
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import numpy as np"
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"<h3 style='color:blue'>Calculate profit/loss from revenue and expenses</h3>"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": 4,
22+
"metadata": {},
23+
"outputs": [],
24+
"source": [
25+
"revenue = np.array([[180,200,220],[24,36,40],[12,18,20]])\n",
26+
"expenses = np.array([[80,90,100],[10,16,20],[8,10,10]])"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": 5,
32+
"metadata": {
33+
"scrolled": true
34+
},
35+
"outputs": [
36+
{
37+
"data": {
38+
"text/plain": [
39+
"array([[100, 110, 120],\n",
40+
" [ 14, 20, 20],\n",
41+
" [ 4, 8, 10]])"
42+
]
43+
},
44+
"execution_count": 5,
45+
"metadata": {},
46+
"output_type": "execute_result"
47+
}
48+
],
49+
"source": [
50+
"profit = revenue - expenses\n",
51+
"profit"
52+
]
53+
},
54+
{
55+
"cell_type": "markdown",
56+
"metadata": {},
57+
"source": [
58+
"<h3 style='color:blue'>Calculate total sales from units and price per unit using matrix multiplication</h3>"
59+
]
60+
},
61+
{
62+
"cell_type": "code",
63+
"execution_count": 6,
64+
"metadata": {},
65+
"outputs": [],
66+
"source": [
67+
"price_per_unit = np.array([1000,400,1200])\n",
68+
"units = np.array([[30,40,50],[5,10,15],[2,5,7]])"
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": 7,
74+
"metadata": {
75+
"scrolled": true
76+
},
77+
"outputs": [
78+
{
79+
"data": {
80+
"text/plain": [
81+
"array([[30000, 16000, 60000],\n",
82+
" [ 5000, 4000, 18000],\n",
83+
" [ 2000, 2000, 8400]])"
84+
]
85+
},
86+
"execution_count": 7,
87+
"metadata": {},
88+
"output_type": "execute_result"
89+
}
90+
],
91+
"source": [
92+
"price_per_unit*units"
93+
]
94+
},
95+
{
96+
"cell_type": "markdown",
97+
"metadata": {},
98+
"source": [
99+
"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"
100+
]
101+
},
102+
{
103+
"cell_type": "code",
104+
"execution_count": 8,
105+
"metadata": {},
106+
"outputs": [
107+
{
108+
"data": {
109+
"text/plain": [
110+
"array([34400, 50000, 64400])"
111+
]
112+
},
113+
"execution_count": 8,
114+
"metadata": {},
115+
"output_type": "execute_result"
116+
}
117+
],
118+
"source": [
119+
"np.dot(price_per_unit,units)"
120+
]
121+
}
122+
],
123+
"metadata": {
124+
"kernelspec": {
125+
"display_name": "Python 3",
126+
"language": "python",
127+
"name": "python3"
128+
},
129+
"language_info": {
130+
"codemirror_mode": {
131+
"name": "ipython",
132+
"version": 3
133+
},
134+
"file_extension": ".py",
135+
"mimetype": "text/x-python",
136+
"name": "python",
137+
"nbconvert_exporter": "python",
138+
"pygments_lexer": "ipython3",
139+
"version": "3.7.3"
140+
}
141+
},
142+
"nbformat": 4,
143+
"nbformat_minor": 2
144+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#### Exercise: Matrix Math
2+
1. Below is some indian companies revenues in US dollars. Using numpy can you convert this into Indian rupees? 1 USD = 75 INR
3+
4+
![](revenue_usd.jpg)
5+
6+
2. Divine flowers is a flower shop that sells different type of flowers. Below is the table showing how many flowers of each type they sold in different months. Also given are the prices of one flower each. Using this find out their total sales in every month.
7+
8+
![](flowers.jpg)
9+
10+
[Click here for solution of 1 and 2](https://github.com/codebasics/py/tree/master/DeepLearningML/4_matrix_math/4_matrix_math_exercise_solution.ipynb)
11+
12+
3. Here is some matrix exercise from mathisfun.com. Please click on a link below and do the exercise.
13+
14+
[Click me for matrix exercise](https://www.mathopolis.com/questions/q.html?id=714&t=mif&qs=714_715_716_717_2394_2395_2397_2396_8473_8474_8475_8476&site=1&ref=2f616c67656272612f6d61747269782d6d756c7469706c79696e672e68746d6c&title=486f7720746f204d756c7469706c79204d61747269636573)
15+
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 5,
6+
"metadata": {
7+
"scrolled": true
8+
},
9+
"outputs": [],
10+
"source": [
11+
"import numpy as np"
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"metadata": {},
17+
"source": [
18+
"<h3 style='color:blue'>Solution 1: Convert USD revenues to INR</h3>"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 4,
24+
"metadata": {},
25+
"outputs": [
26+
{
27+
"data": {
28+
"text/plain": [
29+
"array([[15000, 16500, 18750],\n",
30+
" [ 5100, 5925, 7875],\n",
31+
" [ 8250, 10500, 13500],\n",
32+
" [ 6000, 6375, 6750]])"
33+
]
34+
},
35+
"execution_count": 4,
36+
"metadata": {},
37+
"output_type": "execute_result"
38+
}
39+
],
40+
"source": [
41+
"revenues_usd = np.array([[200,220,250],[68,79,105],[110,140,180],[80,85,90]])\n",
42+
"revenues_inr = 75*revenues_usd\n",
43+
"revenues_inr"
44+
]
45+
},
46+
{
47+
"cell_type": "markdown",
48+
"metadata": {},
49+
"source": [
50+
"<h3 style='color:blue'>Solution 2: Calculate total flowers sale every month for divine flowers shop</h3>"
51+
]
52+
},
53+
{
54+
"cell_type": "code",
55+
"execution_count": 6,
56+
"metadata": {},
57+
"outputs": [],
58+
"source": [
59+
"units_sold = np.array([[50,60,25],[10,13,5],[40,70,52]])\n",
60+
"price_per_unit = np.array([20,30,15])"
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": 7,
66+
"metadata": {},
67+
"outputs": [
68+
{
69+
"data": {
70+
"text/plain": [
71+
"array([1900, 2640, 1430])"
72+
]
73+
},
74+
"execution_count": 7,
75+
"metadata": {},
76+
"output_type": "execute_result"
77+
}
78+
],
79+
"source": [
80+
"total_sales_amount = np.dot(price_per_unit,units_sold)\n",
81+
"total_sales_amount"
82+
]
83+
}
84+
],
85+
"metadata": {
86+
"kernelspec": {
87+
"display_name": "Python 3",
88+
"language": "python",
89+
"name": "python3"
90+
},
91+
"language_info": {
92+
"codemirror_mode": {
93+
"name": "ipython",
94+
"version": 3
95+
},
96+
"file_extension": ".py",
97+
"mimetype": "text/x-python",
98+
"name": "python",
99+
"nbconvert_exporter": "python",
100+
"pygments_lexer": "ipython3",
101+
"version": "3.7.3"
102+
}
103+
},
104+
"nbformat": 4,
105+
"nbformat_minor": 2
106+
}
43.4 KB
Loading

0 commit comments

Comments
 (0)
Please sign in to comment.