[Python Data Science Quick Start Series 02] More than a dozen ways to create ndarray objects

Hits: 0

This is the 39th article in the future of machines

Original address: /RobotFutures/article/details/126092621

1. np.array method creation

Convert list or tuple to ndarray array

  • Create a one-dimensional array

import numpy as np
arr = np.array([ 1 , 2 , 3 , 4 , 5 ])   # create a one-dimensional array 
arr

array([1, 2, 3, 4, 5])

  • Create a two-dimensional array and specify the data type

arr2 = np.array([[1,2,3,4],[5,6,7,8]], dtype=float)   # Create a two-dimensional array 
arr2

array([[1., 2., 3., 4.],
       [5., 6., 7., 8.]])

  • Create high-dimensional arrays

arr3 = np.array([[[1]]])             # Create a three-dimensional array 
arr3

array([[[1]]])

print( f"arr_ndim: {arr.ndim} , arr2_ndim: {arr2.ndim} , arr3_ndim: {arr3.ndim} " )
print(f"arr_shape:{arr.shape}, arr2_shape:{arr2.shape}, arr3_shape:{arr3.shape}")

arr_shape :1 , arr2_shape :2 , arr3_shape :3 
arr_shape :(5 ,), arr2_shape :(2 , 4), arr3_shape :(1 , 1, 1)

2 Methods of creating ndarray objects according to rules

2.1 Arithmetic progression linespace

The linespace function is often used to generate the X-axis displayed by the matplot icon.

x1 = np.linspace(start=1,stop=10,num=10)
start: start value, stop: end value (inclusive), num: number of elements

x1 = np.linspace(start=1,stop=10,num=10)
x1

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

2.2 Generate arithmetic sequence

x2 = np.arange(start=1,stop=100,step=3)
can specify the step size step

x2 = np.arange(start=1,stop=100,step=3)
x2

array([ 1,  4,  7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 49,
       52, 55, 58, 61, 64, 67, 70, 73, 76, 79, 82, 85, 88, 91, 94, 97])

2.3 Generate an array with all 1s

x = np.ones(shape=(3,3))
x

array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])

2.4 Generate an array with all 0 values

np.zeros(shape=(3,3))

array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])

2.5 According to the shape of x2, generate an array with all 1 values

x2 = np.arange(start=1,stop=100,step=3)
print(x2)
x2_1 = np.ones_like(x2)
print(x2_1)

[ 1  4  7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70
 73 76 79 82 85 88 91 94 97]
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]

2.6 Generate an array with all 0 values ​​according to x2

np.zeros_like(x2)

array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

2.7 Diagonal matrices

Generates a diagonal matrix of the specified ndarray object

x2 = np.linspace(start=1, stop=64,num=64)
x2 = x2.reshape(8, 8)
print(x2)
np.diag(x2)

[[ 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. 54. 55. 56.]
 [57. 58. 59. 60. 61. 62. 63. 64.]]

array([ 1., 10., 19., 28., 37., 46., 55., 64.])

It can be seen that diag only extracts the values ​​of the upper left, right and lower diagonals of the 8*8 matrix.

2.8 Identity Matrix

Generates an identity matrix of the specified shape.

np.eye(8)

array([[1., 0., 0., 0., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0., 0., 0., 0.],
       [0., 0., 1., 0., 0., 0., 0., 0.],
       [0., 0., 0., 1., 0., 0., 0., 0.],
       [0., 0., 0., 0., 1., 0., 0., 0.],
       [0., 0., 0., 0., 0., 1., 0., 0.],
       [0., 0., 0., 0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 0., 0., 0., 1.]])

3. Random number generation

3.1 Generating a normally distributed random array

np.random.normal(loc=0.0, scale=1.0, size=None)

Input: 
loc  - mean, it can be a single integer or an array, when it is an array, specify the mean value of each column 
scale  - standard deviation, it can be a single integer or an array, when it is an array, specify the standard deviation of each column 
size  - the shape of the data

Returns: 
ndarray object

np.random.normal(3, 2.5, size=(2, 4))

array([[ 4.99637559,  4.41112766, -4.46850171,  2.34375326],
       [ 7.80167697,  4.88896638,  4.50739853,  1.91669014]])

import numpy as np
from matplotlib import pyplot as plt

# loc-mean, scale-standard deviation, size-data shape of random array shape 
y = np.random.normal(loc= 0.0 , scale= 1.0 , size= 1000 )
x = np.linspace(0, 100, num=1000)

plt.hist(x=y, bins=50)
plt.show()


3.2 Generating a uniformly distributed random array

np.random.uniform(low=0.0, high=1.0, size=None)

Input: 
low  - the minimum value, which can be a single integer or an array, when it is an array, specify the minimum value of each column 
high  - the maximum value, the range is an open interval [low, high), which can be a single integer or an array Array, when it is an array, specify the maximum value of each column 
size  - the shape of the data

The data type is float

Returns: 
ndarray object

import numpy as np

x = np.random.uniform(size=(3,3))
x

array([[0.55604474, 0.23922789, 0.49744522],
       [0.72736682, 0.50886455, 0.89055923],
       [0.86007017, 0.45767947, 0.64721748]])

import numpy as np
from matplotlib import pyplot as plt

y = np.random.uniform(size=(10000))
x = np.linspace(0, 100, num=10000)

plt.hist(x=y, bins=36)
plt.show()


3.3 Generating Randomly Distributed Arrays

np.random.random(size=None) is
equivalent to np.random.uniform(size=None), that is, the uniform of the default low and high, which is the alias/abbreviation of random_sample

enter:
size - the shape of the data
The data interval is: [0.0, 1.0)
The data type is float

return:
ndarray object

x = np.random.random(size=(3,3))
x

array([[0.81853323, 0.68854024, 0.93096203],
       [0.73975546, 0.48552889, 0.62078013],
       [0.08407448, 0.16430265, 0.08354929]])

import numpy as np
from matplotlib import pyplot as plt

y = np.random.random(size=100000)
x = np.linspace(0, 100, num=100000)

plt.hist(x=y, bins=36)
plt.show()


After the random distribution reaches a certain amount of data, it can be seen that it is also uniformly distributed.

In addition, np.random.rand can also achieve the same function as np.random.random, even size=() does not need to be written, just fill in the data shape directly.

x = np.random.rand(3,3)
x

array([[0.48217616, 0.45697736, 0.51243651],
       [0.42060063, 0.12191877, 0.11430536],
       [0.43719726, 0.88747028, 0.63576976]])

3.4 Generating an array of uniformly distributed integers

np.random.randint(low, high=None, size=None, dtype=int)

Input: 
low  - the minimum value, must be filled in, can be a single integer or an array, when it is an array, specify the minimum value of each column 
high  - the maximum value, can be a single integer, or an array, when it is an array, specify Maximum 
size  per column size - data shape

The data interval is : [low, high), if you want to implement the [low, high] closed interval, you can use the random_integers 
data type to choose a variety of integer types

Returns: 
ndarray object

x = np.random.randint(low=0, high=10, size=(3,3), dtype=np.uint8)
x

array([[5, 6, 4],
       [6, 9, 8],
       [4, 6, 7]], dtype=uint8)

Write at the end:

  • Blog Introduction: Focus on the AIoT field, follow the pulse of the future era, and record the technological growth on the road!
  • Column introduction: Master the common data science libraries Numpy, Matploblib, and Pandas from 0 to 1.
  • Target audience: AI primary learners
  • Column plan: Next, we will gradually publish a series of blog posts that step into artificial intelligence, so stay tuned

Leave a Reply

Your email address will not be published.