SoFunction
Updated on 2025-03-02

What does np do 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 as "np".

Demo:

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

turn out:

[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, that is, all elements in it must be of the same type. Each array has a shape and a dtype.

shape is both an array shape, for example

import numpy as np
from  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]]


(3, 4)

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

The following function can be used to create an array

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

This is the end of this article about what np does in python. For more related content about what np is in python, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!