reshape vs. resize
The array methods reshape and resize both enable you to change an array’s dimensions. Method reshape returns a view (shallow copy) of the original array with the new dimensions. It does not modify the original array:
In [1]: import numpy as np
In [2]: grades = np.array([[87, 96, 70], [100,87, 90]])
In [3]: grades
Out[3]:
array([[ 87, 96, 70],
[100, 87, 90]])
In [4]: grades.reshape(1, 6)
Out[4]: array([[ 87, 96, 70, 100, 87, 90]])
In [5]: grades
Out[5]:
array([[ 87, 96, 70],
[100, 87, 90]])
Method resize modifies the original array’s shape:
In [6]: grades.resize(1, 6)
In [7]: grades
Out[7]: array([[ 87, 96, 70, 100, 87, 90]])
flatten vs. ravel
You can take a multidimensional array and flatten it into a single dimension with the methods flatten and ravel. Method flatten deep copies the original array’s data:
In [8]: grades = np.array([[87, 96, 70], [100,87, 90]])
In [9]: grades
Out[9]:
array([[ 87, 96, 70],
[100, 87, 90]])
In [10]: flattened = grades.flatten()
In [11]: flattened
Out[11]: array([ 87, 96, 70, 100, 87, 90])
In [12]: grades
Out[12]:
array([[ 87, 96, 70],
[100, 87, 90]])
To confirm that grades and flattened do not share the data, let’s modify an element of flattened, then display both arrays:
In [13]: flattened[0] = 100
In [14]: flattened
Out[14]: array([100, 96, 70, 100, 87, 90])
In [15]: grades
Out[15]:
array([[ 87, 96, 70],
[100, 87, 90]])
Method ravel produces a view of the original array, which shares the grades array’s data:
In [16]: raveled = grades.ravel()
In [17]: raveled
Out[17]: array([ 87, 96, 70, 100, 87, 90])
In [18]: grades
Out[18]:
array([[ 87, 96, 70],
[100, 87, 90]])
To confirm that grades and raveled share the same data, let’s modify an element of raveled, then display both arrays:
In [19]: raveled[0] = 100
In [20]: raveled
Out[20]: array([100, 96, 70, 100, 87, 90])
In [21]: grades
Out[21]:
array([[100, 96, 70],
[100, 87, 90]])
Transposing Rows and Columns
You can quickly transpose an array’s rows and columns that is “flip” the array, so the rows become the columns and the columns become the rows. The T attribute returns a transposed view (shallow copy) of the array. The original grades array represents two students’ grades (the rows) on three exams (the columns). Let’s transpose the rows and columns to view the data as the grades on three exams (the
rows) for two students (the columns):
In [22]: grades.T
Out[22]:
array([[100, 100],
[ 96, 87],
[ 70, 90]])
Transposing does not modify the original array:
In [23]: grades
Out[23]:
array([[100, 96, 70],
[100, 87, 90]])
Horizontal and Vertical Stacking
You can combine arrays by adding more columns or more rows—known as horizontal stacking and vertical stacking.
Let’s create another 2-by-3 array of grades:
In [24]: grades2 = np.array([[94, 77, 90], [100,81, 82]])
Let’s assume grades2 represents three additional exam grades for the two students in the grades array. We can combine grades and grades2 with NumPy’s hstack (horizontal stack) function by passing a tuple containing the arrays to combine. The extra parentheses are required because hstack expects one argument:
In [25]: np.hstack((grades, grades2))
Out[25]:
array([[100, 96, 70, 94, 77, 90],
[100, 87, 90, 100, 81, 82]])
Next, let’s assume that grades2 represents two more students’ grades on three exams. In this case, we can combine grades and grades2 with NumPy’s vstack (vertical stack) function:
In [26]: np.vstack((grades, grades2))
Out[26]:
array([[100, 96, 70],
[100, 87, 90],
[ 94, 77, 90],
[100, 81, 82]])
0 comments:
Post a Comment