Four types of methods commonly used to create numpy arrays

When it comes to python's third-party extension library for data analysis, we have to mention numpy and pandas. Today we will learn how to create arrays in numpy.

numpy create array

1. Existing data filling

2. Fill in the specified value

3. Create an array with a range of values

  • arange ([start,] stop[, step,][, dtype])
    returns evenly spaced values ​​within the given interval.
    The value range obtained by arange is a range of left-closed and right-open. If only a maximum value is given, the default value range is from 0 to the maximum value.

    If you want to get an array of negative numbers, you can't get it with only one stop value.
    insert image description here

  • linspace(start, stop[, num, endpoint, …])
    returns an arithmetic array within the specified interval.
    The obtained array is within the given value range (closed on the left and closed on the right), and it will be automatically divided into an array of equal differences according to the number you need.
    insert image description here

  • logspace(start, stop[, num, endpoint, base, ...])
    returns a geometric sequence with base 10 by default. The array determines the proportional ratio according to the num value you need.
    insert image description here

4. Create a diagonal matrix

  • diag(v[, k])
    passes in the diagonal array, and according to the number of elements n of the incoming diagonal array, an n*n array is created correspondingly, and the rest of the diagonal is filled with 0. If the input is all 1, it is equivalent to the identity matrix.
    insert image description here

For other methods of creating numpy arrays, please refer to the official documentation ( https://www.numpy.org.cn/reference/routines/array-creation.html ).

Introduction to common attributes

array1 = np.array([[1,2],[3,4],[5,6]])

array1.shape #数组的维度
array1.size # 数组中的元素个数
array1.dtype # 数组中元素的数据类型
array1.ndim # 数组维数
array1.itemsize # 一个数组元素的长度,以字节为单位
array1.nbytes # 数组元素消耗的总字节数
array1.T # 数组转置

insert image description here
Well, today's article is over here, I believe you have mastered these four methods of creating arrays.
Thank you for reading~

Guess you like

Origin blog.csdn.net/youzi85/article/details/128592792