0% found this document useful (0 votes)
5 views

vertopal.com_12_Numpy

Good

Uploaded by

chauhanjeel57
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

vertopal.com_12_Numpy

Good

Uploaded by

chauhanjeel57
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 17

NumPy => Numerical Python

Array => Array is a collection of element of same data type.


types of array => 1D , 2D , Multi-Dimentional

1D Array
import numpy as np

array_1 = np.array([1,2,3])
print(array_1)

[1 2 3]

import numpy as np

array_2 = np.array((1,2,3))
print(array_2)

[1 2 3]

import pandas as pd
print(pd.__version__)

2.2.3

import numpy as np
print(np.__version__)

2.2.0

2D Array
import numpy as np

array_3 = np.array([ [1,2,3], [4,5,6], [7,8,9] ])


print(array_3)

[[1 2 3]
[4 5 6]
[7 8 9]]

We can create array and Mention datatype inside that.


np.array([1,2,3,4,5], dtype="float")

array([1., 2., 3., 4., 5.])

np.array([1,2,3,4,5], dtype="complex")

array([1.+0.j, 2.+0.j, 3.+0.j, 4.+0.j, 5.+0.j])


How the NumPy Arrays are different from list ?????
import numpy as np

array1= np.array([1,2,3])
array2= np.array([30,40,50])

print(array1+array2)

[31 42 53]

# import numpy as np

list1 = [1,2,3]
list2 = [30,40,50]

list_ans=[]

for i,j in zip(list1,list2):


list_ans.append(i+j)

print(list_ans)

[31, 42, 53]

---> NumPy arrays have fixed size of array creation but it is not the case when we talk about
lists.
---> The element in the NumPy array are all required to be of same data type.
---> NumPy takes very less time to perform atithmetic activities between arrays and list
takes more time.
---> NumPy takes less space as compared to that of lists.

NumPy Array Method


how to check dimension of array??
import numpy as np

arr1 = np.array([1,2,3,4,50])

arr1.ndim

import numpy as np

arr2 = np.array([[1,2,3],[4,5,6],[7,8,9]])
arr2.ndim

how to check shape of array ???? [row,col]


import numpy as np

a1 = np.array([1,2,3,4,5])

a1.shape

(5,)

import numpy as np

a2 = np.array([[1,2,3],[4,5,6],[7,8,9]])

a2.shape

(3, 3)

how to check size of array ???? (number of element in array)


import numpy as np

a1 = np.array([1,2,3,4,5])

a1.size

import numpy as np

a2 = np.array([[1,2,3],[4,5,6],[7,8,9]])

a2.size

NumPy Array Attributes


we can create a array using arange() function
import numpy as np

array01 = np.arange(10)

print(array01)

[0 1 2 3 4 5 6 7 8 9]
import numpy as np

array02 = np.arange(12, dtype="float")

print(array02)

[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.]

How many bites occupide in array


int32 = 4 byte
int64 = 8 byte
float = 8 byte
import numpy as np

a1 = np.arange(10).astype(np.int32)

a1.itemsize

import numpy as np

a3 = np.arange(10).astype(np.int64)

a3.itemsize

import numpy as np

a5 = np.arange(12).astype(float)

a5.itemsize

We can convert one dimension (1D) to two dimension (2D)


before coverting 1D to 2D, we can find the find factor of numbers of element in array
ex = > array1 has 12 element then factor are : 3x4 4x3 6x2 2x6 12x1 1x12
import numpy as np

a1 = np.arange(12, dtype="float").reshape(4,3)

print(a1)
How check the Data Type of An Array
import numpy as np

a1 = np.arange(12)

a2 = np.arange(10, dtype=float)

print(a1.dtype)
print(a2.dtype)

int64
float64

Change the data type of Array


import numpy as np

a1 = np.arange(12, dtype="float").reshape(4,3)

a1.astype(np.int64)

array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])

How to fetch element from array using loops.


import numpy as np

a1 = np.arange(12, dtype="float").reshape(4,3)

for i in nditer(a1):
prin(i)

Create array element using zeros (0s)


import numpy as np

np.zeros(10)

array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])

Create array element using ones (1s)


import numpy as np

np.ones(10)

array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
Create array element using Random
import numpy as np

np.random.random(12).reshape(4,3)

array([[0.14276624, 0.28927688, 0.28024076],


[0.3108936 , 0.56279847, 0.09840232],
[0.18568831, 0.09469543, 0.98156396],
[0.1824776 , 0.80649768, 0.07999861]])

Array Operation
Scaler Operations Arithmetic
import numpy as np

a1 = np.arange(12).reshape(3,4)
a2 = np.arange(12,24).reshape(3,4)

print(a1)
print("\n")
print(a2)
print("\n")
#scaler operation
print(a1*2)
print("\n")
print(a2*3)

