SoFunction
Updated on 2024-11-16

Getting Started with NumPy for Scientific Computing in Python Tutorials

preamble

NumPy is an extremely fast math library for Python for working with large matrices. It allows you to do vector and matrix arithmetic in Python, and with many of the underlying functions written in C, you'll get a speed of operation that you wouldn't be able to achieve in regular Python. This is due to the fact that every element in a matrix has the same data type, which also reduces type detection during operations.

matrix foundation

In the numpy package we use arrays to represent vectors, matrices and higher order data structures. They are then composed of arrays, one dimension is represented by an array, and two dimensions are represented by arrays containing arrays within arrays.

establish

# coding: utf-8
import numpy as np

a = ([
 [1.73, 1.68, 1.71, 4],
 [1, 2, 3, 4],
 [1, 2, 3, 4]
])
print type(a) # <type ''>

ndarray (N-dimensional array object) means an n-dimensional array. The example represents a two-dimensional array with 3 rows and 4 columns.

geometry

The size of the array can be obtained from its shape attribute:

print  # (3L,4L)

The number of elements in an array can be obtained by

print  # 12

Using the dtype attribute of ndarray we can get the type of the array elements:

print  # float64

You can reshape the matrix using shape or create a new array with changed dimensions using the reshape method, leaving the original array's shape unchanged.

 = 4, 3
b = ((2, 6))
# (go ahead and do it) without hesitatingbThe shape of the new,stillacap (a poem)bis the shared data storage memory area of the,in the event thatb[0][1] = 8 in that casea[0][1] It will also be8

Array Generation

You can use it. to create an array with parameters similar to range:

x = (0, 10, 1) # arguments: start, stop, step

It is also possible to create an equivariant series using.

x = (1, 10, 5) # arguments: start, stop, num number of elements
# [ 1. 3.25 5.5 7.75 10. ]

#  is to create an isometric series

matrix operation

The computation involves the variable directly in the operator, with no change in operator precedence:

a = (5, 5)
b = (5, 5)

print a + b
print a - b
print a * b
print a / b
print a ** 2
print a < b
print a > b

An array that contains nothing butdot() function, these other operations are unary operations.

np_arr = ([2,3,34,5,5])
print (np_arr) # Average
print (np_arr) # Median
print (a[0], a[1]) # Determine if the data in the two axes are correlated
print (np_arr) # (statistics) standard deviation

data extraction

Slice index syntax:M[lower:upper:step]

a = ([1,2,3,4,5])
a[1:3] # array([2, 3])

# When making a slice assignment, the original array is modified
a[1:3] = [-2, -3] # array([ 1, -2, -3, 4, 5])


b = (5, 5)
b[1:4, 1:4] # Extract 1~4 rows, 1~4 columns

b > 0.1 #array([False, False, False, ...])
# So to extract it can be used, it's utilizing the boolean masking feature #
b[ b > 0.1 ]

The # where() function is another useful way to retrieve array elements when it is necessary to retrieve them with a specific condition. Simply pass it a condition and it will return a list of elements that match the condition.
c = (b > 0.1)

matrix operation

NumPy and Matlab is not the same, for multi-dimensional arrays of arithmetic, the default case does not use matrix operations, if you want to matrix arrays, you can call the corresponding function.

matrix object

The numpy library provides the matrix class, which creates matrix objects whose addition, subtraction, multiplication, and division operations are computed by default using the matrix method, so the usage is very similar to that of matlab. However, since there are both ndarray and matrix objects in NumPy, it is easy to confuse them. This goes against Python's principle of "explicit over implicit", so it is not recommended to use matrix in more complex programs.

>>> a = ([[1,2,3],[5,5,6],[7,9,9]])
>>> a*a**-1
matrix([[ 1.00000000e+00, 1.66533454e-16, -8.32667268e-17],
  [ -2.77555756e-16, 1.00000000e+00, -2.77555756e-17],
  [ 1.66533454e-16, 5.55111512e-17, 1.00000000e+00]])

Converting from an array to a matrix can be done with them = (a) The transpose matrix of m can be obtained by converting

Matrix inverse

 * m
=> matrix([[ 1.00000000e+00+, 4.44089210e-16+],
   [ 0.00000000e+00+, 1.00000000e+00+]])

Shallow vs. deep copy

For high performance, assignments in Python often do not copy the underlying object, which is called shallow copying. Deep copies are made using copy:

b = copy(a)

Iterate over the elements of the array

Normally, we want to avoid iterating over array elements whenever possible. That's because iteration is much slower than vectorization. But there are times when iteration is unavoidable, and Python's for is the most convenient way to do it:

v = ([1,2,3,4])

for element in v:
 print(element)

M = ([[1,2], [3,4]])

for row in M:
 print("row", row)
 for element in row:
  print(element)

summarize

The above is all about Python scientific computing NumPy, I hope that the content of this article on everyone's learning or work can bring some help, if there are questions you can leave a message to exchange.