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 create numpy Matrix

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

Share

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

This article focuses on "how to create a numpy matrix", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to create a numpy matrix.

Numpy is a class library commonly used in python, and it is very common in the use of python. It is widely used in matrix calculation. The speed of matrix operation of numpy is very different from that of pure python.

First, construct matrix

There are several ways to construct a matrix:

1. Construct a matrix using the method in python

-generate one-dimensional matrix

# generate a matrix a = list (range (100)) # range () using the range () method included with python to generate a list print (a) from 0-99

-generate two-dimensional and multidimensional matrices

# generate a matrix a = list by using the range () method that comes with python () ([[1min2, 3], [4, 5, 6], [7, 8, 9]) print (a)

two。 Use the methods in numpy to generate matrices

The data type of the matrix generated in the numpy class library is numpy.ndarray, which is different from the list in python.

(1) array () method to generate matrix

# numpy entry import numpy as npdata = [6 data 7.5 print 8 0 print 1] data1 = [1 Magi 2 print 3], [4 5 data 6]] arr = np.array (data1) print (arr) print (arr1)

The array () method converts a list into a numpy matrix with the same dimensions.

(2) generate random matrix methods rand () and randn ()

Import numpy as np# generates a random number matrix data = np.random.randn (2prim 3) # returns one or more sample values from the standard normal distribution data1 = np.random.rand (2mai 3) # Random samples are located in [0,1) print (data) print (data1)

(3) addition and multiplication of matrices. If matrices and numbers in a numpy matrix are added or multiplied, then each element in the array is added or multiplied.

Import numpy as npdata = np.random.randn (10) # returns one or more sample values from the standard normal distribution print (data) print ("data*10:\ n", data*10) # each element is multiplied by ten print ("data+data:\ n", data+data) # to implement each position self-addition operation in the array

(4) Zero matrix

You can use numpy's zeros () method to generate a matrix with all zero element values.

Import numpy as npdata = np.zeros (10) # to generate an one-dimensional all-zero matrix, the elements of the matrix are ten print ("data:", data) data1 = np.zeros ((3jing4)) # generate a three-row and four-column all-zero matrix print ("data1:", data1) data2 = np.zeros ((3jin4p3)) print ("data2:", data2) # generate a three-dimensional all-zero matrix

(5) one matrix

Like a zero matrix, the ones () method in numpy can produce a matrix whose element values are all one.

Import numpy as npdata = np.ones (10) # generates an one-dimensional all-zero matrix. The elements of the matrix are ten print ("data:", data) data1 = np.ones ((3jing4)) # to generate a three-row and four-column all-zero matrix print ("data1:", data1) data2 = np.ones ((3p4p3)) print ("data2:", data2)

(6) empty () method

You can also use the numpy.empty () method in python to produce numbers that appear to be zeros. The syntax is the same as the ones () method.

# numpy introduction import numpy as npdata = np.empty (10) # generate an one-dimensional all-zero matrix, the elements of the matrix are ten print ("data:", data) data1 = np.empty ((3Power4)) # generate a three-row and four-column all-zero matrix print ("data1:", data1) data2 = np.empty ((3p4pp3)) print ("data2:", data2)

Even if the value shown in the compiler is 0, the actual value is not 0, just a number very close to 0.

# numpy entry import numpy as npdata1 = np.empty ((3Power4)) # generate an all-zero matrix print ("data1:\ n", data1) print ("1/data1:\ n", 1/data1) with three rows and four columns

Inf means infinity, and if the value of the data in data1 is 0, the interpreter will make an error while running.

# Note: it is not safe to think that np.empty will return an all-zero array. In many cases (as shown earlier), it returns uninitialized garbage values.

(7) arange () method

Similar to the range () method

Import numpy as npa = np.arange (10) b = np.arange (2Jing 20) c = np.arange (0Jing 50 Magi 5) print ("a:", a) print ("b:", b) print ("c:", c)

When there is only one parameter n, it produces a matrix that does not contain n from [0mern).

A matrix that does not contain n from [m < m > n] is generated when there are two parameters m < m > n.

When there are three parameters m _ ~ n ~ 1, it means that starting from m, each time l is a step, a matrix is generated, and the maximum value does not exceed n

(8) reshape () method to regenerate the dimension size of the matrix

Import numpy as npa = np.arange (10) print (a) a=a.reshape (2) print (b)

In the above example, an one-dimensional ten-element matrix is transformed into a matrix with two rows and five columns.

Note: when you use the reshape () method to change from one-dimensional to multi-dimensional, the number of elements of the one-dimensional matrix must be the same as that of the multidimensional matrix, that is, 10-2-5 in the above example. If it is not equal, the interpreter may have an error.

(9) some values related to the size of the matrix

Import numpy as nparray = np.array ([[1jime 2jue 3], [4je 5rect 6], [7je 8rect 9]) print (array) print (array.ndim) # Dimension print (array.shape) # the value of each dimension print (array.size) # number of elements print (array.dtype) # element data type

At this point, I believe you have a deeper understanding of "how to create a numpy matrix". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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