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
- array (object[, dtype, copy, order, subok, ndmin])
uses array to create data. Pass in one-dimensional data to get a one-dimensional array, and pass in two-dimensional data to get a two-dimensional array.
2. Fill in the specified value
-
ones(shape[, dtype, order])
specifies the shape of the array, and the values inside are all filled with 1.
-
zeros(shape[, dtype, order])
specifies the shape of the array, and the values inside are filled with 0.
-
full(shape, fill_value[, dtype, order])
specifies the shape of the array, and the values inside are filled with fill_value
-
empty(shape[, dtype, order])
returns an array according to the specified shape, but it does not initialize (I understand that if you already have an array of the same shape and data type before the specified shape, an identical array will be generated, the first array6 below. If the specified shape has not been created before, an array filled with 0 will be returned, the second array6 below).
-
Unit matrix: Method 1: eye(N[, M, k, dtype, order])
returns a matrix whose diagonal is 1 and the rest are 0
-
Identity matrix: Method 2: identity(n[, dtype])
returns a matrix whose diagonal is 1 and the rest are 0.
identity can only return n*n matrix.
Others that can also create arrays by padding are: -
empty_like(prototype[, dtype, order, subok, ...])
Pass an array, and you will get a new array with the same shape and value type as the array. Give two examples.
You can try the rest of the methods yourself.
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.
-
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.
-
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.
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.
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 # 数组转置
Well, today's article is over here, I believe you have mastered these four methods of creating arrays.
Thank you for reading~