Num Py Notes
Num Py Notes
Eg :- [5 6 7 8]
One dimensional(vectors)
Two dimensional(matrices)
3. Shape/size: number of elements along each axis of it . Output in the form of tuple
Array Shape
[5 7 8] (3,)
[[5 7 8],[4 5 6]] (2,3) # ROW,COLS
[[5 7 8],[4 5 6],[1 2 3],[6 7 8]] (4,3)
Function used to calculate shape is shape
4. Item Size :- size of each elements in byte
Import numpy as np
X=np.array([5,7,8])
Print(x.itemsize) // output is 4
2. fromiter() :-used to create non numeric sequence type array however it can create any type of
array(creating an array using dictionary or string values)
Import numpy as np // here numpy is represented as np
y={3:”d”,4:”e”,5:”f”}
x=np.fromiter(y,dtype=np.int32) // list is converted to array
print(x) // output [3 4 5]
2. arrange() :- crate 2-d array using a sequence , used with reshape function
Import numpy as np
x=np.arrange(6).reshape(2,3)
print(x) // output [[0 1 2]
[3 4 5]]
3. empty():- This function is used to create empty array of specified size and mentioned dtype.
Syntax:
empty(size of array,[dtype], [order])
4. zeros() :- This function is used to create empty array with zero values of specified size and mentioned
dtype.
Import numpy as np
x=np.zeros([3,4],dtype=np.int32,order=”c”)
[ [0 0 0 0],
[0 0 0 0],
[0 0 0 0]]
5. ones () :- This function is used to create empty array with zero values of specified size and mentioned
dtype.
Import numpy as np
x=np.ones ([3,4],dtype=np.int32,order=”c”)
[ [1 1 1 1],
[1 1 1 1],
[1 1 1 1]]
6. zeros_like() :- creates an empty array same as size of already existing array with zero filled values
import numpy as np
a=np.array([[1,2,3],[4,5,6]])
b=np.zeros_like(a)
print(a)
print(b) // [[0 0 0 ],
[0 0 0]]
7. ones_like():-:- creates an empty array same as size of already existing array with ones filled values
10. random.rand() Create a 2-d array and filled random float values between 0 to 1
import numpy as np
a=np.random.rand(3,4)
// it will create a 3*4 array filled with floating random values
import numpy as np
a=np.random.rand(3,4)*100
// it will create a 3*4 array filled with floating random values between 0 to 100
11. random.randint() Create a 2-d array and filled random float values between 0 to 1
random.randint(start,end,size)
import numpy as np
a=np.random.randint(1,10,size=(2,3))
// it will create a 3*4 array filled with floating random values between 1 to 10
x=np.array([1,2,3,4,5,6,7,8])
x=np.array([5,6,6,7,8],[7,6,5,4,2],[5,6,4,3,2])
print(x[::3,::2]) //[5,6,8]
import numpy as np
a=np.array([1,2,3,4,5])
b=np.array([5,6,7,8,9])
c=np.hstack((a,b))
print(c)
x=np.array([5,6,6,7,8],[7,6,5,4,2]]
y=np.array([1,2,3,4,5],[6,7,8,9,9]]
z=np.hstack((x,y))
print(z)
output // first row on first two-d is concatenate with first row of first row of second two-d array and so on
[[5 6 6 7 8 1 2 3 4 5]
[7 6 5 4 2 6 7 8 9 9]]
x=np.array(([5,6,6,7,8],[7,6,5,4,2],[5,6,4,3,2]))
import numpy as np
ls1=[1,2,3]
ls2=[4,5,6]
ls3=[7,8,9]
ls4=[10,11,12]
x=np.array([ls1,ls2])
y=np.array([ls3,ls4])
a=np.vstack((x,y))
print(a)
output // first row on first two-d is concatenate with first row of first row of second two-d array and so on
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
3. concatenate():-
Axis-0 vstack()
Axis-1 hstack()
[[ 1 2 3] [[ 1 2 3 7 8 9]
[ 4 5 6] [ 4 5 6 10 11 12]]
[ 7 8 9]
[10 11 12]]
Axis=None
import numpy as np
ls1=[1,2,3]
ls2=[4,5,6]
ls3=[7,8,9]
ls4=[10,11,12]
x=np.array([ls1,ls2])
y=np.array([ls3,ls4])
a=np.concatenate((x,y),axis=None)
[ 1 2 3 4 4 5 6 4 7 8 9 10 11 12] outpur
Subset : part of array
Contiguous subset :- divide array without any condition
1. hsplit() : divide an array in equal parts horizontally(cols)
import numpy as np [array([[1, 2],
ls1=[1,2,3,4] [4, 5]]), array([[3, 4],
ls2=[4,5,6,4] [6, 4]])]
x=np.array([ls1,ls2])
y=np.hsplit(x,2)
Operations on array
1. Arithmetic operation
1) Addition +
2) Multiplication *
3) Subtraction –
4) Exponent **
5) Matrix multiplication @
6) Division /
7) Remainder %
import numpy as np [ 6 8 10 12]
x=np.array([1,2,3,4]) [-4 -4 -4 -4]
y=np.array([5,6,7,8]) [ 5 12 21 32]
print(x+y) [ 1 8 27 64]
print(x-y) [0.2 0.33333333 0.42857143 0.5 ]
print(x*y) [1 2 3 4]
print(x**3)
print(x/y)
print(x%y)
import numpy as np [[6 8]
x=np.array([[1,2],[4,5]]) [6 7]]
y=np.array([[5,6],[2,2]]) [[-4 -4]
print(x+y) [ 2 3]]
print(x-y) [[ 5 12]
print(x*y) [ 8 10]]
print(x@y) [[ 9 10]
print(x**3) [30 34]]
print(x/y) [[ 1 8]
print(x%y) [ 64 125]]
[[0.2 0.33333333]
[2. 2.5 ]]
[[1 2]
[0 1]]
2. Transpose :- convert rows into cols and cols into rows
import numpy as np [[1 4]
x=np.array([[1,2,3],[4,5,6]]) [2 5]
y=np.transpose(x) [3 6]]
print(y)
import numpy as np
studentdata = np.loadtxt('D:/NCERT/student.csv', skiprows=1, delimiter=',')
print(studentdata)
Output
[[ 1. 34. 44. 54.]
[ 2. 35. 45. 55.]
[ 3. 36. 46. 56.]
[ 4. 37. 47. 57.]
[ 5. 38. 48. 58.]]
import numpy as np
stud1, stud2, stud3, stud4,stud5 = np.loadtxt('D:/NCERT/student.csv', skiprows=1, delimiter=',')
print(stud1)
print(stud2)
print(stud3)
print(stud4)
print(stud5)
[ 1. 34. 44. 54.]
[ 2. 35. 45. 55.]
[ 3. 36. 46. 56.]
[ 4. 37. 47. 57.]
[ 5. 38. 48. 58.]
import numpy as np
stud1, stud2, stud3, stud4 = np.loadtxt('D:/NCERT/student.csv', skiprows=1,
delimiter=',',unpack=True)
print(stud1)
print(stud2)
print(stud3)
print(stud4)
[1. 2. 3. 4. 5.]
[34. 35. 36. 37. 38.]
[44. 45. 46. 47. 48.]
[54. 55. 56. 57. 58.]
Using NumPy.genfromtxt()
From this function if any blank value it is replaced by nan value
import numpy as np
studentdata = np.genfromtxt('D:/NCERT/student.csv', skip_header=1, delimiter=',')
print(studentdata)
[[ 1. 34. 44. 54.]
[ 2. 35. 45. nan]
[ 3. 36. 46. 56.]
[ 4. 37. 47. 57.]
[ 5. 38. 48. 58.]]
import numpy as np
studentdata = np.genfromtxt('D:/NCERT/student.csv', skip_header=1,filling_values=-999,
delimiter=',')
print(studentdata)
[[ 1. 34. 44. 54.]
[ 2. 35. 45. -999.]
[ 3. 36. 46. 56.]
[ 4. 37. 47. 57.]
[ 5. 38. 48. 58.]]