The numpy package in python USES an array of tutorials and related operations

  • 2020-06-12 09:56:48
  • OfStack

preface

Numpy (Numerical Python) is an Python package for scientific calculation of the third square.

NumPy provides advanced numerical programming tools such as matrix data types, vector processing, and sophisticated libraries. Produced for rigorous numerical processing. The following article will describe the array of numpy package usage tutorials and related operations in python in detail. Without further discussion, let's start with the detailed introduction:

1. Introduction to arrays

The most important data structure in Numpy is the multidimensional array type ( numpy.ndarray )

ndarray consists of two parts:

Data actually held; Metadata describing this data (metadata)

The dimensions of an array (that is, a matrix) are called axes, and the dimensions rank

Important attributes of ndarray include:

ndarray.ndim : The dimension of the array, also known as rank ndarray.shape : The size of each dimension of the array, for a matrix with n row m column shape is (n,m) ndarray.size : Total number of elements. ndarray.dtype : The type of each element can be ES46en.es47EN32, ES48en.int16, and ES51en.float64, etc ndarray.itemsize : Number of bytes per element. ndarray.data : Points to data memory.

2. Use of arrays

To import the module before using numpy, import the module using the following statement:


improt numpy as np # Among them np for numpy Is the nickname of 1 Kind of idiom  

1. Generate the array using array method

array, also known as array, is the most basic data structure in numpy. The most critical attributes are dimension and element types. In numpy, it is very convenient to create various types of multidimensional arrays and perform 1 basic operation.
Generated as an array with list or tuple variables:


>>> print np.array([1,2,3,4]) 
[1 2 3 4] 
>>> print np.array((1.2,2,3,4)) 
[ 1.2 2. 3. 4. ] 

Take list or tuple variables as elements to generate 2-dimensional arrays or multidimensional arrays:


>>> x = np.array(((1,2,3),(4,5,6))) 
>>> x 
array([[1, 2, 3], 
 [4, 5, 6]]) 
>>> y = np.array([[1,2,3],[4,5,6]]) 
>>> y 
array([[1, 2, 3], 
 [4, 5, 6]]) 

2. Generate the array using the numpy.arange method


>>> print np.arange(15) 
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14] 
>>> print type(np.arange(15)) 
<type 'numpy.ndarray'> 

3. Generate special matrices (arrays) using built-in functions

The matrix zero


>>> print np.zeros((3,4)) 
[[ 0. 0. 0. 0.] 
 [ 0. 0. 0. 0.] 
 [ 0. 0. 0. 0.]] 

1 matrix


>>> print np.ones((3,4)) 
[[ 1. 1. 1. 1.] 
 [1. 1. 1. 1.] 
 [ 1. 1. 1. 1.]] 

Unit matrix


>>> print np.eye(3) 
[[ 1. 0. 0.] 
 [0. 1. 0.] 
 [ 0. 0. 1.]] 

4. Index and slice


>>> x = np.array(((1,2,3),(4,5,6))) 
>>> x[1,2] # For the first 2 Line first 3 The number listed  
6 

>>> y=x[:,1] # For the first 2 column  
>>> y 
array([2, 5]) 

With python grammar 1, no more examples.

5. Get array properties


>>> a = np.zeros((2,2,2)) 
>>> print a.ndim # The dimension of the array  
3 
>>> print a.shape # Each array 1 The size of the d  
(2, 2, 2) 
>>> print a.size # The number of elements in an array  
8 
>>> print a.dtype # The element type  
float64 
>>> print a.itemsize # The number of bytes per element  
8 

6. Array transformation

Multi-dimensional conversion to 1-dimensional:


>>> print np.array([1,2,3,4]) 
[1 2 3 4] 
>>> print np.array((1.2,2,3,4)) 
[ 1.2 2. 3. 4. ] 
0

1 dimensional conversion to multi-dimensional:


>>> print np.array([1,2,3,4]) 
[1 2 3 4] 
>>> print np.array((1.2,2,3,4)) 
[ 1.2 2. 3. 4. ] 
1

Transpose:


>>> print np.array([1,2,3,4]) 
[1 2 3 4] 
>>> print np.array((1.2,2,3,4)) 
[ 1.2 2. 3. 4. ] 
2

7. Array composition

Horizontal combination:


>>> y=x 
>>> numpy.hstack((x,y)) 
array([[1, 2, 3, 1, 2, 3], 
  [4, 5, 6, 4, 5, 6]] 

The vertical combination


>>> print np.array([1,2,3,4]) 
[1 2 3 4] 
>>> print np.array((1.2,2,3,4)) 
[ 1.2 2. 3. 4. ] 
4

The concatenate function allows you to do both by specifying the axis parameter, which defaults to 0 and is combined vertically.


>>> print np.array([1,2,3,4]) 
[1 2 3 4] 
>>> print np.array((1.2,2,3,4)) 
[ 1.2 2. 3. 4. ] 
5

8. Split the array

Vertical segmentation


>>> print np.array([1,2,3,4]) 
[1 2 3 4] 
>>> print np.array((1.2,2,3,4)) 
[ 1.2 2. 3. 4. ] 
6

Horizontal partitioning


>>> print np.array([1,2,3,4]) 
[1 2 3 4] 
>>> print np.array((1.2,2,3,4)) 
[ 1.2 2. 3. 4. ] 
7

You can achieve both effects with the split function by setting its axis parameter distinction, similar to the combination, which is not demonstrated here.

3. The matrix

As you can see from the array manipulation above, numpy can simulate the matrix through the array, but numpy also has a data structure dedicated to dealing with the matrix, matrix.

1. Generate matrix


>>> numpy.mat('1 2 3;4 5 6;7 8 9') 
matrix([[1, 2, 3], 
  [4, 5, 6], 
  [7, 8, 9]]) 

2. Array matrix conversion

Matrix transformation array


>>> print np.array([1,2,3,4]) 
[1 2 3 4] 
>>> print np.array((1.2,2,3,4)) 
[ 1.2 2. 3. 4. ] 
9

Array transition matrix


>>> n=numpy.array(m) 
>>> numpy.mat(n) 
matrix([[1, 2, 3], 
  [4, 5, 6], 
  [7, 8, 9]]) 

3. Matrix method

Inverse:


>>> m.I 
matrix([[ -4.50359963e+15, 9.00719925e+15, -4.50359963e+15], 
  [ 9.00719925e+15, -1.80143985e+16, 9.00719925e+15], 
  [ -4.50359963e+15, 9.00719925e+15, -4.50359963e+15]]) 

conclusion


Related articles: