![]() |
VOOZH | about |
Manipulating multidimensional arrays involves working with data arranged in multiple dimensions such as rows and columns or higher-dimensional structures. It enables efficient storage, transformation and computation on complex datasets commonly used in scientific and data analysis tasks.
NumPy allows to create multidimensional arrays from different Python data structures. Each nested structure represents a new dimension in the array.
1. 2D Array from List of Lists: NumPy's np.array() function can create a 2D array from a list of lists, where each sub-list represents a row in the array.
[[1 2 3] [4 5 6]]
2. 3D Array from Nested Lists: Similarly, a 3D array can be created from a nested list structure, with each sublist representing a 2D slice of the array.
[[[1 2 3] [4 5 6] [7 8 9]] [[1 2 3] [4 5 6] [7 8 9]] [[1 2 3] [4 5 6] [7 8 9]]]
3. 2D Array from List of Tuples: NumPy arrays can also be created from a list of tuples, with each tuple representing a row in the array.
[[1 2 3] [4 5 6]]
4. 2D Array from List of Arrays: Arrays within a list can be used to construct a 2D array, with each array representing a row in the resulting array.
[[1 2 3] [4 5 6]]
5. 2D Array from List of Strings: Even a list of strings can be converted into a 2D array, where each string is treated as an element in the array.
[['1' '2' '3'] ['4' '5' '6']]
6. Array from List of Dictionaries: Although less common, it's possible to create a 2D array from a list of dictionaries, with each dictionary representing a row where keys are column names.
[{'a': 1, 'b': 2, 'c': 3} {'d': 4, 'e': 5, 'f': 6}]
NumPy arrays provide several built-in attributes that describe their shape, size, dimensions and data type. These properties help understand how data is stored inside an array.
1. Array Shape: The shape attribute of a NumPy array provides information about its dimensions, indicating the number of rows and columns.
(3, 2)
2. Number of Dimensions: The ndim attribute of an array gives the number of dimensions or axes of the array.
2
3. Data Type of Elements: The dtype attribute of an array specifies the data type of its elements, such as integer, float, or string.
int64
4. Total Number of Elements: The size attribute of an array returns the total number of elements in the array.
8
5. Size of Each Element: The itemsize attribute of an array specifies the size of each element in bytes.
8
6. Array Type: The type() function provides the type of the array, which is typically <class 'numpy.ndarray'>.
<class 'numpy.ndarray'>
NumPy provides several methods to modify the shape, dimensions and arrangement of multidimensional arrays. It also allows combining multiple arrays or splitting a single array into parts for easier data manipulation and analysis.
These methods are used to change the shape, orientation or dimension of arrays without affecting the actual data.
1. reshape(): returns a new array with a different shape without changing the original data.
[[1 2 3] [4 5 6]]
2. resize(): changes the shape of the array and directly modifies the original array.
[[1 2] [3 4]]
3. ravel(): converts a multidimensional array into a flattened 1D array, usually as a view of the original array.
[1 2 3 4]
4. flatten(): converts a multidimensional array into a flattened 1D array by creating a new copy.
[5 6 7 8]
5. transpose(): rearranges the array by swapping its rows and columns (axes).
[[1 4] [2 5] [3 6]]
6. moveaxis(): moves a specified axis of an array to a new position while keeping the data same.
[[[1] [2]] [[3] [4]]]
These methods help combine multiple arrays into one or divide an array into smaller parts.
1. concatenate(): combines two or more arrays into a single array along an existing axis.
[1 2 3 4]
2. stack(): joins multiple arrays by creating a new axis in the result.
[[1 2] [3 4]]
3. hstack(): combines arrays horizontally (column-wise) into a single array.
[1 2 3 4]
4. vstack(): combines arrays vertically (row-wise) into a single array.
[[1 2] [3 4]]
5. dstack(): combines arrays depth-wise along a third axis.
[[[1 3] [2 4]]]
6. column_stack(): stacks 1D arrays as columns to form a 2D array.
[[1 3] [2 4]]
7. split(): divides an array into multiple equal-sized sub-arrays.
[array([1, 2]), array([3, 4])]
8. array_split(): divides an array into multiple sub-arrays, allowing unequal sizes.
[array([1, 2]), array([3, 4]), array([5])]
9. hsplit(): splits an array into multiple parts horizontally (column-wise).
[array([[1, 2]]), array([[3, 4]])]
10. vsplit(): splits an array into multiple parts vertically (row-wise).
[array([[1, 2]]), array([[3, 4]])]
11. dsplit(): splits an array into multiple parts along the depth (third axis).
[array([[[1], [2]]]), array([[[3], [4]]])]