[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]

[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]

[[ 0 2 4 6]
[ 8 10 12 14]
[16 18 20 22]]

[[36 39 42 45]
[48 51 54 57]
[60 63 66 69]]
Vector Operation Artithmetic
import numpy as np

a1 = np.arange(12).reshape(3,4)
a2 = np.arange(12,24).reshape(3,4)

print(a1+a2)
print("\n")
print(a2-a1)

[[12 14 16 18]
[20 22 24 26]
[28 30 32 34]]

[[12 12 12 12]
[12 12 12 12]
[12 12 12 12]]

Array Function
round()
import numpy as np

a = np.random.random((3,3))
print(a)
print("\n")

b = np.round(a*100)
print(b)

[[0.09474992 0.84432135 0.42321112]


[0.6186932 0.9159806 0.15019438]
[0.13322751 0.4519615 0.85427585]]

[[ 9. 84. 42.]
[62. 92. 15.]
[13. 45. 85.]]

min( ) max( ) sum( ) prod( ) mean( ) std( ) var( ) sin( ) cos( )


import numpy as np

a = np.random.random((3,3))
b = np.round(a*100)
print(b)
print("min =",np.min(b))
print("max =",np.max(b))
print("sum =",np.sum(b))
print("prod =",np.prod(b))
print("mean =",np.mean(b))
print("mean of row =",np.mean(b, axis=1)) # row mean
print("mean of col =",np.mean(b, axis=0)) # column mean
print("std =",np.std(b))
print("var =",np.var(b))
print("sin =")
print(np.sin(b))
print("cos =")
print(np.cos(b))

[[92. 77. 93.]


[18. 54. 62.]
[81. 51. 60.]]
min = 18.0
max = 93.0
sum = 588.0
prod = 9840697928772480.0
mean = 65.33333333333333
mean of row = [87.33333333 44.66666667 64. ]
mean of col = [63.66666667 60.66666667 71.66666667]
std = 22.29100466306732
var = 496.8888888888889
sin =
[[-0.77946607 0.99952016 -0.94828214]
[-0.75098725 -0.55878905 -0.7391807 ]
[-0.62988799 0.67022918 -0.30481062]]
cos =
[[-0.62644445 -0.03097503 0.3174287 ]
[ 0.66031671 -0.82930983 0.67350716]
[ 0.77668598 0.7421542 -0.95241298]]

Dot Product dot()


import numpy as np

a = np.arange(1,13).reshape(3,4)

b = np.arange(12,24).reshape(4,3)

print(a,"\n", " X", "\n", b,"\n" " ||","\n",np.dot(a,b))

