Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the method of calculating and transposing numpy array in Python

Shulou Source: shulou.com Published: 2022-06-02 08:12:25 12月04日 Update

This article introduces the relevant knowledge of "what is the method of calculating and transposing numpy array in Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Numpy array and operation of numbers

Mainly includes the addition, subtraction, multiplication and division of arrays and numbers. Don't talk too much nonsense. Look at the code:

Import numpy as np a = np.array ([[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]]) # divide every number in array a + 1b = a+1print (b) # every number in array a-3c = a-3print (c) # divide each number in array a * 3D = a*3print (d) # divide each number in array a by 3e = a/3print (e)

The running results are as follows:

2. Array operation of the same size of numpy

Numpy addition, subtraction, multiplication and division of the same size, the code is as follows:

Import numpy as np a = np.array ([[1,2,3,4,5,6], [7, 8, 9, 10, 11, 12]) b = np.array ([[11, 22, 33, 44, 55, 66], [77, 88, 99, 10, 11]) 12]]) # addition operation of array an and array b c = a+bprint (c) # subtraction operation of array an and array b d = a-bprint (d) # multiplication operation of array an and array b e = a*bprint (e) # division operation of array an and array b f = a/bprint (f)

The running results are as follows:

3. Array calculation of different sizes of numpy

Numpy arrays of different sizes can also be operated and follow the broadcast principle. The code is as follows:

Import numpy as np a = np.array ([1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]) b = np.array ([1, 2, 3, 4, 5, 6]) c = np.array ([1]) [2]) print (a) print (b) print (c) # subtraction of array an and b d = a-bprint (d) # multiplication of array an and array b e = a*bprint (e) # subtraction of array an and array c f = a-cprint (f) # multiplication of array an and array c g = a*cprint (g)

The running result is as follows:

You should be able to see the difference between the two. All array operations follow the broadcast principle.

4. Transpose of numpy array

Mainly talk about three transpose methods, the specific code is as follows:

Import numpy as np a = np.array ([[1,2,3,4,5,6], [7,8,9,10,11,12], [13,14,15,16,17,18]) # three methods of array transposition b = np.transpose (a) c = a.Td = a.swapaxes (1,0) print (a) print (b) print (c) print (d)

The running result is as follows:

This is the end of the content of "what is the method of calculating and transposing the numpy array in Python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Arrays operations methods code dimensions results operations multiplication subtraction different same addition subtraction multiplication and division content principles more knowledge broadcasting utility learning and then Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei vpn Linux macOS MariaDB