Numpy: ndarray data types and operations

Source: Internet
Author: User
Numpy ndarray: a multidimensional array object

N-dimensional array object, which is a fast and flexible Big Data Set container. nadarry is a universal multi-dimensional homogeneous data container. That is to say, all the elements must be of the same type. Each array has a shape (indicating the size of the tuples of each dimension) and a dtype (an object used to describe the data type of the array)

Import numpy as NP # create ndarry # The simplest way to create an array is to use the Array Function, which accepts all sequential objects data1 = [6, 7.5, 1] # generate a new numpy array arr1 = NP. array (data1) arr1array ([6 ., 7.5, 8 ., 0 ., 1.])
# The nested sequence is converted into a multi-dimensional array data2 = [[1, 2, 3, 4], [5, 6, 7, 8] arr2 = NP. array (data2) arr2array ([[1, 2, 3, 4], [5, 6, 7, 8]) arr2.shape # shape two-dimensional array, each dimension has four values (2, 4) # NP. array will try to deduce a suitable data type for the newly created array and save it in a special dtype object arr1.dtypedtype ('float64') arr2.dtypedtype ('int32 ') # Except NP. in addition to array, there are some functions that can also be used to create an array # For more information, see [word meaning]. Create 10 new arrays whose names are all 0 NP. zeros (10) array ([0 ., 0 ., 0 ., 0 ., 0 ., 0 ., 0 ., 0 ., 0 ., 0.]) # For more information, see the word meaning. Create 10 new arrays, which are all 1 (NP. ones (10) array ([1 ., 1 ., 1 ., 1 ., 1 ., 1 ., 1 ., 1 ., 1 ., 1.]) NP. zeros (3, 5) array ([0 ., 0 ., 0 ., 0 ., 0.], [0 ., 0 ., 0 ., 0 ., 0.], [0 ., 0 ., 0 ., 0 ., 0.]) # Empty is not known. It returns some uninitialized spam value NP. empty (311, 2) array ([[1.22955771e-322, 9.5820.0000e-], [0.20.0000e + 000, 0.20.0000e + 000], [0.20.0000e + 000, 5.02034658e + 175], [4.66725104e-062, 4.01832345e-057], [Listen + 169, 3.20.22073e-033], [[1.47763641e + 248, 1.16096366e-028], [7.69165785e + 218, 248], [3.10139001e + 179, 9.15011178e-071], [1.13823902e-042, 1.3366723e + 165], [4.30420903e-096, 6.32299154e + 233], [[6.48224638e + 170, 5.22411352e + 257], [Listen + 180, 8.37174974e-144], [1.20.29402e + 161, 9.16651763e-072], [4.10024115e + 097, 5.06265680e-038], [3.75559122e + 126, 1.58268989e-319])
Function Description
Array Convert the input data (list, tuples, arrays, or other sequence types) to ndarray. Either deduce the dtype or explicitly specify the dtype
Asarray Converts the input to ndarray. If the input is an ndarray, no data is copied.
Arange Similar to the built-in range, but an ndarray instead of a list is returned.
Ones, ones_like Creates a full 1 Array Based on the specified shape and dtype. Ones_like takes another array as the parameter, and creates a full 1 Array Based on its shape and dtype.
Zeros, zeros_like
Empty, empty_like Create a new array and allocate only memory space without filling in any value
Eye, identity Create a matrix of N * n units in the Square (the diagonal line is 1, and the rest is 0)
# Asarray converts the input to ndarray. If the input is an ndarray, no np is copied. asarray ([, 5]) array ([1, 2, 3, 4, 5]) # array returns the input data (list, tuples, arrays, or other sequence types) convert to ndarray. Either deduce dtype or explicitly specify dtype # specify dtypearr3 = NP. array ([1, 2, 3], dtype = NP. float64) arr3.dtypedtype ('float64') arr4 = NP. array ([4, 5, 6], dtype = NP. int32) arr4.dtypedtype ('int32 ')

Numpy Data Type

Type Type Code Description
Int8, uint I1, u1 Signed and unsigned 8-digit integer
Int16, uint16 I2, U2 Signed and unsigned 16-bit integer
Int32, uint32 I4, U4
Int64, uint64 I8, u8
Float16 F2 Half-precision floating point number
Float32 F4 or F Standard double-precision floating point number
Float64 F8 or d Standard double-precision floating point number
Float128 F16 or G Extended Floating Point Number
Complex64, complex128, and complex256 C8, C16, c32 The plural number expressed by two 32-bit, 64-bit, or 128-bit floating point numbers, respectively.
Object O Python object type
String _ S Fixed Length string type (1 byte per character). For example, to create a string with a length of 10, use S10
Unicode _ U Unicode type with fixed length (U10)
# Explicitly convert dtypearr6 = NP. array ([1, 2, 3, 4, 5]) dtype ('int32 ') arr6.dtypedtype ('float64') # If you convert a floating point number to an integer, then the fractional part is truncated. arr8 = NP. array ([3.7,-1.2, 2.6, 0.5, 12.9, 10.1]) arr8.dtypedtype ('float64') arr9 = arr8.astype (NP. int32) arr9.dtypearr9array ([3,-1, 2, 0, 12, 10]) # If a string array represents all numbers, it can also be converted to the numerical form arr10 = NP. array (['1. 25', '-9.6', '42'], dtype = NP. string _) arr10array ([B '1. 25', B '-9.6', B '42'], dtype = '| S4') arr11 = arr10.astype (NP. float32) arr11array ([1.25,-9.6, 42.], dtype = float32) # Another use of array dytpe int_array = NP. arange (10) int_arrayarray ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) calibers = NP. array ([. 22 ,. 270 ,. 357 ,. 44,50], dtype = NP. float64) # copy int_array.astype (calibers. dtype) # Call astype to create a new array (a copy of the original data) in any case, even if the dtype is the same as the old dtype, array ([0 ., 1 ., 2 ., 3 ., 4 ., 5 ., 6 ., 7 ., 8 ., 9.])
Calculation Between arrays and scalar

The meaning of arrays is that you can complete batch data operations without writing loops. This is usually called vectoring.

Any operation between arrays of the same size applies the operation to the element level.

The operation between arrays of different sizes is called broadcast.

arr12 = np.array([[1.,2.,3.],[4.,5.,6.]])arr12array([[1., 2., 3.],       [4., 5., 6.]])arr12 * arr12array([[ 1.,  4.,  9.],       [16., 25., 36.]])arr12 - arr12array([[0., 0., 0.],       [0., 0., 0.]])1 / arr12array([[1.        , 0.5       , 0.33333333],       [0.25      , 0.2       , 0.16666667]])arr12 ** 0.5array([[1.        , 1.41421356, 1.73205081],       [2.        , 2.23606798, 2.44948974]])

Numpy: ndarray data types and operations

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.