Home  >  Article  >  Backend Development  >  Understand what np does in python

Understand what np does in python

coldplay.xixi
coldplay.xixiforward
2020-07-23 16:58:174383browse

Understand what np does in python

In python, "np" generally refers to the "numpy" library, which is an alias for the third-party library "numpy". Method: Use the command "import numpy as np" to alias the numpy library to "np".

Demonstration:

import numpy as np
arr = np.array([1, 2, 3])
print(arr)

The result is:

[1 2 3]

Knowledge point expansion:

Basic use of NumPy in Python

ndarray (hereinafter referred to as array) is an array object of numpy. It should be noted that it is isomorphic, which means that all elements in it must be of the same type. Each of these arrays has a shape and dtype.

Shape is the shape of the array, such as

import numpy as np
from numpy.random import randn

arr = randn(12).reshape(3, 4)

arr

[[ 0.98655235 1.20830283 -0.72135183 0.40292924]
 [-0.05059849 -0.02714873 -0.62775486 0.83222997]
 [-0.84826071 -0.29484606 -0.76984902 0.09025059]]

arr.shape
(3, 4)

(3, 4) means that arr is an array with 3 rows and 4 columns, and the dtype is float64

The following function can Used to create an array

array  Convert the input data to ndarray, the type can be specified or defaulted
asarray  Convert the input to ndarray
arange Similar to the built-in range
ones, ones_like Create an array of all 1s based on the shape, which can copy the shape of other arrays
zeros, zeros_like Similar to the above, all 0
empty, empty_like Create a new array, allocate only space
eye,identity Create pair Diagonal matrix with diagonal line 1

Related learning recommendations: python video tutorial

The above is the detailed content of Understand what np does in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete