• Numpy.array数组学习笔记(数组结构详解,数组操作方法,数组读取方法,数组计算方法,数组画图方法,数组存储方法)


    This blog is about learning Numpy with python.
    You are welcomed to chat about it and if you like this blog, do not forget to give me a like.
    
    • 1
    • 2

    Welcome to see my homepage and contact me: NicholasYe’s Homepage.


    Recommendation:


    1. Introduction of Array

    • An array is a central data structure of the NumPy library:

      • The elements are all of the same type, referred to as the array dtype.
      • An array can be indexed by a tuple of nonnegative integers, by booleans, by another array, or by integers.
      # We can initialize an array with python list:
      a = np.array([1, 2, 3, 4, 5, 6])
      b = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
      
      # If we want to get the first element of the array:
      print(a[0])             # Output: 1
      print(type(a[0]))       # Output: 
      print(b[0])             # Output: [1 2 3 4]
      print(type(b[0]))       # Output: 
      # Please notice: b[0] is not the same as python list, it is an array.
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10

    2. How to create an Array

    • From the last part, we have a vague view of how to create a array, here we will introduce how to create array in multiple ways.
    1. Create array with all elements are 0.

      # We can create zero array with np.zeros
      a = np.zeros(2)
      
      # Here is how arrays look like:
      print(a)
      # Output: 
      # [0. 0.]
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
    2. Create array with all elements are 1.

      • Use np.ones() function
        • This is the same as np.zeros() function, I will escape illustration.
    3. Create array with recursive numbers

      • Use np.arange() function, in the following example:
        • 0 means the start of the array
        • 10 means the end of the array
        • 2 means the step of the array
      a = np.arange(0, 10, 2)
      print(a)            # Output: [0 2 4 6 8]
      
      • 1
      • 2
    4. Create array with random float number vary from 0 to 1

      • Use np.random.random() function, in the following example:
        • (2,3) means the structure of array:
          • It is an 2D array
          • This array has 2 dimensions(axes). The first axis has a length of 2 and the second axis has a length of 3.
      a = np.random.random(3)
      b = np.random.random((2, 3))
      
      print(a)
      # Output: [0.55022573 0.91890014 0.27154755]
      print(b)            
      # Output:
      # [[0.19379429 0.14326045 0.08705205] 
      #  [0.04354501 0.82007126 0.90535807]]
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • I know it is a kind of hard to understand, since it is related to higher dimension array. So I will post np.random.random((2, 3, 4)) result here for you to compare:
      c = np.random.random((2, 3, 4))
      
      print(c)
      # Output:
      # [[[0.34256251 0.10954162 0.59984443 0.57395688]
      #   [0.8414659  0.81810578 0.83390859 0.53966319]
      #   [0.56616342 0.17938677 0.41628244 0.28977822]]
      
      #  [[0.99204515 0.38727915 0.42309386 0.95354766]
      #   [0.19958058 0.0175912  0.91000997 0.54684459]
      #   [0.14314477 0.60824123 0.82409755 0.57325684]]]
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
    5. Create array with random int number

      • Use np.random.randint() function, in the following example:
        • 0 means the start range of the number
        • 9 means the end range of the number
        • size=(3,4) means the structure of the array
      a = np.random.randint(0, 9, size=(3,4))
      print(a)
      # Output:
      # [[4 0 5 6] 
      #  [6 8 0 7] 
      #  [6 7 4 1]]
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6

    3. How to manipulate the Array

    1. Specifying or Checking your array type

      • From the former function, we may find that we actually cannot specify the type of data, thus we can add dtype=np.{type} to specify it.
      a = np.array([1, 2, 3], dtype=np.float64)
      b = np.array([1, 2, 3], dtype=np.complex64)
      print(a)            # Output: [1. 2. 3.]
      print(b)            # Output: [1.+0.j 2.+0.j 3.+0.j]
      
      • 1
      • 2
      • 3
      • 4
      • If we get an unknown array, then how can we know its type?
      a = np.array(([1, 2, 3, 4], [5, 6, 7, 8]), dtype=np.in32)
      print(a.dtype)       # Output: int32
      
      • 1
      • 2
      • If we want to change the type:
      a = a.astype(np.float64)
      print(a.dtype)      # Output: float64
      
      • 1
      • 2
      • Here is a list of some useful type:
      Data typeDescription
      boolBool type (True/False)
      int64Integer
      uint64No symbol integer (>0)
      float64Float type number
      complex64Complex numbers
    2. Specifying or Checking your array dimension and shape

      • In former period, we have a glance at array shape when using np.random.random(), here we will explain more with array.ndim and array.shape:

        • If we create like this: np.random.random((2,3,4,5,6)), guess how many dimension this array has?
        • The answer is 5, we use np.ndim to verify:
        a = np.random.random((2, 3, 4, 5, 6))
        print(a.shape)        # Output: (2, 3, 4, 5, 6)
        print(a.ndim)         # Output: 5
        
        • 1
        • 2
        • 3
      • If you want to get more information about array, there are several guidances:

      • Here is a trick:

        • If you want to count how many elements in the array, use array.size:
        a = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
        print(a.size)           # Output: 12
        
        • 1
        • 2
      • Here is another trick:

        • If you want to flatten any array to 1D array, use array.flatten():
        a = np.array([[1,2,3],[4,5,6]])
        print(a)                # Output: [[1 2 3] [4 5 6]]
        print(a.shape)          # Output: (2, 3)
        
        b = a.flatten()
        print(b)                # Output: [1 2 3 4 5 6]
        print(b.shape)          # Output: (6,)
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
    3. Reshaping or Resizing your array

      • In last chapter we have a clear view of array structure, but whether we can change array shape without changing the data in it? The answer is yes! We will use two functions here:

        • array.reshape()
        • array.resize
      • You may wonder what are the differences of two functions? Actually they have the same feature but different ways to use, see the example here:

        a = np.random.randint(0,10,size=(3,4))
        print(a)
        # [[5 8 2 4] 
        #  [5 0 0 1] 
        #  [3 5 2 6]]
        
        b = a.reshape((2,6))
        print(b)
        # [[5 8 2 4 5 0] 
        #  [0 1 3 5 2 6]]
        
        a.resize(2,6)
        print(a)
        # [[5 8 2 4 5 0] 
        #  [0 1 3 5 2 6]]
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
      • Yes! You may find that array.reshape() return a new array while the array.resize() directly change the shape on original array, so be careful with resize which will may destroy your array.

      • I also want to remind you that when you use the reshape method, the array you want to produce needs to have the same number of elements as the original array. If you start with an array with 12 elements, you’ll need to make sure that your new array also has a total of 12 elements. (Quote from here)

    4. Add new axis(dimension) to an array

      • Here comes another question: How can we extent the dimension of any array? We use np.newaxis or np.expand_dims to solve the problem, see example here:

        • Using np.newaxis to create new dimension:
        a = np.array([1, 2, 3, 4, 5, 6])
        print(a.shape)                # Output: (6,)
        row_vector = a[np.newaxis, :]
        print(row_vector.shape)       # Output: (1, 6)
        col_vector = a[:, np.newaxis]
        print(col_vector.shape)       # Output: (6, 1)
        mul_vector = a[np.newaxis, :, np.newaxis]
        print(mul_vector.shape)       # Output: (1, 6, 1)
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • Using np.expand_dims() to create new dimension, axis={number} means the order of dimension you want to expand.
        a = np.array([1, 2, 3, 4, 5, 6])
        print(a.shape)                # Output: (6)
        b = np.expand_dims(a, axis=1)
        print(b.shape)                # Output: (6, 1) 
        c = np.expand_dims(a, axis=[0,2,3])
        print(c.shape)                # Output: (1, 6, 1, 1)
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
      • If you want to get more information about expanding axis, there are several guidances:


    4. How to index and slice the Array

    • Actually this part is quite easy since you can index and slice NumPy arrays in the same ways you can slice Python lists.
    1. If we have a array like this: a = np.array([1, 2, 3, 4, 5]):

      a = np.array([1, 2, 3, 4, 5])
      print(a[1])        # Output: 2          Choose the 2 order element.
      print(a[0:2])      # Output: [1 2]      Choose the 1 and 2 element
      print(a[2:])       # Output: [3 4 5]    Choose the element after 2
      print(a[:2])       # Output: [1 2]      Choose the element before 2
      print(a[-2:])      # Output: [4 5]      Choose the element after last 2
      print(a[:-2])      # Output: [1 2 3]    Choose the elements before last 2
      print(a[::2])      # Output: [1 3 5]    Choose all the elements with step 2
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • I have a more clear picture from official website to help you understand:

    2. If we have a more complex array like this:
      a = np.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]]])

      • Ok, right now you may say: wtf, why you give me an example like this? But the more serious problems come, please answer the outcome of these:

        print(a[0])
        print(a[0,2])
        print(a[0:2])
        print(a[[0,2]])
        print(a[[0,2],1])
        
        • 1
        • 2
        • 3
        • 4
        • 5
      • No worry, I will go though with you. First, you should have a view of how this array constructs in below picture, then we will check every kinds of indexing and slicing array mentioned above:

        图片.png

      1. a[0]: The first dimension of the array.

        print(a[0])
        # Output:
        # [[ 1  2  3  4]
        #  [ 5  6  7  8]
        #  [ 9 10 11 12]]
        print(a[0].shape)
        # Output: (3, 4)
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
      2. a[0,2]: The first dimension and its third element:

        print(a[0,2])          # Output: [ 9 10 11 12]
        print(a[0,2].shape)    # Output: (4,)
        
        • 1
        • 2
      3. a[0:2]: The first dimension to the second dimension:

        print(a[0:2])
        # Output:
        # [[[ 1  2  3  4]  
        #   [ 5  6  7  8]  
        #   [ 9 10 11 12]] 
        
        #  [[13 14 15 16]  
        #   [17 18 19 20]  
        #   [21 22 23 24]]]
        print(a[0:2].shape)
        # Output: (2, 3, 4)
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
      4. a[[0,2]]: The first and the third dimension:

        print(a[[0,2]])
        # Output:
        # [[[ 1  2  3  4]  
        #   [ 5  6  7  8]  
        #   [ 9 10 11 12]] 
        
        # [[25 26 27 28]  
        #   [29 30 31 32]  
        #   [33 34 35 36]]]
        print(a[0,2].shape)
        # Output: (2, 3, 4)
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
      5. a[[0,2],1]: The first and third dimensions and their second elements:

        print(a[[0,2],1])
        # Output:
        # [[ 5  6  7  8] 
        #  [29 30 31 32]]
        print(a[[0,2],1].shape)
        # Output:
        # (2, 4)
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
      • I really hope this example will help you have a better view of indexing and slicing the complex array!!
    3. Some tricks of getting array numbers:

      • We have an example array like this: a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
      1. Get numbers with some conditions:

        print(a[a<5])             # Output: [1 2 3 4]
        
        five_up = (a >= 5)
        print(a[five_up])         # Output: [ 5  6  7  8  9 10 11 12]
        
        two_up_ten_down = (a > 2) & (a < 10)
        print(a[two_up_ten_down]) # Output: [3 4 5 6 7 8 9]
        
        divisible_by_2 = (a % 2 == 0)
        print(a[divisible_by_2])  # Output: [ 2  4  6  8 10 12]
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
      2. Bool index:

        range_condition = (a > 9) | (a <= 3)
        print(range_condition)
        # Output:
        # [[ True  True  True False] 
        #  [False False False False] 
        #  [False  True  True  True]]
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6

    5. How to calculate the Array

    1. Addition, Subtraction, Multiplication, Division

      a = np.array([4,6], dtype=np.float32)
      b = np.array([2,5], dtype=np.float32)
      
      print(a+b)        # Output: [ 6. 11.]
      print(a-b)        # Output: [2. 1.]
      print(a*b)        # Output: [ 8. 30.]
      print(a/b)        # Output: [2.  1.2]
      print(a*2)        # Output: [ 8. 12.]
      print(b/2)        # Output: [1.  2.5]
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
    2. More useful array operations

      a = np.random.random((2, 3, 4))
      
      print(a)
      # Output: 
      # [[[0.74613068 0.50713338 0.85054524 0.94298687] 
      #   [0.77740829 0.02997798 0.63936557 0.87512259] 
      #   [0.94362238 0.69907517 0.98721314 0.15294181]]
      
      #  [[0.13813987 0.62780067 0.03182895 0.77030893] 
      #   [0.37040885 0.27097483 0.06284363 0.9514591 ]
      #   [0.06638285 0.88425577 0.34459018 0.44372085]]]
      print(a.sum())    # Output: 13.114237592310422
      print(a.min())    # Output: 0.029977983465746427
      print(a.max())    # Output: 0.9872131446108788
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
    3. Calculate array with mathematical formula

      • In neural network training, we often want to calculate the mean loss of neural network output compared with true data. We can directly calculate these array with Numpy mathematical functions.

        y_pred = np.array([0.1, 0.2, 0.3, 0.4, 0.5], dtype=np.float32)
        y_true = np.array([0., 0., 0., 1., 1.], dtype=np.float32)
        
        mse_loss = np.mean(np.sum(np.square(y_pred - y_true)))
        print(mse_loss)         # Output: 0.75
        
        • 1
        • 2
        • 3
        • 4
        • 5

    6. How to plot the Array

    • At some point, you may want to show your array with some pictures, so here we will use matplotlib to demonstrate your array. You can see more detailed instruction with this webpage.
    1. 1D array

      import matplotlib.pyplot as plt
      a = np.array([2, 1, 5, 7, 4, 6, 8, 14, 10, 9, 18, 20, 22])
      plt.plot(a)
      plt.show()
      
      • 1
      • 2
      • 3
      • 4

      图片.png

    2. 2D array

      x = np.linspace(0, 5, 20)
      y = np.linspace(0, 10, 20)
      plt.plot(x, y, 'purple') # line
      plt.plot(x, y, 'o')      # dots
      plt.show()
      
      • 1
      • 2
      • 3
      • 4
      • 5

      图片.png

    3. 3D array

      fig = plt.figure()
      ax = fig.add_subplot(projection='3d')
      X = np.arange(-5, 5, 0.15)
      Y = np.arange(-5, 5, 0.15)
      X, Y = np.meshgrid(X, Y)
      R = np.sqrt(X**2 + Y**2)
      Z = np.sin(R)
      
      ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='viridis')
      plt.show()
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10

      图片.png

    7. How to save or load the Array

    • Ok, here we comes the last part. At some point, you may find yourself want to save your array to your disk, so here are some way to do it. At here, I recommend you to save your data into csv file.
    1. Save array to .npy file

      a = np.array([1, 2, 3, 4, 5, 6], dtype=np.float32)
      np.save('test', a)
      
      b = np.load('test.npy')
      print(b)        # [1. 2. 3. 4. 5. 6.]
      
      • 1
      • 2
      • 3
      • 4
      • 5
    2. Save array to .csv file

      csv_arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
      np.savetxt('new_file.csv', csv_arr)
      load_csv_arr = np.loadtxt('new_file.csv')
      print(load_csv_arr)     # [1. 2. 3. 4. 5. 6. 7. 8.]
      
      • 1
      • 2
      • 3
      • 4
    3. Save array with pd.DataFrame

      • At here, we need to import another very useful lib: pandas
      import pandas as pd
      
      a = np.array([[-2.58289208,  0.43014843, -1.24082018, 1.59572603],
                    [ 0.99027828, 1.17150989,  0.94125714, -0.14692469],
                    [ 0.76989341,  0.81299683, -0.95068423, 0.11769564],
                    [ 0.20484034,  0.34784527,  1.96979195, 0.51992837]])
      
      df = pd.DataFrame(a)                # Change array form into dataframe.
      df.to_csv('pd.csv', index=False)    # Store dataframe in csv without index.
      data = pd.read_csv('pd.csv')        # Read csv file data.
      arr = data.to_numpy()               # Change dataframe form to array.
      
      print(data)
      # Output:
      #           0         1         2         3
      # 0 -2.582892  0.430148 -1.240820  1.595726
      # 1  0.990278  1.171510  0.941257 -0.146925
      # 2  0.769893  0.812997 -0.950684  0.117696
      # 3  0.204840  0.347845  1.969792  0.519928
      print(arr)
      # Output:
      # [[-2.58289208  0.43014843 -1.24082018  1.59572603]
      #  [ 0.99027828  1.17150989  0.94125714 -0.14692469]
      #  [ 0.76989341  0.81299683 -0.95068423  0.11769564]
      #  [ 0.20484034  0.34784527  1.96979195  0.51992837]]
      
      • 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

    Please clearly mark the source of the article in the process of reproducing it! Respect for originality and intellectual property rights, thanks!

  • 相关阅读:
    linux文件属性
    一、初始化个人简历项目
    ARP协议;DHCP协议;ICMP协议
    澳福外汇还不会超短线交易,可以了解一下混沌理论
    文件服务器审核
    深入理解Linux内核进程的管理与调度(全知乎最详细)
    2023/10/5 下午3:38:53 SCROLLINFO scrollInfo;
    ubuntu 22 Docker部署Nacos
    [04]Web前端进阶—JS伪数组
    5.第五部分 异步爬虫
  • 原文地址:https://blog.csdn.net/NicholasYTZ/article/details/126913258