[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
X
[[12 13 14]
[15 16 17]
[18 19 20]
[21 22 23]]
||
[[180 190 200]
[444 470 496]
[708 750 792]]

log() and exp()


import numpy as np

a = np.arange(1,13).reshape(4,3)

print(np.log(a))
print("\n")
print(np.exp(a))

[[0. 0.69314718 1.09861229]


[1.38629436 1.60943791 1.79175947]
[1.94591015 2.07944154 2.19722458]
[2.30258509 2.39789527 2.48490665]]

[[2.71828183e+00 7.38905610e+00 2.00855369e+01]


[5.45981500e+01 1.48413159e+02 4.03428793e+02]
[1.09663316e+03 2.98095799e+03 8.10308393e+03]
[2.20264658e+04 5.98741417e+04 1.62754791e+05]]

round( ) floor( ) ceil( )


import numpy as np

a = np.random.random((3,3))*100
print(a)

print("round =")
print(np.round(a))

print("floor =")
print(np.floor(a))

print("ceil =")
print(np.ceil(a))

[[82.36702836 62.0011103 16.91152934]


[46.12722025 62.5656803 51.43431621]
[31.00480828 63.33271385 9.7530856 ]]
round =
[[82. 62. 17.]
[46. 63. 51.]
[31. 63. 10.]]
floor =
[[82. 62. 16.]
[46. 62. 51.]
[31. 63. 9.]]
ceil =
[[83. 63. 17.]
[47. 63. 52.]
[32. 64. 10.]]

Indexing And Slicing


In Matrix row star from 0 index and colum start from 0 index
1D array syntax = array[start:stop:step]
2D array syntax = array[row_start:row_stop:row_step , col_start:col_stop:col_step]
import numpy as np

# create a 1D array
array1 = np.array([1, 3, 5, 7, 8, 9, 2, 4, 6])

# slice array1 from index 2 to index 6 (exclusive)


print(array1[2:6]) # [5 7 8 9]

# slice array1 from index 0 to index 8 (exclusive) with a step size of


2
print(array1[0:8:2]) # [1 5 8 2]

# slice array1 from index 3 up to the last element


print(array1[3:]) # [7 8 9 2 4 6]

# items from start to end


print(array1[:]) # [1 3 5 7 8 9 2 4 6]

import numpy as np

# create a numpy array


numbers = np.array([2, 4, 6, 8, 10, 12])

# slice the last 3 elements of the array


# using the start parameter
print(numbers[-3:]) # [8 10 12]

# slice elements from 2nd-to-last to 4th-to-last element


# using the start and stop parameters
print(numbers[-5:-2]) # [4 6 8]

# slice every other element of the array from the end


# using the start, stop, and step parameters
print(numbers[-1::-2]) # [12 8 4]

import numpy as np

# create a 2D array
array1 = np.array([[1, 3, 5, 7],
[9, 11, 13, 15],
[2, 4, 6, 8]])

# slice the array to get the first two rows and columns
subarray1 = array1[:2, :2]

# slice the array to get the last two rows and columns
subarray2 = array1[1:3, 2:4]

# print the subarrays


print("First Two Rows and Columns: \n",subarray1)
print("Last two Rows and Columns: \n",subarray2)

First Two Rows and Columns:


[[ 1 3]
[ 9 11]]
Last two Rows and Columns:
[[13 15]
[ 6 8]]

import numpy as np

a = np.arange(10)
b = np.arange(12).reshape(3,4) #reshape(a,b)=> aXb matrix a = row
, b = column
c = np.arange(8).reshape(2,2,2) #reshape(a,b,c)=> a= number of
matrix , b= no of rows in matrix , c= no of col in matrix

print(b)

[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]

# lets find out 6 from b


print(b[1,2])

# lets find out 7 from b


print(b[1,3])

#lets find out 10 from b


print(b[2,2])
6
7
10

print(c)

[[[0 1]
[2 3]]

[[4 5]
[6 7]]]

#lets find out 7 from c


print(c[1,1,1]) # arr_name[index,row,col]

#lets find out 5 from c


print(c[1,0,1])

#lets find out 1 from c


print(c[0,0,1])

7
5
1

import numpy as np

a = np.arange(10)
b = np.arange(12).reshape(3,4) #reshape(a,b)=> aXb matrix a = row
, b = column
c = np.arange(8).reshape(2,2,2) #reshape(a,b,c)=> a= number of
matrix , b= no of rows in matrix , c= no of col in matrix

print(a)

[0 1 2 3 4 5 6 7 8 9]

#find out 3 to 7
print(a[3:8])

[3 4 5 6 7]

#find out 3 to 7, with interval 2


print(a[3:8:2])

[3 5 7]

print(b)

[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Iterating
import numpy as np

a = np.arange(10)
b = np.arange(12).reshape(3,4)
c = np.arange(27).reshape(3,3,3)

for i in a:
print(i)

0
1
2
3
4
5
6
7
8
9

for i in b:
print(b)

[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]

for i in c:
print(c)

[[[ 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]]]
[[[ 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]]]
[[[ 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]]]

Printing indivisual element fro 2D and 3D array


for i in np.nditer(b):
print(i)

0
1
2
3
4
5
6
7
8
9
10
11

for i in np.nditer(c):
print(i)

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

Transpose Array
import numpy as np

a = np.arange(12).reshape(3,4)
print(a)
print("\n")
print(np.transpose(a))

[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]

[[ 0 4 8]
[ 1 5 9]
[ 2 6 10]
[ 3 7 11]]

Conver any dimension array to 1D array


import numpy as np

a = np.arange(12).reshape(3,4)
print(a)
print("\n")
print(np.ravel(a))

[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]

[ 0 1 2 3 4 5 6 7 8 9 10 11]
Stacking
it is used to joining multiple arrays.it joins array along a new axis.
import numpy as np

a = np.arange(12).reshape(3,4)
b = np.arange(12,24).reshape(3,4)

horizontal stacking
np.hstack((a,b))

array([[ 0, 1, 2, 3, 0, 1, 2, 3],
[ 4, 5, 6, 7, 4, 5, 6, 7],
[ 8, 9, 10, 11, 8, 9, 10, 11]])

vertical stacking
np.vstack((a,b))

array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

Splitting
Cutting should be equal position
import numpy as np

a = np.arange(12).reshape(3,4)

# horizontally cutting 2 part


np.hsplit(a,2)

[array([[0, 1],
[4, 5],
[8, 9]]),
array([[ 2, 3],
[ 6, 7],
[10, 11]])]

#horizontally cutting 4 part


np.hsplit(a,4)

[array([[0],
[4],
[8]]),
array([[1],
[5],
[9]]),
array([[ 2],
[ 6],
[10]]),
array([[ 3],
[ 7],
[11]])]

#vertically cutting 3 part


np.vsplit(a,3)

[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10,


11]])]

Find Maximum value over Index In Data Frame

You might also like