In this article, we will discuss how to create 1D or 2D numpy arrays filled with ones (1s).
Python’s Numpy module provides a function to create a numpy array of given shape & type and filled with 1’s i.e,
numpy.ones(shape, dtype=float, order='C')
- shape: Shape of the numpy array. Single integer or sequence of integers.
- dtype: (Optional) Data type of elements. Default is float64.
- order: (Optional) Order in which data is stored in multi-dimension array i.e. in row major(‘F’) or column major (‘C’). Default is ‘C’.
- It returns a numpy array of given shape but filled with ones.
Let’s understand with some examples but first we need to import the numpy module,
import numpy as np
Create 1D Numpy Array of given length and filled with ones
Suppose we want to create a numpy array of five ones (1s). For that we need to call the numpy.ones() function with argument 5 i.e.
It returns a 1D numpy array with five 1s,
array([1., 1., 1., 1., 1.])
We can assign the array returned by ones() to a variable and print its type to confirm if it is a numpy array or not,
arr = np.ones(5) print(arr) print(type(arr))
[1. 1. 1. 1. 1.] <class 'numpy.ndarray'>
Create Numpy array with ones of integer data type
By default numpy.ones() returns a numpy array of float ones. But if we want to create a numpy array of ones as integers, then we can pass the data type too in the ones() function. For example,
arr = np.ones(5, dtype=np.int64) print(arr)
[1 1 1 1 1]
It returned a numpy array of ones as integers because we pass the datatype as np.int64.
Create two dimensional (2D) Numpy Array of ones
To create a multidimensional numpy array filled with ones, we can pass a sequence of integers as the argument in ones() function. For example, to create a 2D numpy array or matrix of 4 rows and 5 columns filled with ones, pass (4, 5) as argument in the ones() function.
arr_2d = np.ones( (4, 5) , dtype=np.int64) print(arr_2d)
[[1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1]]
It returned a matrix or 2D Numpy Array of 4 rows and 5 columns filled with 1s.
Create 3D Numpy Array filled with ones
To create a 3D Numpy array filled with ones, pass the dimensions as the argument in ones() function. For example,
arr_3d = np.ones( (2, 4, 5) , dtype=np.int64) print(arr_3d)
[[[1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1]] [[1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1]]]
It created a 3D Numpy array of shape (2, 4, 5) filled with 1s.
In this article we learned how to create 1D or 2D numpy array of given shape and filled with ones.
Pandas Tutorials -Learn Data Analysis with Python
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.