Advertisement
elena1234

numpy array in Python

Apr 14th, 2022 (edited)
893
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | None | 0 0
  1. import numpy as np
  2.  
  3. # Numpy.array()
  4. my_list = [1, 2, 3, 4, 5]
  5. my_array = np.array(my_list)
  6. print(my_array)
  7.  
  8. matrix = [[1, 2, 3, 4], [3, 4, 5, 6], [6, 7, 8, 9]]
  9.  
  10. np_matrix = np.array(matrix)
  11. print(np_matrix)  # 4 by 4 matrix
  12.  
  13.  
  14. # Numpy.arange()
  15. new_array = np.arange(0, 11)
  16. print(new_array)
  17.  
  18. new_array2 = np.arange(0, 11, 2)
  19. print(new_array2)
  20.  
  21.  
  22. # Numpy.zeros()
  23. zeros_array = np.zeros(5)
  24. print(zeros_array)
  25. zeros_array1 = np.zeros((5, 5))
  26. print(zeros_array1)  # 5 by 5 matrix with zeros
  27.  
  28.  
  29. # Numpy.ones()
  30. ones_array = np.ones(5)
  31. print(ones_array)
  32. ones_array1 = np.ones((5, 5))
  33. print(ones_array1)  # 5 by 5 matrix with ones
  34.  
  35.  
  36. # Numpy.linspace()
  37. array = np.linspace(0, 10, 5)  # [ 0. 2.5 5. 7.5 10. ]
  38. print(array)
  39. array1 = np.linspace(0, 5, 5)  # [ 0. 1.25  2.5  3.75  5. ]
  40. print(array1)
  41. array2 = np.linspace(0, 5, 6)
  42. print(array2)  # [0. 1. 2. 3. 4. 5.]
  43.  
  44.  
  45. # Create identity matrix
  46. identity_matrix = np.eye(5)
  47. print(identity_matrix)
  48.  
  49.  
  50. # Return random samples from a uniform distribution over [0, 1)
  51. var = np.random.rand(10)  # 10 numbers from 0 to 1
  52. print(var)
  53. var1 = np.random.rand(5, 4)  # matrix with 5 rows and 4 colums from 0 to 1
  54. print(var1)
  55.  
  56.  
  57. # Return a sample (or samples) from the "standard normal" distribution
  58. var2 = np.random.randn(5, 4)
  59. print(var2)
  60.  
  61.  
  62. # Return random integers from "low"(inclusive) to "high"(exclusive)
  63. var3 = np.random.randint(1, 11, 2)
  64. print(var3)  # [10 8]
  65. var4 = np.random.randint(1, 11, (2, 2))
  66. print(var4)  # array 2 by 2
  67.  
  68. # The seed() method is used to initialize the random number generator.
  69. # The random number generator needs a number to start with (a seed value), to be able to generate a random number.
  70. np.random.seed(42)
  71. print(np.random.rand(4))
  72.  
  73. np.random.seed(42)
  74. print(np.random.rand(4))  # tha same result like above
  75.  
  76.  
  77. # reshape()
  78. arr = np.arange(0, 25)
  79. print(arr)
  80. print(arr.reshape(5, 5))
  81.  
  82.  
  83. # max() and min()
  84. ranarray1 = np.random.randint(0, 10, 4)
  85. print(ranarray1.max())
  86. print(ranarray1.min())
  87. print(ranarray1.argmin())  # get the index of min value
  88. print(ranarray1.argmax())  # get the index of max value
  89.  
  90.  
  91. # dtype
  92. print(ranarray1.dtype) # int32
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement