Num Py
Num Py
In [ ]:
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 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.
1.23.5
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...............
[1 2 3 4]
. Two-Dimensional array...............
In [176]: print(numpy_arr)
[[1 2 3 4]
[4 5 6 7]]
. Three-Dimensional array.................
In [178]: print(numpy_arr)
[[[ 1 2 3 4]
[ 4 5 6 7]
[ 7 8 9 10]]]
In [5]: type(np.array([2,3,4,5,6]))
Out[5]: numpy.ndarray
In [9]: a = np.array([3,4,5,6],ndmin=5)
Out[14]: 2
In [15]: # so array and matrix is same matrix are a sub classes of array
In [17]: np.random.randint(1,10,(4,4)) # random.randint this is function inside numpy and this functi
In [18]: # above dataset we genrate data 1 to 10 range and 4,4 means 4 row and 4 columns.
[[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 [21]: l = [1,2,3,4,5,6]
In [24]: l1 = [4,5,6,7,"himu",45.556,True]
In [25]: np.array(l1)
In [26]: # l1 list every elements is converted in string format its difference between l & l1 list.
In [28]: np.array([[[1,2,3],[4,5,6],[7,8,9]]])
In [29]: a1 = np.array(l1)
In [31]: a3 = np.array([[[1,2,3],[4,5,6],[7,8,9]]])
In [32]: a1
In [33]: a2
In [34]: a3
In [35]: # I want know above every array what's the dimension of thsese array
Out[36]: 1
Out[37]: 2
Out[38]: 3
In [40]: a1.size
Out[40]: 7
In [41]: a2.size
Out[41]: 6
In [42]: a3.size
Out[42]: 9
In [45]: a1.shape
Out[45]: (7,)
In [47]: a2.shape
Out[47]: (2, 3)
In [49]: a3.shape
Out[49]: (1, 3, 3)
In [52]: a1
In [53]: a2
In [54]: a3
Out[56]: 23
In [57]: np.random.randint(2,50,(3,4))
In [59]: np.random.randint(2,50,(2,3,4))
In [60]: np.random.rand(2,3)
In [62]: np.random.rand(5,4)
In [63]: np.random.rand(4,4)
In [65]: a4 = np.random.rand(4,4)
In [66]: a4
In [67]: a4.reshape(2,8)
In [68]: a4.reshape(8,2)
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)
In [72]: a4.reshape(1,1,1,1,1,1,1,2,2,4)
In [74]: a1
In [76]: a1[0]
Out[76]: '4'
In [80]: a1[::-1]
In [82]: a3
In [84]: # extract 1, 2, 3
In [85]: a2[0]
In [86]: a2[:,[1,2]]
In [87]: a2[[0,1],1:]
In [89]: a5
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])
In [92]: a5
In [96]: a5
In [97]: a6 = np.random.randint(0,3,(3,3))
In [98]: a7 = np.random.randint(0,3,(3,3))
In [99]: a6
In [100]: a7
In [101]: # multipliction
In [102]: a6*a7 # this is element wise multipliction this is not matrix multiplication at all.
In [103]: # addition
In [104]: a6+a7 # this is element sequences wise addition this is not matrix addition at all.
In [105]: # subtraction
In [106]: a6-a7 # this is element sequences wise subtraction this is not matrix subtraction at all.
In [114]: a6**3
In [115]: a8 = np.zeros((4,4)) # zeros is function inside the numpy its giving everything is zero.
In [116]: a8
In [117]: a9 = np.ones((4,5)) # ones means all the elements gives one (1).
In [118]: a9
In [121]: a9
In [122]: a9 + np.array([1,2,3,4,5]) # here its did the row by row operation is added.
Out[126]: array([[1],
[2],
[3],
[4]])
In [128]: a6
In [131]: np.sqrt(a5)
In [133]: # expo...
np.exp(a5)
In [134]: #log...
np.log10(a5)
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]
Out[138]: array([ 1. , 3.5, 6. , 8.5, 11. , 13.5, 16. , 18.5, 21. , 23.5, 26. ,
28.5])
In [141]: np.linspace(2,3,num=50)
In [145]: np.eye(5) # Its generate 5x5 matrixes where all the diagonal matrix is 1 and rest of the 0
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 [154]: print(b)
[1 2 3]
In [155]: print(c)
[1 2 3]
To create a deep copy of an array, you can use the copy() method. For example:
In [158]: print(b)
[1 2 3]
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 [161]: a = np.array([1,2,3,4,5,6,7,8,9])
In [162]: b = np.array([11,22,33,44,55,66,77,88,99])
In [164]: print(c)
[ 1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99]
In [169]: print(c)
[[1 2]
[3 4]
[5 6]]
In [181]: f = lambda x: x ** 2
In [184]: print(squares)
[ 1 4 9 16 25]
In [ ]:
end...................................................................................
In [ ]: