- 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 scalar type which is common to the input arrays in Python
To return a scalar type which is common to the input arrays, use the numpy.common_type() method in Python Numpy. The 1st parameter is the input array(s). The return type will always be an inexact (i.e. floating point) scalar type, even if all the arrays are integer arrays. If one of the inputs is an integer array, the minimum precision type that is returned is a 64-bit floating point dtype.
All input arrays except int64 and uint64 can be safely cast to the returned dtype without loss of information.
Steps
At first, import the required library −
import numpy as np
To return a scalar type which is common to the input arrays, use the numpy.common_type() method −
print("Using the common_type() method in Numpy\n") print("Result...",np.common_type(np.arange(3,dtype=np.float32))) print("Result...",np.common_type(np.arange(3,dtype=np.float32), np.arange(2))) print("Result...",np.common_type(np.arange(3), np.array([22, 2.j]), np.array([32.9]))) print("Result...",np.common_type(np.arange(3), np.array([22, 39]), np.array([32.9]))) print("Result...",np.common_type(np.arange(3,dtyp =np.int32), np.arange(2))) Example
Example
import numpy as np # To return a scalar type which is common to the input arrays, use the numpy.common_type() method in Python Numpy. # The 1st parameter is the input array(s). print("Using the common_type() method in Numpy\n") print("Result...",np.common_type(np.arange(3,dtype=np.float32))) print("Result...",np.common_type(np.arange(3,dtype=np.float32), np.arange(2))) print("Result...",np.common_type(np.arange(3), np.array([22, 2.j]), np.array([32.9]))) print("Result...",np.common_type(np.arange(3), np.array([22, 39]), np.array([32.9]))) print("Result...",np.common_type(np.arange(3,dtype=np.int32), np.arange(2)))
Output
Using the common_type() method in Numpy Result... <class 'numpy.float32'> Result... <class 'numpy.float64'> Result... <class 'numpy.complex128'> Result... <class 'numpy.float64'> Result... <class 'numpy.float64'>
- Related Articles
- Return the scalar type of highest precision of the same kind as the input in Python
- Return the data type with the smallest size and scalar kind to which both the given types be safely cast in Python
- Return the scalar dtype or NumPy equivalent of Python type of an object
- Return the string representation of a scalar dtype in Python
- Return True if cast between scalar and data type can occur according to the casting rule in Python
- Return the result of the power to which the input value is raised with scimath in Python
- Return True if cast between array scalar and data type can occur according to the casting rule in Python
- Return the common filling value of two masked arrays in Numpy
- Find the minimal data type of a scalar value in Python
- Return the result of the negative power to which the input value is raised with scimath in Python
- Return the result of the power to which the negative input value is raised with scimath in Python
- Determine whether the given object represents a scalar data-type in Python
- Python Pandas - Return a numpy timedelta64 array scalar view in nanoseconds
- intersection_update() in Python to find common elements in n arrays
- Python program to find common elements in three sorted arrays?

Advertisements