Table of Contents
NumPy is a powerful Python library that provides efficient data structures and tools for scientific computing. One of its key features is its ability to create and manipulate homogeneous multidimensional arrays. To fill a NumPy array with values, one can use the `numpy.array()` function and pass in a list or tuple containing the desired values. Alternatively, NumPy also offers specialized functions such as `numpy.zeros()` and `numpy.ones()` to create arrays filled with zeros or ones respectively. Here are some examples of how to fill a NumPy array with values:
1. Using the `numpy.array()` function:
“`python
import numpy as np
my_arr = np.array([1, 2, 3, 4, 5])
print(my_arr)
# Output: [1 2 3 4 5]
“`
2. Using the `numpy.zeros()` function:
“`python
import numpy as np
my_arr = np.zeros((3, 3)) # creates a 3×3 array filled with zeros
print(my_arr)
# Output:
# [[0. 0. 0.]
# [0. 0. 0.]
# [0. 0. 0.]]
“`
3. Using the `numpy.ones()` function:
“`python
import numpy as np
my_arr = np.ones((2, 4)) # creates a 2×4 array filled with ones
print(my_arr)
# Output:
# [[1. 1. 1. 1.]
# [1. 1. 1. 1.]]
“`
In summary, NumPy provides various methods for filling arrays with values, allowing for efficient and flexible data manipulation for scientific computing tasks.
Fill NumPy Array with Values (2 Examples)
You can use the following methods to fill a NumPy array with values:
Method 1: Use np.full()
#create NumPy array of length 10 filled with 3's
my_array = np.full(10, 3)
Method 2: Use fill()
#create empty NumPy array with length of 10 my_array = np.empty(10) #fill NumPy array with 3's my_array.fill(3)
The following examples show how to use each function in practice.
Example 1: Use np.full()
The following code shows how to use the np.full() function to fill up a NumPy array of length 10 with the value 3 in each position:
import numpy as np
#create NumPy array of length 10 filled with 3's
my_array = np.full(10, 3)
#view NumPy array
print(my_array)
[3 3 3 3 3 3 3 3 3 3]
The NumPy array is filled with a value of 3 in each position.
We can use similar syntax to create a NumPy array of any size.
For example, the following code shows how to create a NumPy array with 7 rows and 2 columns:
import numpy as np
#create NumPy array filled with 3's
my_array = np.full((7, 2), 3)
#view NumPy array
print(my_array)
[[3 3]
[3 3]
[3 3]
[3 3]
[3 3]
[3 3]
[3 3]]
The result is a NumPy array with 7 rows and 2 columns where each position is filled with a value of 3.
Example 2: Use fill()
The following code shows how to use the fill() function to fill up an empty NumPy array with the value 3 in each position:
#create empty NumPy array with length of 10 my_array = np.empty(10) #fill NumPy array with 3's my_array.fill(3) #view NumPy arrayprint(my_array) [3. 3. 3. 3. 3. 3. 3. 3. 3. 3.]
The following tutorials explain how to perform other common tasks in Python:
Cite this article
stats writer (2024). How can I fill a NumPy array with values? Can you provide some examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-fill-a-numpy-array-with-values-can-you-provide-some-examples/
stats writer. "How can I fill a NumPy array with values? Can you provide some examples?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-fill-a-numpy-array-with-values-can-you-provide-some-examples/.
stats writer. "How can I fill a NumPy array with values? Can you provide some examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-fill-a-numpy-array-with-values-can-you-provide-some-examples/.
stats writer (2024) 'How can I fill a NumPy array with values? Can you provide some examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-fill-a-numpy-array-with-values-can-you-provide-some-examples/.
[1] stats writer, "How can I fill a NumPy array with values? Can you provide some examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I fill a NumPy array with values? Can you provide some examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
