NumPy - Array From Existing Data
Creating Array From Existing Data in NumPy
You can create arrays from existing data in NumPy by initializing NumPy arrays using data structures that already exist in Python, or can be converted to a format compatible with NumPy. Following are a few common ways to achieve this −
- Using numpy.asarray() Function
- Using numpy.frombuffer() Function
- Using numpy.fromiter() Function
- From Python Lists
- From Nested Lists
- From Python Tuples
- From Existing NumPy Arrays
- Using Range Objects
NumPy’s ability to work fast and perform complex operations on arrays is really important in fields like data handling and performing scientific calculations.
Using numpy.asarray() Function
The numpy.asarray() function is used to convert various Python objects into NumPy arrays. These objects includes Python lists, tuples, other arrays, and even scalar values.
This function ensures that the result is always a NumPy array, making it convenient for data manipulation and numerical computations. Following is the syntax −
numpy.asarray(a, dtype=None, order=None)
Where,
- a − It is the input data, which can be a list, tuple, array, or any object that can be converted to an array.
- dtype (optional) − Desired data type of the array. If not specified, NumPy determines the data type based on the input.
- order (optional) − Specifies whether to store the array in row-major (C) or column-major (F) order. Default is None, which means NumPy decides based on the input.
Example: Convert Python List to NumPy Array
In the following example, we are using the numpy.asarray() function to convert a Python list into a NumPy array −
import numpy as np
# Convert list to array
my_list = [1, 2, 3, 4, 5]
arr_from_list = np.asarray(my_list)
print("Array from list:",arr_from_list)
Following is the output obtained −
Array from list: [1 2 3 4 5]
Example: Preserve Data Type
In here, we are converting the Python list which contains elements of different data types (int, float, bool, str), into a NumPy array using the numpy.asarray() function −
import numpy as np
# Convert list with different data types to array
my_mixed_list = [1, 2.5, True, 'hello']
arr_from_mixed = np.asarray(my_mixed_list)
print("Array from mixed list:", arr_from_mixed)
This will produce the following result −
Array from mixed list: ['1' '2.5' 'True' 'hello']
Using numpy.frombuffer()Function
The numpy.frombuffer() function creates an array from a buffer object, such as bytes objects or byte arrays. This is useful when working with raw binary data or memory buffers.
This function interprets the buffer object as one-dimensional array data. It allows you to specify the data type of the elements in the resulting array.Following is the syntax −
numpy.frombuffer(buffer, dtype=float, count=-1, offset=0)
Where,
- buffer − It is the buffer object containing the data to be interpreted as an array.
- dtype (optional) − It is the desired data type of the elements in the resulting array. Default is float.
- count (optional) − It is the number of items to read from the buffer. Default is -1, which means all data is read.
- offset (optional) − It is the starting position within the buffer to begin reading data. Default is 0.
Example
In this example, we are using the numpy.frombuffer() function to interpret the bytes object “my_bytes” as a one-dimensional array of bytes −
import numpy as np
# Create bytes object
my_bytes = b'hello world'
# Create array from bytes object
arr_from_bytes = np.frombuffer(my_bytes, dtype='S1')
print("Array from bytes object:",arr_from_bytes)
The resulting NumPy array contains each byte of the original bytes object ‘hello world’ −
Array from bytes object: [b'h' b'e' b'l' b'l' b'o' b' ' b'w' b'o' b'r' b'l' b'd']
Using numpy.fromiter() Function
The numpy.fromiter() function creates a new one-dimensional array from an iterable object. It iterates over the iterable object, converting each element into an array element. Following is the syntax −
numpy.fromiter(iterable, dtype, count=-1)
Where,
- iterable − The iterable object that yields elements one by one.
- dtype − The data type of the elements in the resulting array.
- count (optional) − The number of items to read from the iterable. Default is -1, which means all items are read.
Example
In the example below, we are using the numpy.fromiter() function to create a NumPy array “gen_array” from the generator “my_generator” that yields numbers from 0 to 4 −
import numpy as np
# Generator function that yields numbers
def my_generator(n):
for i in range(n):
yield i
# Create array from generator
gen_array = np.fromiter(my_generator(5), dtype=int)
print("Array from generator:",gen_array)
In the resulting array, each element corresponds to a value yielded by the generator function converted to integers −
Array from generator: [0 1 2 3 4]
Generators in Python are functions that generate a sequence of values one at a time. NumPy provides np.fromiter() function to create arrays from generators.
From Python Lists
One of the most common ways to create a NumPy array is by converting a Python list. This method provides the numpy.array() function or numpy.asarray() function to convert lists, which are commonly used data structures in Python, into NumPy arrays.
Following is the syntax −
numpy.array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0)
Where,
- object − The input data, which in this case is a Python list.
- dtype (optional) − Desired data type of the array. If not specified, NumPy interprets the data type from the input data.
- copy (optional) − If True, ensures that a copy of the input data is made. If False, avoids unnecessary copies when possible.
- order (optional) − Specifies the memory layout order of the array. ‘C’ for row-major (C-style), ‘F’ for column-major (Fortran-style), and ‘K’ for the layout of the input array (default).
- subok (optional) − If True, subclasses are passed through; otherwise, the returned array will be forced to be a base-class array.
- ndmin (optional) − Specifies the minimum number of dimensions the resulting array should have.
Example
In the example below, we are converting the Python list “my_list” containing integers into a NumPy array using the numpy.array() function −
import numpy as np
# Convert list to array
my_list = [1, 2, 3, 4, 5]
arr_from_list = np.array(my_list)
print("Array from list:",arr_from_list)
After executing the above code, we get the following output −
Array from list: [1 2 3 4 5]
From Nested Lists
Nested lists in Python are lists within lists, which can represent multi-dimensional data structures. NumPy provides the array() function to convert these nested lists into multi-dimensional arrays.
Example: Convert a Nested List to a 2D NumPy Array
In this example, the nested list “nested_list” represents a 2D structure (list of lists). The array() function converts it into a 2D NumPy array “arr_from_nested_list” −
import numpy as np
# Convert nested list to array
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
arr_from_nested_list = np.array(nested_list)
print("Array from nested list:")
print(arr_from_nested_list)
The resulting array retains the 2D structure and contains the same elements as the original nested list as shown in the output below −
Array from nested list:
[[1 2 3]
[4 5 6]
[7 8 9]]
Example: Convert a Nested List with Different Data Types
In here, the nested list contains elements of different data types (integers, floats, booleans, and strings). The array() function converts all elements to strings, resulting in a homogeneous 2D array with string data type −
import numpy as np
# Convert nested list with different data types to array
nested_mixed_list = [[1, 2.5], [True, 'hello']]
arr_from_nested_mixed_list = np.array(nested_mixed_list)
print("Array from nested mixed list:")
print(arr_from_nested_mixed_list)
The result produced is as follows −
Array from nested mixed list:
[['1' '2.5']
['True' 'hello']]
From Python Tuples
Python tuples are another commonly used data structure that can be converted into NumPy arrays. Like lists, tuples can be used to store multiple items, but they are immutable, meaning their content cannot be changed after creation.
It can be used to represent both one-dimensional and multi-dimensional data using the numpy.array() function.
Example
In the following example, we are converting the Python tuple containing integers into a NumPy array using the array() function −
import numpy as np
# Convert tuple to array
my_tuple = (1, 2, 3, 4, 5)
arr_from_tuple = np.array(my_tuple)
print("Array from tuple:",arr_from_tuple)
We get the output as shown below −
Array from tuple: [1 2 3 4 5]
From Existing NumPy Arrays
NumPy provides several methods to create new arrays from existing NumPy arrays. They are −
- numpy.copy() Function
- numpy.asarray() Function
- numpy.view() Function
- numpy.reshape() Function
- Slicing
This can be useful for various tasks, such as copying data, changing data types, or creating new arrays with specific attributes derived from the original array.
Example: Using numpy.copy() Function
The numpy.copy() function creates a new array that is a copy of the original array. This ensures that any modifications to the new array do not affect the original array −
import numpy as np
# Original array
original_array = np.array([1, 2, 3, 4, 5])
# Create a copy of the array
copied_array = np.copy(original_array)
print("Original array:",original_array)
print("Copied array:",copied_array)
Following is the output obtained −
Original array: [1 2 3 4 5]
Copied array: [1 2 3 4 5]
Example: Using numpy.asarray() Function
The numpy.asarray() function converts the input to an array, but if the input is already an array, it does not create a copy unless necessary (e.g., if a different data type is specified) −
import numpy as np
# Original array
original_array = np.array([1, 2, 3, 4, 5])
# Create an array from the existing array
new_array = np.asarray(original_array, dtype=float)
print("Original array:",original_array)
print("New array:",new_array)
This will produce the following result −
Original array: [1 2 3 4 5]
New array: [1. 2. 3. 4. 5.]
Example: Using numpy.view() Function
The numpy.view() function creates a new array object that looks at the same data as the original array. This can be useful for viewing the data with a different data type −
import numpy as np
# Original array
original_array = np.array([1, 2, 3, 4, 5], dtype=np.int32)
# Create a view of the array with a different dtype
viewed_array = original_array.view(dtype=np.float32)
print("Original array:",original_array)
print("Viewed array with dtype float32:",viewed_array)
Following is the output of the above code −
Original array: [1 2 3 4 5]
Viewed array with dtype float32: [1.e-45 3.e-45 4.e-45 6.e-45 7.e-45]
Example: Using numpy.reshape() Function
The numpy.reshape() function reshapes an existing array into a new shape without changing its data −
import numpy as np
# Original array
original_array = np.array([1, 2, 3, 4, 5, 6])
# Reshape the array to 2x3
reshaped_array = original_array.reshape((2, 3))
print("Original array:",original_array)
print("Reshaped array (2x3):",reshaped_array)
The output obtained is as shown below −
Original array: [1 2 3 4 5 6]
Reshaped array (2x3): [[1 2 3]
[4 5 6]]
Example: Using Slicing
Slicing an existing array creates a new array that is a subset of the original array −
import numpy as np
# Original array
original_array = np.array([1, 2, 3, 4, 5])
# Slice the array to get a subarray
sliced_array = original_array[1:4]
print("Original array:",original_array)
print("Sliced array (elements 1 to 3):",sliced_array)
The output obtained is as shown below −
Original array: [1 2 3 4 5] Sliced array (elements 1 to 3): [2 3 4]
Using Range Objects
Python range object generates numbers within a specified range and can be converted into a NumPy array using the numpy.array() function or numpy.fromiter() function. This is useful when you need to create large sequences without explicitly storing all numbers in memory first.
Example
In this example, the range() object generates numbers from 1 to 9. The numpy.array() function converts this range object into a NumPy array −
import numpy as np
# Create a range object
my_range = range(1, 10)
# Convert range object to array
arr_from_range = np.array(my_range)
print("Array from range object:",arr_from_range)
After executing the above code, we get the following output −
Array from range object: [1 2 3 4 5 6 7 8 9]