0% found this document useful (0 votes)
83 views16 pages

Num Py

NumPy is a Python library used for working with multidimensional arrays and matrices. It contains functions for linear algebra, Fourier transforms, and random number generation. NumPy arrays are fast and memory efficient for numerical processing. NumPy allows users to create arrays of multiple dimensions and provides functions to easily perform operations on these arrays like addition, multiplication etc.

Uploaded by

Surabhi Kumari
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)
83 views16 pages

Num Py

NumPy is a Python library used for working with multidimensional arrays and matrices. It contains functions for linear algebra, Fourier transforms, and random number generation. NumPy arrays are fast and memory efficient for numerical processing. NumPy allows users to create arrays of multiple dimensions and provides functions to easily perform operations on these arrays like addition, multiplication etc.

Uploaded by

Surabhi Kumari
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/ 16

4/17/23, 1:25 PM numpy documents - Jupyter Notebook

In [ ]: ​

1. What is NumPy? Why should we use it?

Ans:- NumPy is a Python library.

NumPy is used for working with arrays.

NumPy is short for "Numerical Python".

2. What is NumPy?
Ans: NumPy is a Python library used for working with arrays.

It also has functions for working in domain of linear algebra, fourier transform, and matrices.

NumPy was created in 2005 by Travis Oliphant. It is an open source project and you can use it freely.

NumPy stands for Numerical Python.

Why Use NumPy?


Ans: In Python we have lists that serve the purpose of arrays, but they are slow to process.

NumPy aims to provide an array object that is up to 50x faster than traditional Python lists.

The array object in NumPy is called ndarray, it provides a lot of supporting functions that make working with
ndarray very easy.

Arrays are very frequently used in data science, where speed and resources are very important.

In [1]: # Import numpy library.

In [2]: import numpy as np # NumPy is usually imported under the np alias.

localhost:8888/notebooks/Downloads/numpy documents.ipynb# 1/16


4/17/23, 1:25 PM numpy documents - Jupyter Notebook

In [147]: # check the version of numpy...

In [146]: import numpy as np


#The version string is stored under __version__ attribute.
print(np.__version__)

1.23.5

NumPy Creating Arrays

how we can use array in numpy?

In [148]: #NumPy is used to work with arrays. The array object in NumPy is called ndarray.

# We can create a NumPy ndarray object by using the array() function.

. One-Dimensional array...............

In [174]: import numpy as np

arr = [1,2,3,4] #python list


numpy_arr = np.array(arr) #numpy arrary
print(numpy_arr)

[1 2 3 4]

. Two-Dimensional array...............

In [175]: import numpy as np



arr = [[1,2,3,4],[4,5,6,7]]
numpy_arr = np.array(arr)

In [176]: print(numpy_arr)

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

. Three-Dimensional array.................

In [177]: import numpy as np



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

In [178]: print(numpy_arr)

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

localhost:8888/notebooks/Downloads/numpy documents.ipynb# 2/16


4/17/23, 1:25 PM numpy documents - Jupyter Notebook

In [4]: np.array([2,3,4,5,6]) # this is numpy based array.

Out[4]: array([2, 3, 4, 5, 6])

In [5]: type(np.array([2,3,4,5,6]))

Out[5]: numpy.ndarray

In [6]: # ndarray means n dimensional array.

In [7]: np.array([[2,3,4],[4,5,6]]) # this is called 2 dimensional array.

Out[7]: array([[2, 3, 4],


[4, 5, 6]])

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

Out[8]: array([[1, 2, 3, 4],


[2, 3, 4, 5]])

In [9]: a = np.array([3,4,5,6],ndmin=5)

In [10]: a # ndmin = 5 means we have 5 different layer this array.

Out[10]: array([[[[[3, 4, 5, 6]]]]])

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

In [12]: arr # It's 2d array .

Out[12]: array([[1, 2, 3],


[4, 5, 6],
[7, 8, 9]])

In [13]: # why we call it's 2d array?....

