- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Return a new array of given shape filled with ones but with different datatype in Numpy
To return a new array of given shape and type, filled with ones, use the numpy.ones() method in Python Numpy. The 1st parameter sets the number of rows. The 2nd parameter sets the number of columns. Both 1st and 2nd parameters forms the shape of the array. The "dtype" parameter is used to set the desired data-type for the array.
The function returns an array of ones with the given shape, dtype, and order. The order suggests wether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory.
NumPy offers comprehensive mathematical functions, random number generators, linear algebra routines, Fourier transforms, and more. It supports a wide range of hardware and computing platforms, and plays well with distributed, GPU, and sparse array libraries.
Steps
At first, import the required library −
import numpy as np
To return a new array of given shape and type, filled with ones, use the numpy.ones() method in Python Numpy. The "dtype" parameter is used to set the desired data-type for the array −
arr = np.ones((4,5), dtype = int)
Display the array −
print("Array...
",arr)
Get the datatype −
print("
Array datatype...
",arr.dtype)
Get the dimensions of the Array −
print("
Array Dimensions...
",arr.ndim)
Get the shape of the Array −
print("
Our Array Shape...
",arr.shape)
Get the number of elements in the Array −
print("
Elements in the Array...
",arr.size)
Example
import numpy as np # To return a new array of given shape and type, filled with ones, use the numpy.ones() method in Python Numpy # The 1st parameter sets the number of rows # The 2nd parameter sets the number of columns # Both 1st and 2nd parameters forms the shape of the array # The "dtype" parameter is used to set the desired data-type for the array arr = np.ones((4,5), dtype = int) # Displaying our array print("Array...
",arr) # Get the datatype print("
Array datatype...
",arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...
",arr.ndim) # Get the shape of the Array print("
Our Array Shape...
",arr.shape) # Get the number of elements of the Array print("
Elements in the Array...
",arr.size)
Output
Array... [[1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1]] Array datatype... int64 Array Dimensions... 2 Our Array Shape... (4, 5) Elements in the Array... 20
- Related Articles
- Return a new array of given shape filled with ones in Numpy
- Return a new array of a given shape filled with ones in Numpy
- Return a new array of given shape filled with zeros in Numpy
- Return a new array of given shape filled with zeros and also set the desired datatype in Numpy
- Return a new array of given shape filled with ones and also set the desired data-type in Numpy
- Return a new array of given shape and type, filled with array-like in Numpy
- Return a new array of given shape filled with a fill value and a different output type in Numpy
- Return a new array of given shape and type filled with a fill value in Numpy
- Return a 2-D array with ones on the diagonal and zeros elsewhere but set a different datatype in Numpy
- Return an array of zeroes with the same shape as a given array but with a different type in Numpy
- Return a new array of given shape filled with zeros and also set the desired output in Numpy
- Return a new array with the same shape as a given array but change the default type in Numpy
- Return an array of ones with the same shape and type as a given array in Numpy
- Return a masked array containing the same data but with a new shape in Numpy
- Return a new array with the same shape and type as given array in Numpy
