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

Guide for Beginners

Python

Uploaded by

tanvir islam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
12 views

Guide for Beginners

Python

Uploaded by

tanvir islam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 13
‘wraa2g, 1:40 AM In [1]: In [2]: out[2]: In [3]: out(3]: In [4]: out[4]: In [5]: out(5]: Unttles40- Jupyter Notebook Why NumPy? NumPy is a powerful library in Python that makes numerical computations easy and efficient. 1. NumPy provides efficient storage. 2. Italso provides better ways of handling data for processing. 3. Its fast. 4. Itis easy to lear, 5. NumPy uses relatively less memory to store data. Importing the NumPy library import numpy as np Lista=[1,2,3,4,5] print(list1) type(list1) (1, 2,3, 4, 5] list #Converting List to NumPy array arr = np.array(list1) print(arr) type(arr) [12345] numpy nndarray listarray= np.array([[1,2,3],[5,5,8],[4,6,9]]) print (listarray) listarray.size #Number of elements in an array [[1 23] [5 5 8] [46 9] 9 listarray.dtype #Checking the data type of the values in the array dtype(*int32') Creating a simple 1D array localhost B888inotebooks/Untied0 ipynb an. ‘12324, 1:40 AM In [6]: out[6]: In [7]: out[7]: In [8]: out[8]: Unttled40 - upyter Notebook arr = np.array([1, 2, 3, 4, 5]) print("1D Array:") print(arr) arr.shape 1D Array: (22345) (53) Creating a 2D array (Matrix) np.array([[1, 2, 3], [4, 5, 6]]) print("2D Array:") print(arr_2d) arr_2d.shape #the number of rows and columns an array 20 Array: (1 23] [45 6)) (2, 3) aenp.array({(1,2,3,2],[2,5,82,7], [5.8.7.5] ]dtypesnp.int32) print(a) a.shape Number of rows (axis @): 3(Refers to the vertical direction in the array) #Number of columns (axis 1): 4(Refers to the horizontal direction in the array, C 1 [123 2 [2 582 7] [5 8 7 s]] G, 4) Creating a 3D array localhost B888inotebooks/Untied0 ipynb ans ‘12324, 1:40 AM Until - upyter Notebook In [9]: # Create a 30 array with dimensions (2, 3, 4) # 2 matrices(Layers), 3 rows, and 4 columns arr_3d = np.array({((1, 2, 3, 4], (5, 6 7, 8], [9, 10, 11, 12]], [[23, 14, 15, 16], [27, 18, 19, 20], [21, 22, 23, 24]]], dtype=float) print (arr_3d) print ("Shape: arr_3d. shape) (Ila. 2. 3. 4.) [5. 6. 7. 8] (9. 10. 11. 12.]) [[23. 14, 15. 16.) (17. 18. 19. 26.] (21. 22. 23. 24.]]] Shape: (2, 3, 4) In [10]: arr_3d.ndim 4Number of Dimensions out[1e]: 3 In [11]: zeros_3d = np.zeros((2, 3, 4)) zeros_3d Out[11]: array([[[@., @-, @, @], [0., 0, 0, a], [e., @, ®, @]], [lo., 5 [o., @ [0., @, ese ese J 4 5 Tn [12]: ones_3d = np.ones((2, 3, 4)) ones_3d out(12]: array({([1., 1., 1.5 1], (1,1. 1, 1], (1,1. 1, 1), [fy 2. 2, 2d, (1,2, 4, 1], (4,2. 2, 21) localhost B888inotebooks/Untied0 ipynb 33 ‘wraa2g, 1:40 AM In [13]: out[13]: In [14]: In [15]: out[15]: In [16]: out(16]: In [17]: In [18]: Unttles40- Jupyter Notebook random_3d = np.random.random((2, 3, 4)) random_3d array([[[@.98835175, @.69182741, @.39687112, @.03449365], [0.59837054, @.93019528, @. 26655287, 0.35428686],, [0.@2402669, @.89223808, @.89911346, @.18816227]],, [[0.85583822, .23225966, @.71509715, @.53106299],, [0.95348822, @.25764291, @.59329536, @.08893972],, [0.03570523, @.80498216, ¢.09447798, @.11753341]]]) Creating a zero-filled array zeros = np.zeros((2, 3)) #2x3 array of zeros print ("Zero-filled Array: print (zeros) Zero-filled Array: [[e. 8. 0.) [@. @ @.]] zeros. dtype dtype("floatéa") zeros. shape (2, 3) Creating an array of ones print (ones) Ones-filled Array: ((2. 4.4.) (1. 1. 1.) (1. 1. 1.)) Creating an array of a Particlular value array_of_threes= np.full((5,4),3) #5x4 array filled with 3 print (array_of_threes) ([3 3 (33 (33 (33 (33 localhost B888inotebooks/Untied0 ipynb 4n3 ‘wraa2g, 1:40 AM Unttles40- Jupyter Notebook In [19]: |# Create @ 10 array of Length 5, filled with 2 and with dtype floates array_of_twos_flost = np.full(5, 2, dtype-np.floatea) print (array_of_twos_float) [2.2.2 2. 2.) In [20]: # Creating an empty array of shape (4, 6) emp = np.empty((4, 6)) #Creates a NumPy array emp out[20]: array([[@. @. ie. 8 le. 8 [e. 8 98835175, @.69182741, 93019528], 26655287, @.35428686, 18816227], 85583822, 23225966, 25764291], 59329536, .08893972, -11753341]]) Identity matrix In [21]: |# Create a 45x45 identity matrix ide = np.i dentity(45) 039687112, 2.0240269, @.71509715, .03570523, without initializing its values 03449365, 89223808, 53196299, 80498216, 059837054, 0.89911346, 0.95348822, 0.09447798, fthe diagonal elements are set to 1, with all other elements being 0. print (ide) ide. shape (a. @. [e. 4. (2. 2. Heo (2. 0. 2 [e. 8. [e. 8. @& out{21]: (45, 45) In [22]: ide.size out[22]: 2025 In [23]: ide.dtype out[23]: dtype( "Flo: 2s bee tea") localhost B888inotebooks/Untied0 ipynb sna ‘12324, 1:40 AM In [24]: In [25]: out[25]: In [26]: In [27]: out[27]: In [28]: out [28]: Unttled40 - upyter Notebook # Create an &x8 identity matrix eye matrix = np.eye(8) fyou can specify the number of rows and columns or shift the diagonal. print (eye_matrix) [[1. @. 0. 0. 0. 0. @. 0.) [0. 1. 0. 0.0. 0.0. 0.] [0. 0.1. 0. 0. 0. 8. 2.] [0. 0.0.1.0. 0.0.0] [0. 0. 0. 0.1. 0. 0. 2] [0. 0. 0. 0.0.1.0. 2.) (0. @. @. 6. @. 8. 1, 0.) [0. 0. 0. 0. 0. 0. @ 1.]] Create a numpy array with random values # Create a 3x4 array with random float values between @ and 1 b=np.random.random((3,4)) b array({[@.61654747, 0.123858 , 0.74552042, @.18347791], [@.29773944, @.95492384, @.52978465, @.94309887], [@.36198761, @.98647445, 0.78015207, 6.60135261]]) Create a random integer values array within a specific range #Create a 3x5 array with random integers between 10(incLusive) and 100(excLusi| random_integers = np.random.randint(10, 100, (3, 5)) print (random_integers) [[33 95 32 42 84] [a1 81 47 33 72] [98 5@ 87 53 48]] fcreates a sequence with a specified number of points in a specified range Ispace=np. linspace(1,10,6) # Creating an array with 6 equally(evenly) spaced values between 1 and 10 Ispace array([ 1. , 2.8, 4.6, 6.4, 8.2, 10. }) rng=np.arange(15) rng array([ @, 1, 2, 3, 4, 5, 6 7, 8, 9 10, 11, 12, 13, 14]) localhost B888inotebooks/Untied0 ipynb ens ‘wraa2g, 1:40 AM In [29]: out[29]: In [30]: In [31]: out[32]: In [32]: out[32]: In [33]: out[33]: In [34]: out[34]: Unttles40- Jupyter Notebook fcreates a sequence with a specified step size f#but doesn't guarantee the no. of elements arr_arange = np.arange(1, 10, 2) #1 to 10 with step size of 2 arr_arange array([1, 3, 5, 7, 9]) rng = np.arange(1®, 45) # Creating an array from 10 to 45 print (rng) [10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44] arr=np.arange(99) #Create a 1D array with 99 values from @ to 98 arr array([ ®, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 26, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 5e, 51, 52, 53, 54, 55, 56, 57, 58, 59, 68, 61, 62, 63, 64, 65, 66, 67, 68, 69, 78, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 92, 91, 92, 93, 94, 95, 96, 97, 98]) arr. reshape(3,33) #Reshaping the 1D array into a 3x33 20 array(3 rows and 33 columns) array(({ @, 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, 32], [33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, Sd, 55, 56, 57, 58, 59, 68, 61, 62, 63, 64, 65], [66, 67, 68, 69, 78, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 92, 91, 92, 93, 94, 95, 96, 97, 98]]) arr-arr.ravel() #This flattens the reshaped 2D array back into a 1D array arr array({ @, 1, 2, 3, 4, 5, 6 7, 8, 9, 18, 11, 12, 13, 24, 15, 16, 47, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 62, 61, 62, 63, 64, 65, 66, 67, 68, 69, 7, 71, 72, 73, 74, 75, 76, 77, 78, 79, 8@, 81, 82, 83, 84, 85, 86, 87, 88, 89, 98, 91, 92, 93, 94, 95, 96, 97, 98]) arr.shape (99,) Mathematical operations on a np array and Array Manipulation localhost B888inotebooks/Untied0 ipynb ms ‘12324, 1:40 AM Until - upyter Notebook In [35]: x=[[1,2,3],[4,5,6],[7,1,0]] ar=np.array(x) ar out[35]: array(f[4, 2, 3], [4, 5, 6], (7% 1, @]]) In [73]: ar.sum(axis=1) # Sum along axis 1 (sum of each row) means the operation works across columns (horizontally) for each row. Out[73]: array([ 6, 15, 8]) In [74]: ar.sum(axis=@) # Sum along axis @ (sum of each column) fmeans the operation works across rows (vertically) for each column. out(74]: array([12, 8, 9]) In [38]: ar.T #Transpose of the array out[38]: array([[1, 4, 7], (2, 5, 1), [3, 6, @]]) In [39]: ar.nbytes #checking the no. of bytes(memory) consumption of an array out[39]: 36 In [40]: one=np.array([1,2,5,4,95,8]) one.argmax() #Zndex of the maximum value in one out(4e}: 4 In [41]: one.argmin() #Index of the minimum value in one out[4i]: @ In [42]: one.argsort() #Indices that would sort the array out[42]: array([®, 1, 3, 2, 5, 4], dtypesinted) In [43]: ar out[43]: array([[1, 2, 3], [4 5, 6], {7 1, 11) In [44]: ar.argmin() #index of the minimum value in the entire array out[44]: 8 localhost B888inotebooks/Untied0 ipynb ans ‘12324, 1:40 AM In [45]: out[45]: In [46]: out [46]: In [47]: out(47}: In [76]: out[76]: In [77]: out(77]: In [78]: out[78]: In [49]: out [49]: In [50]: out[se]: In [51]: out[51]: In [52]: out(s2]: localhost B888inotebooks/Untied0 ipynb ar.argnax() 6 Unttled40 - upyter Notebook ar.argsort() #indices that would sort the array array([[@, 1, 2], {@, 1, 2), [2, 1, @]], dtype=int6a) ar.argnax(axis=1) #Index of the maximum array([2, 2, @], dtype=intea) ar.argmin(axis=1) #Index of the minimum array([@, @, 2], dtype=int64) ar.argmax(axis=0) #Index of the maximum array([2, 1, 1], dtype=intea) ar.argmin(axis=@) #Index of the minimum array([®, 2, 2], dtype-ints4) ar.ravel() array([1, 2, 3, 4, 5, 6, 7, 1, @]) array([[1, [2 ic ar array([[1, [4 U7, 2, 5, 1, p-array([[1,2,3],[2,5,4],[7,8,9]]) 31, 4), 91) 31, 61, @]]) artar2 Adding two arrays array([{ 2, (14, 4, 6), [ 6, 19, 10], 9 9))) value value value value in in each each each each #index of the maximum value in the entire array row column column ong ‘12324, 1:40 AM In [53]: out[53]: In [54]: out (54): In [55]: out (55): In [56]: out [56]: In [57]: out {57}: In [58]: out[58]: In [59]: out [59]: In [69]: In [62]: Unttled40 - upyter Notebook [324,32]+134,545] [324, 32, 34, 545] artar2 #£Lement-wise multiplication array([[ 1, 4, 9], [ 8, 25, 24], [49, 8, @1]) np.square(ar) #£Lement-wise square of the array array([[ 1, 4, 9], [16, 25, 36], [49, 4, @]]) np.sqrt(ar2) #£Lement-wise square root of the array array([[1- » 1.41421356, 1.73205081], [1.41421356, 2.23606798, 2. 1 [2.64575131, 2.82842712, 3. i) ar.sum() #Sum of alt elements in the array 29 ar2.min() #Minimum value in the entire array 1 ar.max() #Maximum value in the entire array 7 List1 = [1,2,3,4,5] List2 = [6,7,8,9,10] print(list1 + list2) concatenate or joins two List (2, 2,3, 4 5, 6, 7, 8, 9, 16) np.random.randint(@,1@,(3,3)) np.random.randint (10,28, (3,3)) localhost B888inotebooks/Untied0 ipynb 1013 ‘wraa2g, 1:40 AM In [62]: In [63]: In [64]: In [65]: Unttles40- Jupyter Notebook print(a) print(b) [12 9 6] [5 6 8] (2.2 7]) {15 16 17) [12 19 16] [18 15 11)] print (a+b) print(a-b) print(a*b) print(a/b) [[a7 25 23] [17 25 24] [19 16 18]] ([-13 -7 -41] [ -7 -13. -8] [-17 -14 -4]] [[ 3@ 144 102] [ 60 114 128] {18 15 77)] [(@.13333333 2.5625 35294118) [@.41666667 @.31578947 6.5 ] [0.05555556 0.06666667 0.63636364]] np.random.randint(8,10, (3,3)) np.random.randint (10,20, (3,3)) print(a) print(b) [[4 8 5] [129] [4 4 5]] [faz 14 13) [24 14 35] [as 32 37] localhost B888inotebooks/Untied0 ipynb ans swiasieg SMO AM In [66]: In [67]: In [68]: In [69]: In [70]: Unttles40- Jupyter Notebook print (np.add(a,b)) print (np. subtract (a,b)) print (np.multiply(a,b)) print (np.divide(a,b)) [{16 22 16] [15 16 24] [19 16 22]] [[ -8 -6 -6] [-13 -12 -6] [-11 -8 -12}] [[ 48 112 55] [14 28 135] [ 6@ 48 85}) ((@.33333333 257142857 @,45454545) [8.07142857 @.14285714 0.6 ] [0.26666667 @.33333333 @.29411765]] array = np.randon.randint(2,10,(2,3)) print (array) print (array.shape) (3 3.3] (6 44]] (2, 3) trans = np.transpose(array) print (trans) print (trans. shape) ([3 6] [3 4] (3 2]] @, 2) array = np.random.randint (0,10, (2,3)) print (array) print (array. shape) [[41] [6 4 4]] (2, 3) trans2 = array.T print (trans2) print (trans2. shape) {[4 6] {1 4) {1 4)) (, 2) localhost B888inotebooks/Untied0 ipynb vans ‘wraa2g, 1:40 AM In [71]: In [72]: Unttles40- Jupyter Notebook a = np.random.randint (2,10, (2,3)) print(a) print (a.shape) U7 9 6) (2 23]] (2, 3) b = a.reshape(3,2) print(b) print(b.shape) (7 9] [6 2] [2 3]] (3, 2) localhost B888inotebooks/Untied0 ipynb 1383

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