Advertisement
elena1234

numpy exercises in Python

Apr 15th, 2022 (edited)
1,068
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | None | 0 0
  1. import numpy as np
  2.  
  3. # Create an array of 10 zeros
  4. zeros_array = np.zeros(10)
  5. print(zeros_array)
  6.  
  7. # Create an array of 10 ones
  8. ones_array = np.ones(10)
  9. print(ones_array)
  10.  
  11. # Create an array of 10 fives
  12. fives_array = np.empty(10, dtype = np.int32)
  13. fives_array.fill(5)
  14. print(fives_array)
  15.  
  16. # Create an array of the integers from 10 to 50
  17. integers_array = np.arange(10,51)
  18. print(integers_array)
  19.  
  20. # Create an array of all even integers from 10 to 50
  21. even_array = np.arange(10,51,2)
  22. print(even_array)
  23.  
  24. # Create a 3 by 3 matrix with values ranging from 0 to 8
  25. my_matrix = np.arange(0,9)
  26. reshape_matrix = np.reshape(my_matrix, (3,3))
  27. print(reshape_matrix)
  28.  
  29. # Create a 3 by 3 identity matrix
  30. identity_matrix = np.eye(3)
  31. print(identity_matrix)
  32.  
  33. # Generate random number between  0 and 1
  34. random_number = np.random.rand(1)
  35. print(random_number)
  36.  
  37. # Generate an array of 25 random numbers sampled from a standard normal distribution
  38. st_normal_array = np.random.randn(25)
  39. print(st_normal_array.reshape(5,5))
  40.  
  41. # Create the following matrix
  42. following_matrix = np.arange(0.01, 1.01, 0.01)
  43. print(following_matrix.reshape(10,10))
  44.  
  45. # Create an array of 20 linearly spaced points between 0 and 1
  46. linearly_array = np.linspace(0, 1, 20)
  47. print(linearly_array.reshape(4,5))
  48.  
  49.  
  50. # Crete the matrices shown below from "mat" matrix
  51. mat = np.arange(1,26).reshape(5,5)
  52. slice_of_array = mat[2:,1:]
  53. print(slice_of_array)
  54. print(mat[3][4])
  55. print(mat[0:3,1])
  56. print(mat[4:,])
  57. print(mat[3:,])
  58.  
  59. # Get the sum of all the values in mat
  60. print(mat.sum())
  61.  
  62. # Get the standard deviation of the values in mat
  63. print(np.std(mat))
  64.  
  65. # Get the sum of all the columns in mat
  66. print(mat.sum(axis = 0))
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement