Home >Backend Development >Python Tutorial >Numpy attributes and matrix creation in python

Numpy attributes and matrix creation in python

不言
不言Original
2018-09-08 16:56:532256browse

The content of this article is about the attributes and creation matrix of Numpy in python. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

ndarray.ndim: Dimension

ndarray.shape: Shape

ndarray.size: Number of elements

ndarray.dtype: Element data type

ndarray.itemsize: byte size

Create array:

a = np.array([2,23,4])  
# list 1d
print(a)
# [2 23 4]

Specify data type:

a = np.array([2,23,4],dtype=np.int)
print(a.dtype)
# int 64

dtype The types that can be specified are int32, float, float32, If not followed by a number, the default is 64

a = np.zeros((3,4)) # 数据全为0,3行4列
"""
a = np.ones((3,4),dtype = np.int)   # 数据为1,3行4列
a = np.empty((3,4)) # 数据为empty,3行4列

empty type: the initial content is random, depending on the state of the memory

a = np.arange(10,20,2) # 10-19 的数据,2步长
a = np.arange(12).reshape((3,4))    # 3行4列,0到11

reshape modifies the data shape, such as 3 rows and 4 columns

a = np.linspace(1,10,20)    # 开始端1,结束端10,且分割成20个数据,生成线段

linspace The amount of data can be determined, but arrage cannot determine the amount of data. At the same time, linspace can also use reshape to define the structure.

Related recommendations:

How to create a symmetric matrix in Python based on the numpy module

How does Python numpy extract the specified rows and columns of the matrix?

The above is the detailed content of Numpy attributes and matrix creation in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn