1.Numpy方法初探
>>> import numpy as np
>>> # Create a length-10 integer array filled with zeros
>>> np.zeros(10, dtype=int)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
>>> # Create a 3x5 floating-point array filled with 1s
>>> np.ones((3, 5), dtype=float)
array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
>>> # Create a 3x5 array filled with 3.14
>>> np.full((3, 5), 3.14)
array([[3.14, 3.14, 3.14, 3.14, 3.14],
[3.14, 3.14, 3.14, 3.14, 3.14],
[3.14, 3.14, 3.14, 3.14, 3.14]])
>>> # Create an array filled with a linear sequence
>>> # Starting at 0, ending at 20, stepping by 2
>>> # (this is similar to the built-in range() function)
>>> np.arange(0, 20, 2)
array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
>>> # Create an array of five values evenly spaced between 0 and 1
>>> np.linspace(0, 1, 5)
array([0. , 0.25, 0.5 , 0.75, 1. ])
>>> # Create a 3x3 array of uniformly distributed
>>> # random values between 0 and 1
>>> np.random.random((3, 3))
array([[0.79874333, 0.60991337, 0.29362395],
[0.18324684, 0.09414009, 0.05824703],
[0.67576864, 0.31021357, 0.97921232]])
>>> # Create a 3x3 array of normally distributed random values
>>> # with mean 0 and standard deviation 1 均值0,方差1
>>> np.random.normal(0, 1, (3, 3))
array([[-0.92506352, -0.58967019, 0.72873444],
[-0.50914721, 0.82898162, -0.93988454],
[-1.91362691, -0.39273236, -0.19881052]])
>>> # Create a 3x3 array of random integers in the interval [0, 10)
>>> np.random.randint(0, 10, (3, 3))
array([[3, 3, 7],
[6, 4, 0],
[2, 2, 5]])
>>> # Create a 3x3 identity matrix 恒等方阵
>>> np.eye(3)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
>>> # Create an uninitialized array of three integers
>>> # The values will be whatever happens to already exist at that memory location
>>> np.empty(3)
array([1., 1., 1.])
numpy.
empty
(
shape,
dtype=float,
order='C'
)
Return a new array of given shape and type, without initializing entries.
Parameters: | shape : int or tuple of int
dtype : data-type, optional
order : {‘C’, ‘F’}, optional
|
---|---|
Returns: | out : ndarray
|