Let us see how to convert a list of Celsius temperatures into Fahrenheit by converting them into NumPy array.
The formula to convert Celsius to Fahrenheit is :
feh = (9 * cel / 5 + 32)
Method 1 : Using the numpy.array() method.
python3
import numpy as np
inp = [ 0 , 12 , 45.21 , 34 , 99.91 ]
cel = np.array(inp)
print (f"Celsius {cel}")
feh = ( 9 * cel / 5 + 32 )
print (f"Fahrenheit {feh}")
|
Output :
Celsius [ 0. 12. 45.21 34. 99.91]
Fahrenheit [ 32. 53.6 113.378 93.2 211.838]
Method 2 : Using the numpy.asarray() method.
python3
import numpy as np
inp = [ 0 , 12 , 45.21 , 34 , 99.91 ]
cel = np.asarray(inp)
print (f"Celsius {cel}")
feh = ( 9 * cel / 5 + 32 )
print (f"Fahrenheit {feh}")
|
Output :
Celsius [ 0. 12. 45.21 34. 99.91]
Fahrenheit [ 32. 53.6 113.378 93.2 211.838]
Method 3 : Using numpy.arange().
python3
import numpy as np
inp = [ 0 , 12 , 45.21 , 34 , 99.91 ]
cel = np.arange( 5 )
cel = [i for i in inp]
print (f"Celsius {cel}")
feh = [( 9 * i / 5 + 32 ) for i in cel]
print (f"Fahrenheit {feh}")
|
Output :
Celsius [ 0. 12. 45.21 34. 99.91]
Fahrenheit [ 32. 53.6 113.378 93.2 211.838]