0% found this document useful (0 votes)
16 views7 pages

Class 5 NumPy

Uploaded by

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

Class 5 NumPy

Uploaded by

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

9/28/24, 3:08 PM Class 5 NumPy - Jupyter Notebook

Python NumPy

NumPy
NumPy, which stands for Numerical Python, is an open-source Python library
consisting of multidimensional and single-dimensional array elements.
It's a standard that computes numerical data in Python.
NumPy is most widely used in almost every domain where numerical computation is
required, like science and engineering

In [1]: # Importing NumPy Array


import numpy as np

Creating Arrays

NumPy arrays are more efficient than Python lists for numerical operations due to their fixed
size and homogeneous data type.

1D Array (Vector):

In [2]: arr1d = np.array([1, 2, 3, 4, 5])


print(arr1d)

[1 2 3 4 5]

In [3]: list1=[1,2,3,4]
arr1=np.array(list1) # convert the list into array
print(arr1)

[1 2 3 4]

In [5]: print(type(arr1)) #get type of the array


print(type(arr1d))

<class 'numpy.ndarray'>
<class 'numpy.ndarray'>

In [6]: type(arr1)

Out[6]: numpy.ndarray

In [7]: print(arr1*2)

[2 4 6 8]

localhost:8889/notebooks/Python/Python/Class 5 NumPy.ipynb 1/7


9/28/24, 3:08 PM Class 5 NumPy - Jupyter Notebook

In [8]: print(arr1.shape) #give the no. of row and no . of column

(4,)

In [9]: print(arr1.size) #give the total no. of element in the array

In [10]: print(arr1.dtype) # give the type of element and bit size

int32

In [11]: arr2=np.array([1,2,3,4],np.int64) #to change the data type and bit size
print(arr2.dtype)

int64

In [12]: arr3=np.array([1,2,3,4],np.float32)
print(arr3.dtype)
print(arr3)

float32
[1. 2. 3. 4.]

In [13]: arr4=np.zeros((5)) #give an array of given size consisting 0 only


print(arr4)

[0. 0. 0. 0. 0.]

In [14]: arr5=np.ones((5))
print(arr5)

[1. 1. 1. 1. 1.]

In [15]: print(arr1.ndim) # gives the dimension of the array

2D Array (Matrix):

In [16]: l1=[1,2,3]
l2=[4,5,6]
l3=[7,8,9]
arr6=np.array([l1,l2,l3])
print(arr6)

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

In [17]: type(arr6)

Out[17]: numpy.ndarray

localhost:8889/notebooks/Python/Python/Class 5 NumPy.ipynb 2/7


9/28/24, 3:08 PM Class 5 NumPy - Jupyter Notebook

In [18]: print(arr6.shape) #give the matrix size

(3, 3)

In [19]: print(arr6.size) #give the total element in the matrix

In [20]: print(arr6.dtype) # give the type of element and bit size

int32

In [21]: print(l1+l2)

[1, 2, 3, 4, 5, 6]

In [22]: np1=np.array(l1)
np2=np.array(l2)
print(np1+np2)

[5 7 9]

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

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

In [25]: print(a2.shape)
print(a2.size)

(3, 3)
9

In [26]: print(a2.ndim) # gives the dimension of the array

3D Array (Tensor):

In [27]: arr3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr3d)

[[[1 2]
[3 4]]

[[5 6]
[7 8]]]

localhost:8889/notebooks/Python/Python/Class 5 NumPy.ipynb 3/7


9/28/24, 3:08 PM Class 5 NumPy - Jupyter Notebook

In [28]: a3=np.array([[[1,2,3],[2,3,4]],[[4,5,6],[6,5,4]],[[7,8,5],[2,4,5]]])
print(a3)

[[[1 2 3]
[2 3 4]]

[[4 5 6]
[6 5 4]]

[[7 8 5]
[2 4 5]]]

In [30]: print(a3.shape)
print(a3.size)

(3, 2, 3)
18

In [31]: print(a3.ndim) # gives the dimension of the array

NumPy - Indexing & Slicing


In [32]: a = np.arange(10)

In [33]: a

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

In [34]: s = slice(2,7,2)

In [35]: a[s]

Out[35]: array([2, 4, 6])

In [36]: b = a[2:9:2]

In [37]: print(b)

[2 4 6 8]

In [38]: c = a[5]
print(c)

In [39]: print(a[2:])

[2 3 4 5 6 7 8 9]

localhost:8889/notebooks/Python/Python/Class 5 NumPy.ipynb 4/7


9/28/24, 3:08 PM Class 5 NumPy - Jupyter Notebook

In [40]: print(a[2:5])

[2 3 4]

In [41]: a = np.array([[1,2,3],[3,4,5],[4,5,6]])
print(a)

[[1 2 3]
[3 4 5]
[4 5 6]]

In [43]: print(a[:])

[[1 2 3]
[3 4 5]
[4 5 6]]

In [44]: print(a[:4])

[[1 2 3]
[3 4 5]
[4 5 6]]

In [45]: print(a[4:])

[]

In [46]: print(a[1:4])

[[3 4 5]
[4 5 6]]

In [47]: print(a[2:])

[[4 5 6]]

In [48]: x = np.array([[ 0, 1, 2],[ 3, 4, 5],[ 6, 7, 8],[ 9, 10, 11]])

In [51]: print(x)

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

In [52]: print(x[:,:])

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

localhost:8889/notebooks/Python/Python/Class 5 NumPy.ipynb 5/7


9/28/24, 3:08 PM Class 5 NumPy - Jupyter Notebook

In [53]: print(x[1:,:])

[[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]

In [54]: print(x[1:3,:])

[[3 4 5]
[6 7 8]]

In [56]: print(x[:,1:3])

[[ 1 2]
[ 4 5]
[ 7 8]
[10 11]]

In [58]: print(x[1:3,1:])

[[4 5]
[7 8]]

In [59]: print(x[1:3,1:3])

[[4 5]
[7 8]]

In [61]: print(x[2:,1:2])

[[ 7]
[10]]

In [67]: a1=np.arange(32)
print(a1)
a1.ndim

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

Out[67]: 1

In [69]: a2=a1.reshape(8,4)
a2

Out[69]: array([[ 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]])

localhost:8889/notebooks/Python/Python/Class 5 NumPy.ipynb 6/7


9/28/24, 3:08 PM Class 5 NumPy - Jupyter Notebook

In [70]: a2.ndim

Out[70]: 2

In [72]: a3 = a1.reshape(2,4,4)
a3

Out[72]: array([[[ 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]]])

In [73]: a3.ndim

Out[73]: 3

In [75]: ar4=np.random.randint(10,20,5) # give 5 random element between 10 to 20


print(ar4)

[10 15 17 13 14]

In [76]: ar5=np.random.randn(5,3) # gives a matrix of given size concist of random e


print(ar5)

[[-0.60554537 0.82270419 -0.68292731]


[ 1.11267969 0.57879301 0.43384949]
[ 0.09422181 2.21999736 0.35297232]
[-0.45893539 -1.28833069 -2.15839099]
[ 0.06352359 -1.27736041 0.12127969]]

In [77]: ar6=np.random.random_sample((5,3)) # gives a matrix of given size concist o


print(ar6)

[[0.51779312 0.15548194 0.31707927]


[0.54605588 0.09933863 0.67800372]
[0.77162612 0.84544265 0.56366543]
[0.02496134 0.50177791 0.21819764]
[0.57773426 0.47091659 0.39079135]]

localhost:8889/notebooks/Python/Python/Class 5 NumPy.ipynb 7/7

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy