- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
SciPy - convert_temperature() Method
The SciPy convert_temperature() method is used to calculate the temperature scale in the form of any one scales such as Celcius, Kelvin, Fahrenheit, and Rankine.
Following is the key understanding of various scales −
- Celcius − Unit of temperature.
- Farenheit − This is a scale of temperature when water freezes at 32 degree and boils at 212 degree.
- Kelvin − This is unit of temperature when 0 reflects.
- Rankine − The absolute unit of temperature.
Syntax
Following is the syntax of the SciPy convert_temperature() method −
convert_temperature(val, old_scale, new_scale)
Parameters
This method accepts the following parameters −
- val− Specify the temperature range value.
- old_scale− Specify the original string.
- new_scale− Specify the new string in which the temperature will be converted.
Return value
This function returns an array of float values.
Example 1
Following is the basic SciPy convert_temperature() method illustrates the conversion of Celcius, Farenheit, and Kelvin.
import scipy.constants as const
def convert_temperature(value, from_unit, to_unit):
if from_unit == to_unit:
return value
# Convert the input temperature to Celsius first
if from_unit == 'Celsius':
temp_c = value
elif from_unit == 'Fahrenheit':
temp_c = (value - 32) * 5/9
elif from_unit == 'Kelvin':
temp_c = value - const.zero_Celsius
else:
raise ValueError("Unsupported temperature unit")
# Convert from Celsius to the desired unit
if to_unit == 'Celsius':
return temp_c
elif to_unit == 'Fahrenheit':
return temp_c * 9/5 + 32
elif to_unit == 'Kelvin':
return temp_c + const.zero_Celsius
else:
raise ValueError("Unsupported temperature unit")
# Show the result
print(convert_temperature(100, 'Celsius', 'Fahrenheit'))
print(convert_temperature(32, 'Fahrenheit', 'Celsius'))
print(convert_temperature(273, 'Kelvin', 'Celsius'))
Output
The above code produces the following result −
212.0 0.0 -0.14999999999997726
Example 2
Below the example uses some more scipy constant to convert the base unit into temperature unit.
import scipy.constants as const
def convert_temperature(value, from_unit, to_unit):
if from_unit == to_unit:
return value
# convert input to Kelvin
if from_unit == 'Celsius':
temp_k = value + const.zero_Celsius
elif from_unit == 'Fahrenheit':
temp_k = (value - 32) * 5/9 + const.zero_Celsius
elif from_unit == 'Kelvin':
temp_k = value
else:
raise ValueError("Unsupported temperature unit")
# convert Kelvin to the desired unit
if to_unit == 'Celsius':
return temp_k - const.zero_Celsius
elif to_unit == 'Fahrenheit':
return (temp_k - const.zero_Celsius) * 9/5 + 32
elif to_unit == 'Kelvin':
return temp_k
else:
raise ValueError("Unsupported temperature unit")
# Show the result
print(convert_temperature(100, 'Celsius', 'Fahrenheit'))
print(convert_temperature(32, 'Fahrenheit', 'Kelvin'))
print(convert_temperature(273.15, 'Kelvin', 'Celsius'))
Output
The above code produces the following result −
212.0 273.15 0.0
scipy_reference.htm
Advertisements