Python extension library numpy learning

Introduction to numpy

NumPy is an extension library of the Python language, which supports a large number of dimensional array and matrix operations, and also provides a large number of mathematical function libraries for array operations. It integrates C/C++ code tools, and is very similar to Matlab when used , and also includes linear algebra, Fourier transform, random number generation and other functions.

Before using the numpy library you need to install the numpy library:

Windows pressWin+rentercmdenter afterpip install numpyWaiting for the download and installation to completeSuccessfully collected pacges:numpyIndicates success.

Or you can import the numpy module and generate a 4*4 diagonal matrix for testing:

>>> import numpy
 >>> numpy . eye ( 4 ) 
array ( [ [ 1 . , 0 . , 0 . , 0 . ] , [ 0 . , 1 . , 0 . , 0 . ] , [ 0 . , 0 _  _ . , 1 . , 0 . ] , [ 0   
          
          
       . ,  0 . ,  0 . ,  1 . ] ] )
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Introduction to numpy ndarray objects

One of the most important features of NumPy is its N-dimensional array object ndarray, which is a collection of a series of data of the same type , starting with 0 subscripts to index elements in the collection.

Create an ndarray:

numpy .array ( object , dtype = None , copy = True , order = None , subok = False , ndmin = 0 ) _     
  • 1

Parameter Description:

namedescribe
objectarray or nested array
dtypedata type of array elements
copyWhether the object needs to be copied
orderThe style to create the array, C is the row direction, F is the column direction, A is any direction (default)
subokReturns an array of the same type as the base class by default
ndminSpecify the minimum dimension of the resulting array

Let's modify and test some of the above parameters:

>>> import numpy
 >>> numpy.array ( [ 1 , 2 , 3 , 4 , 5 ] ) 
array ( [ 1 , 2 , 3 , 4 , 5 ] ) >>> numpy .array ( [ 1 , 2 , _  _ _ _ _ _ 3 , 4 , 5 ] , dtype = str )    
      #Set the data type to string, or change str to '<U1' 
array ( [ '1' ,  '2' ,  '3' ,  '4' ,  '5' ] , dtype = '<U1' ) 
>> > numpy . array ( [ 1 , 2 , 3 , 4 , 5 ] , dtype =  complex )    # set to complex type 
array ( [ 1 . + 0.j ,  2 . + 0.j , 3 . + 0.j ,  4 . + 0.j ,  5 . + 0.j ] ) 
>>> b = numpy . array ( [ 1 , 2 , 3 , 4 , 5 ] , order = 'F ' , ndmin = 3 ) #set dimension to 3 >>> b    

array ( [ [ [ 1 ,  2 ,  3 ,  4 ,  5 ] ] ] ) 
>>> b = numpy . array ( [ [ 1 , 2 , 3 , 4 , 5 ] , [ 6 , 7 , 8 ] ] ) > _ > > print ( b ) [ list ( [ 1
 
,  2 ,  3 ,  4 ,  5 ] )  list ( [ 6 ,  7 ,  8 ] ) ]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

numpy data types

Numpy data types are much more than python's built-in data types. The commonly used ones are:

  • bool_ : Boolean data type
  • int_ : the default integer type
  • float_ : Double precision floating point number, equal to float64
  • complex_: shorthand for complex128 type, 64-bit floating point number

Among them, the int type also has int8, int16, int32, int64, uint8, uint16, uint32, uint64

Among them, float types also include float16, float32, and float64

The complex types include complex64, complex128

data type object (dtype)

The data type object is used to describe how the memory area corresponding to the array is used, which depends on the following aspects:

  • type of data
  • the size of the data
  • The byte order of the data (little endian or big endian, **"<" means little endian (the minimum value is stored at the smallest address, that is, the low-order group is placed first). ">" means big endian (The most significant byte is stored at the smallest address, i.e. the high order group is first.) **Like the < in '<U1' we used in the previous code.
  • In the case of structured types, the name of the field, the data type of each field, and the portion of the memory block each field takes

Built-in type character codes:

characterCorresponding type
bboolean
i(signed) integer
uunsigned integer integer
ffloating point
ccomplex float
mtimedelta (time interval)
Mdatetime (datetime)
S, a(byte-) string

Example 2:

The four data types of int8, int16, int32, int64 can be replaced by strings 'i1', 'i2','i4','i8'

>>> numpy.dtype ( ' i4' ) dtype ( 'int32' ) >>> numpy.dtype ( ' i8 ' ) dtype ( ' 
int64 ' ) >>> 
numpy.dtype ( ' i1 ' ) dtype ( 
' int8 ' ) >>> numpy.dtype ( ' i2' ) dtype 
( ' int16 ' )



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Example 3: Use of structured data types

import numpy as np

a = np . dtype ( [ ( 'age' , np . int8 ) ] ) 
b = np . array ( [ ( 10 , ) , ( 20 , ) , ( 30 , ) ] , dtype = a ) 
print ( b [ ' age' ] )

'''
[10 20 30]
'''
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

Example 4: Define a structured data type car, including a string field license, an integer field value, and a floating point field distance

import numpy as np

car = np.dtype([('licence','S20'),('value','i4'),('distance','f4')])
cars = np.array([('AE86',15000,3452.23),('S666',12000,1232.933),('XX00',999,520.521)])
print(cars)


'''
[['AE86' '15000' '3452.23']
 ['S666' '12000' '1232.933']
 ['XX00' '999' '520.521']]
'''
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

Properties of numpy arrays

The dimension of a NumPy array is called rank. The rank is the number of axes, that is, the dimension of the array. The rank of a one-dimensional array is 1, and the rank of a two-dimensional array is 2.

Unfinished to be updated…

Tags: Python extension library numpy learning

Topics in Python python

Related: Python extension library numpy learning