In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces numpy how to achieve array reshaping, the article introduces in great detail, has a certain reference value, interested friends must read it!
1. Array reshaping
To reshape an array is to change the shape of an array. For example, reshape the original array of three rows and four columns into an array of four rows and three columns. Using reshape method to realize Array reshaping in numpy
1.1 one-dimensional array reshaping
One-dimensional array reshaping is to reshape an array from a row or column array to an array with multiple rows and columns.
First create an one-dimensional array
Import numpy as nparr = np.arange (8) print (arr)
Result:
[0 1 2 3 4 5 6 7]
The above array can be converted to either a multidimensional array of two rows and four columns, or a multidimensional array of four rows and two columns.
1.1.1 reshape an array into a multi-dimensional array with two rows and four columns
Print (arr.reshape (2,4))
Result:
[[0 1 2 3]
[4 5 6 7]]
1.1.2 reshape an array into a multi-dimensional array with four rows and two columns
Print (arr.reshape (4,2))
Result:
[[0 1]
[2 3]
[4 5]
[6 7]]
Note: no matter 2 rows or 4 columns or 4 rows or 2 columns, as long as the number of values in the reshaped array is equal to the number of values in the previous one-dimensional array.
1.2 Multidimensional array reshaping
First create a multi-dimensional array
Import numpy as nparr = np.array ([[1,2,3], [4,5,6], [7,8,9], [10,11,12]) print (arr)
Result:
[[1 2 3]
[4 5 6]
[7 8 9]
[10 11 12]]
Similarly, the above array can be converted to either a three-row, four-column multidimensional array or a two-row, six-column multidimensional array.
1.2.1 reshape the array into a multi-dimensional array with three rows and four columns
Print (arr.reshape (3,4))
Result:
[[1 2 3 4]
[5 6 7 8]
[9 10 11 12]]
1.2.2 reshape the array into a multi-dimensional array with 2 rows and 6 columns
Print (arr.reshape (2,6))
Result:
[[1 2 3 4 5 6]
[7 8 9 10 11 12]]
Note: we can also reshape a multi-dimensional array with 4 rows and 3 columns into a multi-dimensional array with 3 rows, 4 columns or 2 rows and 6 columns, as long as the number of values in the reshaped array is equal to the number of values in the previous one-dimensional array.
two。 Array transpose
Array transposition is to rotate the rows of the array into columns, the method used is .T. Transposition can be seen here as a special kind of reshaping.
Import numpy as nparr = np.array ([[1,2,3], [4,5,6], [7,8,9], [10,11,12]) print (arr)
Result:
[[1 2 3]
[4 5 6]
[7 8 9]
[10 11 12]]
Print (arr.T)
Result:
[[1 4 7 10]
[2 5 8 11]
[3 6 9 12]]
These are all the contents of the article "how to reshape arrays in numpy". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.