Data Analysis Learning Notes (i)--numpy: creation and type conversion of array objects

Source: Internet
Author: User
creating an Array object
function Description
Arange A range similar to Python's built-in, but returns a ndarray instead of a list
Array Converts the entered data (list, Ganso, array, or other sequence type) to Ndarray. Either infer the Dtype or display the specified dtype. Direct replication of data entered by default
Asarray Converts the input data to Ndarray and does not replicate if the input itself is a Ndarray
Ones, Ones_like Creates an array of all 1 based on the specified shape and dtype. Ones_like takes another array as a parameter and creates an array of all 1 based on its shape and dtype
Zeros, Zeros_like Similar to ones and Ones_like, but produces an array of all 0.
Empty, Empty_like Creates a new array, allocates only memory space but does not populate any values
Eye, identity Create a square nxn unit matrix (Diagonal Lane is 1, the rest is 0)
properties, methods of array objects
Properties/Methods Description
Ndim Dimension of
Size Number of elements
Shape The shape of an array
ItemSize Bytes per element
Nbytes Total bytes
Real For the real part of the complex number type
Imag For the imaginary part of a complex number type
Dtype The type used to specify or output an array object element
Reshape Split number composition specified shape: Data.reshape (2,3,4)
Astype Type conversions: Arr.astype (Np.float64)
some chestnutsTo create a one-dimensional array
A = one-dimensional array of Np.arange (24) # 0-23
Output some information
Print (a)        # output Array
print (a.dtype)  # Output Type
print (a.shape)  # Output Type
output:
[0  1  2  3  4  5  6  7  8
(9 Int64
) (24, in km).
To create a multidimensional array
# Converts the one-dimensional array a previously created to 2 dimensions, 3 rows 4 array
b = A.reshape (2,3,4)
print (b)        # output Array
print (b.dtype)  # Output Type
print (b.shape)  # Output Type
output: [[
[0  1  2  3]
  [4  5 6 7  ]
  [8  9]]

 [[EUR]
  [A]
  [O]]]
Int64
(2, 3, 4)
Array to create a one-dimensional array
Np.array ([0,1,2,3]) # Equal to Np.arange (4)
Array to create a multidimensional dimension array
Np.array ([Np.arange (4), [4,5,6,7]])
Create an array of all 0/1
Np.ones (10) # Creates a 1-dimensional one-dimensional array [1. 1.1. 1.1. 1.1. 1.1. 1.]
Np.ones ((2,3,4))  # Create multidimensional array
output: [[
[1]. 1.1. 1.]
  [1. 1.1. 1.]
  [1. 1.1. 1.]]

 [[1. 1.1. 1.]
  [1. 1.1. 1.]
  [1. 1.1. 1.]]]

Np.zeros    # creates a 0-dimensional array of one-dimensional arrays: [0.0. 0.0. 0.0. 0.0. 0.0.]
Np.zeros (4,4) # creates a 0-dimensional array
output:
[[0]. 0.0. 0.]
 [0. 0.0. 0.]
 [0. 0.0. 0.]
 [0. 0.0. 0.]]
The properties of the array
A = Np.arange. Reshape (2,3,4)
A.ndim   # dimension    3 a.size # Number of
a.itemsize   # Bytes per element     8
a.nbytes     # Total bytes              (24*8)
a.dtype      # element type             Int64
A.flat       # <numpy.flatiter Object at 0x10214b600>
[item to item in A.flat]    # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ,
a.flat[2]    # 2 # of the
complex array
b = Np.array (Np.arange (4), dty, of the number of, at, the Pe=np.complex) 
b.real       # [0. 1.2. 3.]
b.imag       # [0. 0.0. 0.]
Data Type
type Type Code Description
int8, Uint8 I1, U1 Signed and unsigned 8-bit (1-byte) integer
Int16, UInt16 I2, U2 Signed and unsigned 8-bit (2-byte) integer
Int32, UInt32 I4, U4 Signed and unsigned 8-bit (4-byte) integer
Int64, Uint8 i8, U8 Signed and unsigned 8-bit (8-byte) integer
Float16 F2 Semi-precision floating-point numbers
Float32 F4 or F Standard single-precision floating-point number. Float compatible with C
Float64 F8 or D Standard single-precision floating-point number. Compatible with C's double and Python float objects
float128 F16 or G Extended precision floating-point numbers
Complex64, complex128, complex258 C8, C16, C32 Complex numbers represented by two 32-bit, 64-bit, 128-bit floating-point numbers, respectively
bool ? Boolean type that stores ture and false values
Object O Python Object type
String_ S Fixed-length string type (1 bytes per character), for example, to create a string of length 10, you should use the S10
Unicode_ U Fixed-length Unicode type (the number of bytes is determined by the platform). And the way the string is defined, such as U10
some chestnutsConversion Type 1
Np.float64 (a)  is converted to a floating-point type    .
Np.int (13.2)    # Convert to integral type
Np.bool (12.3)   # Convert to Boolean    True
Np.complex (3)   # Convert to Complex Type   (3 + 0J)
Np.int ((2+3j))  # cannot convert a complex number to an integral type or a floating-point type
Conversion Type 2
# Create an example array
a = Np.arange (1,5)
print (A.dtype)          # The array type is: Int64
# converts it to a floating-point type
float_a = A.astype ( Np.float32)  
print (Float_a.dtype)    # float32
# converts it to an integral type
int_a = Float_a.astype (np.int8)
print (int _a.dtype)      # int8
# Create character array
numeric_strings = Np.array ([' 1.25 ', ' -9.6 ', ', '], dtype= ')
# Convert it to floating-point type
numeric_strings = Numeric_strings.astype (np.float)
print (Numeric_strings.dtype)    # float64
Specify type
A = Np.arange (4,dtype=np.float) # Specifies that its type is float
print (a.dtype)  # output: float64
Properties of the Dtype class
# Dtype class's properties
t = Np.dtype (' d ')
print (T.char)   # D, that is, by specifying ' d ' to get the type float64
print (t.type)   # < Class ' Numpy.float64 ' >
print (T.STR)    # <f8
Data type as a number of bytes
A = Np.array ([[1,2],[3,4]])
To create a custom data type
# customize a new type MyType = Np.dtype ([(' Name ', Np.string_,], (' Age ', Np.int32), (' Height ', np.float) ] Print (MyType) # [(' Name ', ' S40 '), (' Age ', ' <i4 '), (' Height ', ' <f8 ')] # Create an array of arrays = Np.array using the new type ([' Lolita01 (' Luo ', 170.0, 165.3), Dtype=mytype) # remove No. 0 print (array[0]) # (b ' lolita0164 ', 25, 170.) # Take out the No. 0, ' name ' The value of the property print (array[0][' name ']) # b ' lolita0164 ' 

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.