In this lesson, you will learn
Let’s explore some common data manipulation operations with NumPy arrays:
One-dimensional arrays can be reshaped, indexed, sliced, and iterated over, much like lists and other Python sequences
numpy.reshape(a, newshape): Gives a new shape to an array without changing its data.
import numpy as np
arr = np.arange(1, 10)
reshaped_arr = np.reshape(arr, (3, 3))
print(reshaped_arr)
[[1 2 3]
[4 5 6]
[7 8 9]]
numpy.flatten(order='C'): Returns a copy of the array collapsed into one dimension.
Parameters: order: {‘C’, ‘F’, ‘A’, ‘K’}, optional
‘C’ means to flatten in row-major (C-style) order. ‘F’ means to flatten in column-major (Fortran-style) order. ‘A’ means to flatten in column-major order if a is Fortran contiguous in memory, row-major order otherwise. ‘K’ means to flatten a in the order in which the elements occur in memory. The default is ‘C’.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
flattened_arr = arr.flatten()
print(flattened_arr)
[1 2 3 4 5 6]
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
flattened_arr = arr.flatten('F')
print(flattened_arr)
[1 4 2 5 3 6]
There are no reviews yet. Be the first one to write one.
You must be logged in to submit a review.