Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Matrix and Numpy Array in Python

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you the relevant knowledge points of matrix and Numpy array in Python. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

What is a matrix?

Use the Python matrix of nested lists and NumPy packages. A matrix is a two-dimensional data structure in which numbers are arranged in rows and columns.

2. Python matrix 1. List is treated as matrix

Python does not have a built-in type of matrix. However, you can think of a list of lists as a matrix.

Example:

A = [[1,4,5], [- 5,8,9]]

You can think of the list of this list as a matrix with 2 rows and 3 columns.

As shown in the figure:

two。 How to use nested lists. A = [1, 4, 5, 12], [- 5, 8, 9, 0], [- 6, 7, 11, 19]] print ("A =", A) print ("A [1] =", A [1]) # second line print ("A [1] [2] =", A [1] [2]) # third element print ("A [0] [- 1] =" A [0] [- 1]) # the last element of the first line column = [] # empty list for row in A: column.append (row [2]) print ("3rd column =", column)

When running the program, the output is:

3. NumPy array 1. What is NumPy?

NumPy is a software package for scientific computing that supports powerful N-dimensional array objects.

You need to install NumPy before you can use it.

two。 How do I install NumPy?

If you use Windows, install NumPy,NumPy using PyCharm, which comes with some other software packages related to data science and machine learning.

Once NumPy is successfully installed, you can import and use it.

NumPy provides a multidimensional array of numbers (actually an object).

Example:

Import numpy as np a = np.array ([1,2,3]) print (a) # output: [1,2,3] print (type (a)) # output:

The array class of NumPy is called ndarray.

Note:

The array class of NumPy is called ndarray.

3. How do I create an NumPy array?

There are several ways to create an NumPy array.

3.1Array of integers, floating-point and complex numbers import numpy as np A = np.array ([[1,2,3], [3,4,5]]) print (A) A = np.array ([[1.1,2,3], [3,4,5]]) # floating-point array print (A) A = np.array ([[1,2,3], [3,4,5], dtype = complex) # complex array print (A)

Running effect:

Array import numpy as np zeors_array = np.zeros ((2,3)) print (zeors_array) ones_array = np.ones ((1,5), dtype=np.int32) / / dtype print (ones_array) # output: [[1 1 1]]

In this case, the specified dtype has 32 bits (4 bytes). Therefore, the array can take the value from to. -2-312-31-1

3. Use arange () and shape () import numpy as np A = np.arange (4) print ('A =', A) B = np.arange (12). Reshape (2,6) print ('B =', B)

IV. Matrix operation

The addition of two matrices, the multiplication of two matrices, and a matrix transpose. Before writing these programs, nested lists were used. Let's see how to use the NumPy array to accomplish the same task.

Addition of two kinds of matrices

Use the + operator to add the corresponding elements of two NumPy matrices.

Import numpy as np A = np.array ([[2,4], [5,-6]]) B = np.array ([[9,-3], [3,6]]) C = A + B # element clever addition print (C)

Multiply two matrices

To multiply the two matrices, use the dot () method.

Note: used for array multiplication (the multiplication of the corresponding elements of two arrays), not matrix multiplication.

Import numpy as np A = np.array ([[3,6,7], [5,-3,0]]) B = np.array ([[1,1], [2,1], [3,3]]) C = A.dot (B) print (C)

Matrix transpose

Use numpy.transpose to calculate the transpose of the matrix.

Import numpy as np A = np.array ([[1,1], [2,1], [3,-3]]) print (A.transpose ())

Note:

NumPy makes the task easier.

Fifth, case 1. Access matrix elements

Similar to lists, matrix elements can be accessed using indexes. Let's start with an one-dimensional NumPy array.

Import numpy as np A = np.array ([2,4,6,8,10]) print ("A [0] =", A [0]) # First element print ("A [2] =", A [2]) # Third element print ("A [- 1] =", A [- 1]) # Last element

When you run the program, the output is:

Now, let's see how to access the elements of a two-dimensional array (basically a matrix).

Import numpy as np A = np.array ([[1,4,5,12], [- 5,8,9,0], [- 6,7,11,19]]) # First element of first row print ("A [0] [0] =", A [0] [0]) # Third element of second row print ("A [1] [2] =", A [1] [2]) # Last element of last row print ("A [- 1] [- 1] =" A [- 1] [- 1])

When you run the program, the output will be:

two。 Access matrix row import numpy as np A = np.array ([1, 4, 5, 12], [- 5, 8, 9, 0], [- 6, 7, 11, 19]]) print ("A [0] =", A [0]) # First Row print ("A [2] =", A [2]) # Third Row print ("A [- 1] =", A [- 1]) # Last Row (3rd row in this case)

When you run the program, the output will be:

3. Access matrix column import numpy as np A = np.array ([1, 4, 5, 12], [- 5, 8, 9, 0], [- 6, 7, 11, 19]) print ("A [:, 0] =", A [:, 0]) # First Column print ("A [:, 3] =", A [:, 3]) # Fourth Column print ("A [:,-1] =", A [: -1]) # Last Column (4th column in this case)

When you run the program, the output will be:

Note:

Using NumPy instead of nested lists makes it easier to work with matrices without even involving the basics. It is recommended to study the NumPy package in detail, especially when trying to use Python for data science / analysis.

These are all the contents of the article "how to implement Matrices and Numpy arrays in Python". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report