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.
Creating Multidimensional Arrays
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.
import numpy as np
arr = np.array([[1,2,3],[4,5,6]])
print(arr)
Output
[[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.
import numpy as np
arr = np.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]]])
print(arr)
Output
[[[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.
import numpy as np
arr = np.array([(1,2,3),(4,5,6)])
print(arr)
Output
[[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.
import numpy as np
arr = np.array([np.array([1,2,3]), np.array([4,5,6])])
print(arr)
Output
[[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.
import numpy as np
arr = np.array([['1','2','3'],['4','5','6']])
print(arr)
Output
[['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.
import numpy as np
arr = np.array([{'a' : 1, 'b' : 2, 'c' : 3},{'d' : 4, 'e' : 5, 'f' : 6}])
print(arr)
Output
[{'a': 1, 'b': 2, 'c': 3} {'d': 4, 'e': 5, 'f': 6}]
Properties of NumPy Arrays
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.
import numpy as np
arr = np.array([[1,1],[4,1],[2,3]])
print(arr.shape)
Output
(3, 2)
2. Number of Dimensions: The ndim attribute of an array gives the number of dimensions or axes of the array.
import numpy as np
arr = np.array([[1,1],[4,1],[2,3]])
print(arr.ndim)
Output
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.
import numpy as np
arr = np.array([[1,1],[4,1],[2,3]])
print(arr.dtype)
Output
int64
4. Total Number of Elements: The size attribute of an array returns the total number of elements in the array.
import numpy as np
arr = np.array([[1,1,2,2],[2,3,2,1]])
print(arr.size)
Output
8
5. Size of Each Element: The itemsize attribute of an array specifies the size of each element in bytes.
import numpy as np
arr = np.array([[1,1,2],[1,2,3],[3,2,1]])
print(arr.itemsize)
Output
8
6. Array Type: The type() function provides the type of the array, which is typically <class 'numpy.ndarray'>.
import numpy as np
arr = np.array([[1,1,2,2],[2,3,2,1]])
print(type(arr))
Output
<class 'numpy.ndarray'>
Changing Structure and Combining NumPy Arrays
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.
Reshaping and Changing Dimensions
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.
import numpy as np
arr = np.array([1,2,3,4,5,6])
res = arr.reshape(2,3)
print(res)
Output
[[1 2 3] [4 5 6]]
2. resize(): changes the shape of the array and directly modifies the original array.
import numpy as np
arr = np.array([1,2,3,4])
arr.resize(2,2)
print(arr)
Output
[[1 2] [3 4]]
3. ravel(): converts a multidimensional array into a flattened 1D array, usually as a view of the original array.
import numpy as np
arr = np.array([[1,2],[3,4]])
print(arr.ravel())
Output
[1 2 3 4]
4. flatten(): converts a multidimensional array into a flattened 1D array by creating a new copy.
import numpy as np
arr = np.array([[5,6],[7,8]])
print(arr.flatten())
Output
[5 6 7 8]
5. transpose(): rearranges the array by swapping its rows and columns (axes).
import numpy as np
arr = np.array([[1,2,3],[4,5,6]])
print(arr.transpose())
Output
[[1 4] [2 5] [3 6]]
6. moveaxis(): moves a specified axis of an array to a new position while keeping the data same.
import numpy as np
arr = np.array([[[1,2],[3,4]]])
print(np.moveaxis(arr, 0, -1))
Output
[[[1] [2]] [[3] [4]]]
Joining and Splitting Arrays
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.
import numpy as np
a = np.array([1,2])
b = np.array([3,4])
print(np.concatenate((a,b)))
Output
[1 2 3 4]
2. stack(): joins multiple arrays by creating a new axis in the result.
import numpy as np
a = np.array([1,2])
b = np.array([3,4])
print(np.stack((a,b)))
Output
[[1 2] [3 4]]
3. hstack(): combines arrays horizontally (column-wise) into a single array.
import numpy as np
a = np.array([1,2])
b = np.array([3,4])
print(np.hstack((a,b)))
Output
[1 2 3 4]
4. vstack(): combines arrays vertically (row-wise) into a single array.
import numpy as np
a = np.array([1,2])
b = np.array([3,4])
print(np.vstack((a,b)))
Output
[[1 2] [3 4]]
5. dstack(): combines arrays depth-wise along a third axis.
import numpy as np
a = np.array([1,2])
b = np.array([3,4])
print(np.dstack((a,b)))
Output
[[[1 3] [2 4]]]
6. column_stack(): stacks 1D arrays as columns to form a 2D array.
import numpy as np
a = np.array([1,2])
b = np.array([3,4])
print(np.column_stack((a,b)))
Output
[[1 3] [2 4]]
7. split(): divides an array into multiple equal-sized sub-arrays.
import numpy as np
arr = np.array([1,2,3,4])
print(np.split(arr,2))
Output
[array([1, 2]), array([3, 4])]
8. array_split(): divides an array into multiple sub-arrays, allowing unequal sizes.
import numpy as np
arr = np.array([1,2,3,4,5])
print(np.array_split(arr,3))
Output
[array([1, 2]), array([3, 4]), array([5])]
9. hsplit(): splits an array into multiple parts horizontally (column-wise).
import numpy as np
arr = np.array([[1,2,3,4]])
print(np.hsplit(arr,2))
Output
[array([[1, 2]]), array([[3, 4]])]
10. vsplit(): splits an array into multiple parts vertically (row-wise).
import numpy as np
arr = np.array([[1,2],[3,4]])
print(np.vsplit(arr,2))
Output
[array([[1, 2]]), array([[3, 4]])]
11. dsplit(): splits an array into multiple parts along the depth (third axis).
import numpy as np
arr = np.dstack((np.array([1,2]), np.array([3,4])))
print(np.dsplit(arr,2))
Output
[array([[[1],
[2]]]), array([[[3],
[4]]])]