Skip to content

Instantly share code, notes, and snippets.

import numpy as np
a = np.arange(6).reshape((3,2))
print(a)
#[Output]:
#array([[0, 1],
# [2, 3],
# [4, 5]])
print(np.cos(np.deg2rad(np.array((0.,30.,45.)))))
#[Output]:
#array([1. , 0.8660254 , 0.70710678])
import numpy as np
## 1. np.sin()
print(np.sin(np.pi/2.))
#[Output]:
#1.0
"""In this we are taking an array of angles in degree and calculating the sine of that, so we are converting
them to radians first"""
## 1. moveaxis routine
import numpy as np
x = np.zeros((3, 4, 5))
## In this all the elements shape gets shifted in the direction of source to destonation, so for this example
## it is just like cyclic rotation in clockwise direction.
print(np.moveaxis(x, 0, -1).shape)
#[Output]:
import numpy as np
a = np.arange(6)
print(a)
#[Output]:
#[0 1 2 3 4 5]
## 1. Reshaping
a.reshape(2,3)
## Here order doesn't matter
a = np.arange(6).reshape(2,3)
for x in np.nditer(a, order='F'):
print (x,end=",")
#[Output]:
#0,3,1,4,2,5,
import numpy as np
a = np.arange(6).reshape(2,3)
for x in np.nditer(a):
print (x,end=",")
#[Output]:
#0,1,2,3,4,5,
## 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)
import numpy as np
x = np.arange(35)
print(x)
#[Output]:
#[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 24 25 26 27 28 29 30 31 32 33 34]
import numpy as np
x = np.arange(10,1,-1)
print(x)
#[Output]:
#[10 9 8 7 6 5 4 3 2]
print(x[np.array([3,3,4,7])])