NumPy Array Creation Routines

Source: Internet
Author: User

1 Numpy.empty
Empty (shape[, dtype=float, order= ' C ')

To create an uninitialized array that specifies shape and dtype

return: Ndarray.

Description: Order = ' C ' or ' F ' C ' is a C-style array by row, ' F ' is a Fortran-style array by column.

Import= Np.empty ((3,3), dtype = int)print(a)

Run

[[   6553665    7471204    7536741] [   4587635    7143521    7077993] [       120 1702126437 1970217060]]

Note:

An empty array is created at random, and the element is not initialized, so it may run faster than zeros and ones, but he needs the user to manually set all the values in the array and use it with caution.

2 Numpy.zeros
Zeros (shape[, dtype=float, order= ' C ')

Returns an array of element 0, shape, and data type Dtype

ImportNumPy as NPA= Np.zeros (3)Print(a)#[0.0. 0.]b= Np.zeros ((3,), dtype=int)Print(b)#[0 0 0]C= Np.zeros ((2,2), dtype='C8')Print(c)#[[0.+0.J 0.+0.J]#[0.+0.J 0.+0.J]]D= Np.zeros ((2,), dtype=[('x','I4'),('y','I4')]) #自定义类型Print(d)#[(0, 0) (0, 0)]Print(D.dtype)#[(' X ', ' <i4 '), (' Y ', ' <i4 ')]
3 Numpy.ones
Ones (shape[, dtype=float, order= ' C ')

Returns an array of element 1, shape, and data type Dtype

ImportNumPy as NPA= Np.ones (2)Print(a)#[1.1.]b= Np.ones ((2,1), dtype='i8')Print(b)#[[1]#[1]]s= (2,2) C=Np.ones (s)Print(c)#[1.1.]#[1.1.]
4 Numpy.eye
Eye (n[, M=none, k=0, Dtype=<class ' float '), order= ' C '])

In fact, a matrix of rows and columns equal to n is called an n-order matrix or an n-order phalanx, and the elements on the main diagonal are 1 of a square matrix called the unit or unit array.

N-Number of rows in the matrix

M-The number of columns of the matrix, if none, the default is N column

K-Index of the diagonal, 0 (default) main diagonal, positive diagonal, negative diagonal

Import= Np.eye (2)print(a)#  [[1. 0.]#  [0. 1.]]B = np.eye (3,k=-1,dtype=int)print(b)#  [[0 0 0]  #  [1 0 0]#  [0 1 0]]

5 Numpy.full

Full (shape, fill_value[, Dtype=none, order= ' C ')

Returns an array of element values of Fill_value (scalar), shape, and data type Dtype

Import= Np.full ((2,2), np.inf)print(a)#  [[inf inf]#    [inf INF]]= Np.full ((2,2), 5)print(b)# [ [5 5]#  [5 5]]

Note:

INF-value (numeric), infinity (Infinity) value

In the numpy-core-numeric.py

# Set the default values  = inf = Infty = Infinity == NaN == = Bool_ (True)
Note: INF-positive infinity, float type-inf-negative infinity, float type
Import= np.infprint#infc = b>9999999999999999999999  Print#True

参考:NumPy array creation routines and official documents (Numpy-ref-1.14.5.pdf) 3.1.1 Ones and Zeros

NumPy Array Creation Routines

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.