Created
February 15, 2019 12:27
-
-
Save Robofied/24513c0265e7906fca006a683de238be to your computer and use it in GitHub Desktop.
Numpy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Let us consider an example in which we need to calculate the % of calories from carb, protein, fat in different foods | |
## Each colum represent to new food like col1-> food1(apple), col2-> food2(orange) , col3-> food3(banana) =, col4->food4(mango) | |
## Rows represent of as-: row1->carb ,row2->protein ,row3->fat | |
import numpy as np | |
A =np.array([[56.0,0.0,4.4,68.0], | |
[1.2,104.0,52.0,8.0], | |
[1.8,135.0,99.0,0.9]]) | |
print(A) | |
#[Output]: | |
#[[ 56. 0. 4.4 68. ] | |
# [ 1.2 104. 52. 8. ] | |
# [ 1.8 135. 99. 0.9]] | |
## Matrix calculation to find out the | |
## axis=0 means columnwise | |
calc = A.sum(axis=0) | |
print(calc) | |
#[Output]: | |
#[ 59. 239. 155.4 76.9] | |
## Calculation of amount of percentage of carb, protein, fat present in food1, food2,food3,food4 | |
## This division is possible because of broadcasting | |
per = 100*A/calc | |
print(per) | |
#[Output]: | |
#[[94.91525424 0. 2.83140283 88.42652796] | |
# [ 2.03389831 43.51464435 33.46203346 10.40312094] | |
# [ 3.05084746 56.48535565 63.70656371 1.17035111]] | |
print("Food1 i.e, Apple has",per[:,0]) | |
#[Output]: | |
#Food1 i.e, Apple has [94.91525424 2.03389831 3.05084746] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment