Basic knowledge of python data analysis - use of shape() function


shape() function in python

The shape function is a function in numpy.core.fromnumeric, and its function is to read the length of the matrix .


1. shape() input parameters

(1) When the parameter is a number, return empty
insert image description here

(2) The parameter is a one-dimensional matrix
insert image description here

(3) The parameter is a two-dimensional matrix
insert image description here


2. Determine the dimension of the array

  • There are several square brackets for a few-dimensional array
shape()中有3个数。
a = np.array([1,2])     #a.shape值(2,),意思是一维数组,数组中有2个元素。
b = np.array([[1],[2]]) #b.shape值是(2,1),意思是一个二维数组,每行有1个元素。
c = np.array([[1,2]])   #c.shape值是(12),意思是一个二维数组,每行有2个元素。
  • Use shape[0] to read the length of the first dimension of the matrix, that is, the number of rows; use shape[1] to read the length of the second dimension of the matrix, that is, the number of columns.
import numpy as np
x = np.array([[1,2,5],[2,3,5],[3,4,5],[2,3,6]])
#输出数组的行和列数
print x.shape  #结果: (4, 3)
#只输出行数
print x.shape[0] #结果: 4
#只输出列数
print x.shape[1] #结果: 3

3. The meaning of "?" in shape()

When debugging related programs, shape(?,2,3) may appear, which means that each array has 2 rows and 3 columns. The "?" in front of it represents the number of batches. If it is 1, there is 1 If it is 2, there are two, but I don't know how many there are when debugging, so it is displayed in the form of "?".

Guess you like

Origin blog.csdn.net/sodaloveer/article/details/125320740