In [14]: arr.ndim # ndim is function that's give you number.

Out[14]: 2

In [15]: # so array and matrix is same matrix are a sub classes of array

In [16]: # how will be create a 3 D array ?

In [17]: np.random.randint(1,10,(4,4)) # random.randint this is function inside numpy and this functi

Out[17]: array([[2, 5, 6, 6],


[4, 3, 4, 3],
[6, 3, 2, 8],
[4, 6, 7, 7]])

In [18]: # above dataset we genrate data 1 to 10 range and 4,4 means 4 row and 4 columns.

localhost:8888/notebooks/Downloads/numpy documents.ipynb# 3/16


4/17/23, 1:25 PM numpy documents - Jupyter Notebook

In [19]: np.random.randint(1,10,(4,4,2)) # here we have 4 x 2 (4 time) array .

Out[19]: array([[[5, 6],


[9, 3],
[8, 4],
[8, 3]],

[[2, 9],
[3, 2],
[3, 4],
[3, 9]],

[[6, 7],
[3, 3],
[3, 7],
[7, 8]],

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

In [20]: # let's we have a list

In [21]: l = [1,2,3,4,5,6]

In [22]: np.array(l) # its gives as at it same list.

Out[22]: array([1, 2, 3, 4, 5, 6])

In [23]: # another mix list

In [24]: l1 = [4,5,6,7,"himu",45.556,True]

In [25]: np.array(l1)

Out[25]: array(['4', '5', '6', '7', 'himu', '45.556', 'True'], dtype='<U32')

In [26]: # l1 list every elements is converted in string format its difference between l & l1 list.

In [27]: np.array([[1,2,3] , [4,5,6]])

Out[27]: array([[1, 2, 3],


[4, 5, 6]])

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

Out[28]: array([[[1, 2, 3],


[4, 5, 6],
[7, 8, 9]]])

In [29]: a1 = np.array(l1)

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

localhost:8888/notebooks/Downloads/numpy documents.ipynb# 4/16


4/17/23, 1:25 PM numpy documents - Jupyter Notebook

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

In [32]: a1

Out[32]: array(['4', '5', '6', '7', 'himu', '45.556', 'True'], dtype='<U32')

In [33]: a2

Out[33]: array([[1, 2, 3],


[4, 5, 6]])

In [34]: a3

Out[34]: array([[[1, 2, 3],


[4, 5, 6],
[7, 8, 9]]])

In [35]: # I want know above every array what's the dimension of thsese array

In [36]: a1.ndim # 1 dimensional array

Out[36]: 1

In [37]: a2.ndim # 2 dimensional array

Out[37]: 2

In [38]: a3.ndim # 3dimensional array

Out[38]: 3

In [39]: # check the size of these array...

In [40]: a1.size

Out[40]: 7

In [41]: a2.size

Out[41]: 6

In [42]: a3.size

Out[42]: 9

In [43]: # size always gives you total number of element.

In [44]: # check the shape ....

In [45]: a1.shape

Out[45]: (7,)

In [46]: # what's these shape is represent ?

localhost:8888/notebooks/Downloads/numpy documents.ipynb# 5/16


4/17/23, 1:25 PM numpy documents - Jupyter Notebook

In [47]: a2.shape

Out[47]: (2, 3)

In [48]: # (2, 3) It means we have 2 row 3 columns.

In [49]: a3.shape

Out[49]: (1, 3, 3)

In [50]: # (1, 3, 3) It's means we have 3x3 & 1 kind of matrixes.

In [51]: # print all the array ...

In [52]: a1

Out[52]: array(['4', '5', '6', '7', 'himu', '45.556', 'True'], dtype='<U32')

In [53]: a2

Out[53]: array([[1, 2, 3],


[4, 5, 6]])

In [54]: a3

Out[54]: array([[[1, 2, 3],


[4, 5, 6],
[7, 8, 9]]])

lets generate random dataset...

In [56]: np.random.randint(2,50) # we generate range 2 to 50

Out[56]: 23

In [57]: np.random.randint(2,50,(3,4))

Out[57]: array([[35, 27, 18, 22],


[43, 4, 22, 7],
[37, 15, 41, 46]])

In [58]: # range 2 to 50 and 3 row and 4 columns this above dataset .

In [59]: np.random.randint(2,50,(2,3,4))

Out[59]: array([[[40, 33, 47, 3],


[18, 37, 3, 22],
[29, 43, 24, 29]],

[[35, 18, 27, 27],


[16, 30, 31, 44],
[14, 32, 16, 28]]])

In [60]: np.random.rand(2,3)

Out[60]: array([[0.86746188, 0.1407559 , 0.75679224],


[0.25625481, 0.35165797, 0.70507107]])

localhost:8888/notebooks/Downloads/numpy documents.ipynb# 6/16


4/17/23, 1:25 PM numpy documents - Jupyter Notebook

In [61]: # why we generatiing a rondom data?

In [62]: np.random.rand(5,4)

Out[62]: array([[0.52299497, 0.88050136, 0.16676958, 0.90594708],


[0.28314699, 0.85363305, 0.75093054, 0.43450265],
[0.73321838, 0.82296852, 0.67109128, 0.80760906],
[0.47406926, 0.27954786, 0.93558001, 0.45000771],
[0.62244765, 0.40310844, 0.2546776 , 0.03386292]])

In [63]: np.random.rand(4,4)

Out[63]: array([[0.79714615, 0.82052295, 0.45399902, 0.54647648],


[0.77467119, 0.06934046, 0.59787413, 0.2732265 ],
[0.25455033, 0.02551532, 0.1341774 , 0.84606972],
[0.80742428, 0.33104106, 0.82293238, 0.91120843]])

reshape the dataset....

In [65]: a4 = np.random.rand(4,4)

In [66]: a4

Out[66]: array([[0.77112249, 0.67106395, 0.98797863, 0.32726612],


[0.85314391, 0.8932602 , 0.71487914, 0.77116107],
[0.34928874, 0.03327652, 0.45266342, 0.96730277],
[0.04162909, 0.32011503, 0.17987273, 0.76973354]])

In [67]: a4.reshape(2,8)

Out[67]: array([[0.77112249, 0.67106395, 0.98797863, 0.32726612, 0.85314391,


0.8932602 , 0.71487914, 0.77116107],
[0.34928874, 0.03327652, 0.45266342, 0.96730277, 0.04162909,
0.32011503, 0.17987273, 0.76973354]])

In [68]: a4.reshape(8,2)

Out[68]: array([[0.77112249, 0.67106395],


[0.98797863, 0.32726612],
[0.85314391, 0.8932602 ],
[0.71487914, 0.77116107],
[0.34928874, 0.03327652],
[0.45266342, 0.96730277],
[0.04162909, 0.32011503],
[0.17987273, 0.76973354]])

localhost:8888/notebooks/Downloads/numpy documents.ipynb# 7/16


4/17/23, 1:25 PM numpy documents - Jupyter Notebook

In [69]: a4.reshape(16,1)

Out[69]: array([[0.77112249],
[0.67106395],
[0.98797863],
[0.32726612],
[0.85314391],
[0.8932602 ],
[0.71487914],
[0.77116107],
[0.34928874],
[0.03327652],
[0.45266342],
[0.96730277],
[0.04162909],
[0.32011503],
[0.17987273],
[0.76973354]])

In [70]: a4.reshape(2,-232435454)

Out[70]: array([[0.77112249, 0.67106395, 0.98797863, 0.32726612, 0.85314391,


0.8932602 , 0.71487914, 0.77116107],
[0.34928874, 0.03327652, 0.45266342, 0.96730277, 0.04162909,
0.32011503, 0.17987273, 0.76973354]])

In [71]: a4.reshape(2,2,4) # reshape is 2 dimensional array.

Out[71]: array([[[0.77112249, 0.67106395, 0.98797863, 0.32726612],


[0.85314391, 0.8932602 , 0.71487914, 0.77116107]],

[[0.34928874, 0.03327652, 0.45266342, 0.96730277],


[0.04162909, 0.32011503, 0.17987273, 0.76973354]]])

In [72]: a4.reshape(1,1,1,1,1,1,1,2,2,4)

Out[72]: array([[[[[[[[[[0.77112249, 0.67106395, 0.98797863, 0.32726612],


[0.85314391, 0.8932602 , 0.71487914, 0.77116107]],

[[0.34928874, 0.03327652, 0.45266342, 0.96730277],


[0.04162909, 0.32011503, 0.17987273, 0.76973354]]]]]]]]]])

In [73]: # lets we an array...

In [74]: a1

Out[74]: array(['4', '5', '6', '7', 'himu', '45.556', 'True'], dtype='<U32')

In [75]: # extract the value..

In [76]: a1[0]

Out[76]: '4'

In [77]: a1[2:6] # here we do a slicing operation just same as list.

Out[77]: array(['6', '7', 'himu', '45.556'], dtype='<U32')

localhost:8888/notebooks/Downloads/numpy documents.ipynb# 8/16


4/17/23, 1:25 PM numpy documents - Jupyter Notebook

In [78]: a1[2:6:2] # means start from 2 to 6 and take a jump 2.

Out[78]: array(['6', 'himu'], dtype='<U32')

In [79]: # reverse the entire array...

In [80]: a1[::-1]

Out[80]: array(['True', '45.556', 'himu', '7', '6', '5', '4'], dtype='<U32')

In [81]: # another array ...

In [82]: a3

Out[82]: array([[[1, 2, 3],


[4, 5, 6],
[7, 8, 9]]])

In [83]: a2 #this is matrixes

Out[83]: array([[1, 2, 3],


[4, 5, 6]])

In [84]: # extract 1, 2, 3

In [85]: a2[0]

Out[85]: array([1, 2, 3])

In [86]: a2[:,[1,2]]

Out[86]: array([[2, 3],


[5, 6]])

In [87]: a2[[0,1],1:]

Out[87]: array([[2, 3],


[5, 6]])

In [88]: a5 = np.random.randint(2,90 , (6,5))

In [89]: a5

Out[89]: array([[65, 4, 44, 7, 89],


[86, 18, 43, 81, 6],
[20, 36, 61, 64, 5],
[62, 83, 82, 12, 84],
[21, 84, 48, 33, 78],
[26, 76, 10, 88, 70]])

In [90]: ## Give me those record which value is above 40 ?

In [91]: a5[a5>40]

Out[91]: array([65, 44, 89, 86, 43, 81, 61, 64, 62, 83, 82, 84, 84, 48, 78, 76, 88,
70])

localhost:8888/notebooks/Downloads/numpy documents.ipynb# 9/16


4/17/23, 1:25 PM numpy documents - Jupyter Notebook

In [92]: a5

Out[92]: array([[65, 4, 44, 7, 89],


[86, 18, 43, 81, 6],
[20, 36, 61, 64, 5],
[62, 83, 82, 12, 84],
[21, 84, 48, 33, 78],
[26, 76, 10, 88, 70]])

In [93]: ## reassigment operations

In [94]: ## change the vlues where are 3?

In [95]: a5[0,1] = 100

In [96]: a5

Out[96]: array([[ 65, 100, 44, 7, 89],


[ 86, 18, 43, 81, 6],
[ 20, 36, 61, 64, 5],
[ 62, 83, 82, 12, 84],
[ 21, 84, 48, 33, 78],
[ 26, 76, 10, 88, 70]])

In [97]: a6 = np.random.randint(0,3,(3,3))

In [98]: a7 = np.random.randint(0,3,(3,3))

In [99]: a6

Out[99]: array([[2, 2, 2],


[2, 2, 2],
[0, 1, 2]])

In [100]: a7

Out[100]: array([[0, 2, 2],


[2, 2, 2],
[1, 1, 0]])

In [101]: # multipliction

In [102]: a6*a7 # this is element wise multipliction this is not matrix multiplication at all.

Out[102]: array([[0, 4, 4],


[4, 4, 4],
[0, 1, 0]])

In [103]: # addition

In [104]: a6+a7 # this is element sequences wise addition this is not matrix addition at all.

Out[104]: array([[2, 4, 4],


[4, 4, 4],
[1, 2, 2]])

localhost:8888/notebooks/Downloads/numpy documents.ipynb# 10/16


4/17/23, 1:25 PM numpy documents - Jupyter Notebook

In [105]: # subtraction

In [106]: a6-a7 # this is element sequences wise subtraction this is not matrix subtraction at all.

Out[106]: array([[ 2, 0, 0],


[ 0, 0, 0],
[-1, 0, 2]])

In [107]: ### How to do matrix multiplication ?

In [108]: a6@a7 #using @ simble its giving you matrix multiplication.

Out[108]: array([[ 6, 10, 8],


[ 6, 10, 8],
[ 4, 4, 2]])

In [109]: a6 +100 # this is add 100 each an every a6 matrix elements.


Out[109]: array([[102, 102, 102],


[102, 102, 102],
[100, 101, 102]])

In [110]: a6*2 # a6 multipication by 2

Out[110]: array([[4, 4, 4],


[4, 4, 4],
[0, 2, 4]])

In [111]: a6/0 # It's just giving you a warning.

C:\Users\Balodi\AppData\Local\Temp\ipykernel_6408\3042893950.py:1: RuntimeWarning: divide


by zero encountered in divide
a6/0 # It's just giving you a warning.
C:\Users\Balodi\AppData\Local\Temp\ipykernel_6408\3042893950.py:1: RuntimeWarning: invalid
value encountered in divide
a6/0 # It's just giving you a warning.

Out[111]: array([[inf, inf, inf],


[inf, inf, inf],
[nan, inf, inf]])

In [112]: # let's I have an element


a6

Out[112]: array([[2, 2, 2],


[2, 2, 2],
[0, 1, 2]])

In [113]: # I want to be gives power of every element?

In [114]: a6**3

Out[114]: array([[8, 8, 8],


[8, 8, 8],
[0, 1, 8]], dtype=int32)

localhost:8888/notebooks/Downloads/numpy documents.ipynb# 11/16


4/17/23, 1:25 PM numpy documents - Jupyter Notebook

In [115]: a8 = np.zeros((4,4)) # zeros is function inside the numpy its giving everything is zero.

In [116]: a8

Out[116]: array([[0., 0., 0., 0.],


[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])

In [117]: a9 = np.ones((4,5)) # ones means all the elements gives one (1).

In [118]: a9

Out[118]: array([[1., 1., 1., 1., 1.],


[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])

In [119]: a9+10 # every elements is add 10.

Out[119]: array([[11., 11., 11., 11., 11.],


[11., 11., 11., 11., 11.],
[11., 11., 11., 11., 11.],
[11., 11., 11., 11., 11.]])

In [120]: ## let's do the row by row addition operations

In [121]: a9

Out[121]: array([[1., 1., 1., 1., 1.],


[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])

In [122]: a9 + np.array([1,2,3,4,5]) # here its did the row by row operation is added.

Out[122]: array([[2., 3., 4., 5., 6.],


[2., 3., 4., 5., 6.],
[2., 3., 4., 5., 6.],
[2., 3., 4., 5., 6.]])

In [123]: # this above case called is Broadcast operation.

In [124]: ## let's do the columns wise broadcast operation......

In [125]: np.array([[1,2,3,4]]).T * a9 #this is do the broadcasting operation columns wise.

Out[125]: array([[1., 1., 1., 1., 1.],


[2., 2., 2., 2., 2.],
[3., 3., 3., 3., 3.],
[4., 4., 4., 4., 4.]])

localhost:8888/notebooks/Downloads/numpy documents.ipynb# 12/16


4/17/23, 1:25 PM numpy documents - Jupyter Notebook

In [126]: # T is transport with trasport it looks...


np.array([[1,2,3,4]]).T

Out[126]: array([[1],
[2],
[3],
[4]])

In [127]: np.array([[1,2,3,4]]) # without transport

Out[127]: array([[1, 2, 3, 4]])

In [128]: a6

Out[128]: array([[2, 2, 2],


[2, 2, 2],
[0, 1, 2]])

In [129]: a6.T # here it just roated the Transport operation.

Out[129]: array([[2, 2, 0],


[2, 2, 1],
[2, 2, 2]])

In [130]: # square root of the elements...

In [131]: np.sqrt(a5)

Out[131]: array([[ 8.06225775, 10. , 6.63324958, 2.64575131, 9.43398113],


[ 9.2736185 , 4.24264069, 6.55743852, 9. , 2.44948974],
[ 4.47213595, 6. , 7.81024968, 8. , 2.23606798],
[ 7.87400787, 9.11043358, 9.05538514, 3.46410162, 9.16515139],
[ 4.58257569, 9.16515139, 6.92820323, 5.74456265, 8.83176087],
[ 5.09901951, 8.71779789, 3.16227766, 9.38083152, 8.36660027]])

In [133]: # expo...
np.exp(a5)

Out[133]: array([[1.69488924e+28, 2.68811714e+43, 1.28516001e+19, 1.09663316e+03,


4.48961282e+38],
[2.23524660e+37, 6.56599691e+07, 4.72783947e+18, 1.50609731e+35,
4.03428793e+02],
[4.85165195e+08, 4.31123155e+15, 3.10429794e+26, 6.23514908e+27,
1.48413159e+02],
[8.43835667e+26, 1.11286375e+36, 4.09399696e+35, 1.62754791e+05,
3.02507732e+36],
[1.31881573e+09, 3.02507732e+36, 7.01673591e+20, 2.14643580e+14,
7.49841700e+33],
[1.95729609e+11, 1.01480039e+33, 2.20264658e+04, 1.65163625e+38,
2.51543867e+30]])

In [134]: #log...
np.log10(a5)

Out[134]: array([[1.81291336, 2. , 1.64345268, 0.84509804, 1.94939001],


[1.93449845, 1.25527251, 1.63346846, 1.90848502, 0.77815125],
[1.30103 , 1.5563025 , 1.78532984, 1.80617997, 0.69897 ],
[1.79239169, 1.91907809, 1.91381385, 1.07918125, 1.92427929],
[1.32221929, 1.92427929, 1.68124124, 1.51851394, 1.8920946 ],
[1.41497335, 1.88081359, 1. , 1.94448267, 1.84509804]])

localhost:8888/notebooks/Downloads/numpy documents.ipynb# 13/16


4/17/23, 1:25 PM numpy documents - Jupyter Notebook

In [ ]: # know check the difference between range and arange function in numpy....

In [136]: list(range(0,10,2))

Out[136]: [0, 2, 4, 6, 8]

In [138]: np.arange(1.0,30.7,2.5) # Its arange function extract difference a floating number...

Out[138]: array([ 1. , 3.5, 6. , 8.5, 11. , 13.5, 16. , 18.5, 21. , 23.5, 26. ,
28.5])

In [140]: # linespace function...

In [141]: np.linspace(2,3,num=50)

Out[141]: array([2. , 2.02040816, 2.04081633, 2.06122449, 2.08163265,


2.10204082, 2.12244898, 2.14285714, 2.16326531, 2.18367347,
2.20408163, 2.2244898 , 2.24489796, 2.26530612, 2.28571429,
2.30612245, 2.32653061, 2.34693878, 2.36734694, 2.3877551 ,
2.40816327, 2.42857143, 2.44897959, 2.46938776, 2.48979592,
2.51020408, 2.53061224, 2.55102041, 2.57142857, 2.59183673,
2.6122449 , 2.63265306, 2.65306122, 2.67346939, 2.69387755,
2.71428571, 2.73469388, 2.75510204, 2.7755102 , 2.79591837,
2.81632653, 2.83673469, 2.85714286, 2.87755102, 2.89795918,
2.91836735, 2.93877551, 2.95918367, 2.97959184, 3. ])

In [142]: np.linspace(2,3,num=50,retstep =True) # there are last step is jumped size.

Out[142]: (array([2. , 2.02040816, 2.04081633, 2.06122449, 2.08163265,


2.10204082, 2.12244898, 2.14285714, 2.16326531, 2.18367347,
2.20408163, 2.2244898 , 2.24489796, 2.26530612, 2.28571429,
2.30612245, 2.32653061, 2.34693878, 2.36734694, 2.3877551 ,
2.40816327, 2.42857143, 2.44897959, 2.46938776, 2.48979592,
2.51020408, 2.53061224, 2.55102041, 2.57142857, 2.59183673,
2.6122449 , 2.63265306, 2.65306122, 2.67346939, 2.69387755,
2.71428571, 2.73469388, 2.75510204, 2.7755102 , 2.79591837,
2.81632653, 2.83673469, 2.85714286, 2.87755102, 2.89795918,
2.91836735, 2.93877551, 2.95918367, 2.97959184, 3. ]),
0.02040816326530612)

In [143]: # logspace ...


np.logspace(2,4,num=4,base=10)

Out[143]: array([ 100. , 464.15888336, 2154.43469003, 10000. ])

In [144]: #Iterative matrix method inside the numpy....

In [145]: np.eye(5) # Its generate 5x5 matrixes where all the diagonal matrix is 1 and rest of the 0

Out[145]: array([[1., 0., 0., 0., 0.],


[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])

localhost:8888/notebooks/Downloads/numpy documents.ipynb# 14/16


4/17/23, 1:25 PM numpy documents - Jupyter Notebook

Q1. What is the difference between a shallow copy and a deep copy in NumPy?

Answer: In NumPy, a shallow copy creates a new array object that points to the same data as the original array,
while a deep copy creates a new array object with its own data.

To create a shallow copy of an array, you can use the view() method or the slicing operator [:]. For example:

In [149]: import numpy as np



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

b = a.view()

c = a[:]

In [154]: print(b)

[1 2 3]

In [155]: print(c)

[1 2 3]

In [156]: ## In this example, b and c are shallow copies of a.

To create a deep copy of an array, you can use the copy() method. For example:

In [157]: import numpy as np



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

b = a.copy()

In [158]: print(b)

[1 2 3]

In [159]: ## In this example, b is a deep copy of a.

concatenate two arrays in NumPy

concatenate two arrays in NumPy using the concatenate() function or the vstack() and hstack() functions,
depending on whether you want to concatenate the arrays vertically or horizontally.

For example, to concatenate two arrays a and b horizontally, you can use the hstack() function as follows:

In [160]: import numpy as np

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

localhost:8888/notebooks/Downloads/numpy documents.ipynb# 15/16


4/17/23, 1:25 PM numpy documents - Jupyter Notebook

In [162]: b = np.array([11,22,33,44,55,66,77,88,99])

In [163]: c = np.hstack((a, b))

In [164]: print(c)

[ 1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99]

To concatenate the arrays vertically, we can use the vstack() function

In [165]: import numpy as np

In [166]: a = np.array([[1, 2], [3, 4]])

In [167]: b = np.array([[5, 6]])

In [168]: c = np.vstack((a, b))

In [169]: print(c)

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

map function over a NumPy array

In [179]: import numpy as np

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

In [181]: f = lambda x: x ** 2

In [183]: squares = f(x)

In [184]: print(squares)

[ 1 4 9 16 25]

In [ ]: ​

end...................................................................................
In [ ]: ​

localhost:8888/notebooks/Downloads/numpy documents.ipynb# 16/16

You might also like