Using NumPy to Convert Array Elements to Float Type
Last Updated :
30 Jun, 2025
Converting array elements to float type in Python means transforming each item in an array into a floating-point number. For example, an array like ["1.1", "2.2", "3.3"] contains string representations of numbers, which need to be converted to floats for mathematical operations. Let's explore different methods to do this efficiently.
Using astype(float)
astype() method creates a new array with a specified data type. It does not modify the original array but instead returns a copy with the new type.
Python
import numpy as np
a = np.array(["1.1", "2.2", "3.3"])
res = a.astype(float)
print(res)
Explanation: Here, a string array a is converted to a float array res using astype(float), creating a new array without modifying the original.
Using dtype=float
NumPy allows you to define the type of elements directly during the creation of the array using the dtype parameter. This ensures all elements are stored as floats from the beginning.
Python
import numpy as np
a = np.array(["1.1", "2.2", "3.3"], dtype=float)
print(a)
Explanation: Here, a string list is directly converted to a NumPy float array a by specifying dtype=float during array creation, eliminating the need for separate type conversion.
Using np.float64()
np.float64() is a NumPy universal function (ufunc) that converts the elements of an array into 64-bit floating-point numbers. It is highly optimized and suited for numeric data.
Python
import numpy as np
a = np.array([1, 2, 3])
res = np.float64(a)
print(res)
Explanation: Here, an integer array a is converted to a float array res using np.float64(), which casts the elements to 64-bit floating-point numbers efficiently.
In-Place Reassignment using astype()
This approach reuses the astype() method like before, but here you overwrite the original array with the converted one. It’s a form of in-place reassignment, not true in-place conversion which NumPy doesn’t support due to fixed data types .
Python
import numpy as np
a = np.array([100, 200, 300])
a = a.astype(float)
print(a)
Explanation: Here, a is converted to float using astype(float) and reassigned to itself, updating the array without keeping the original.
Using np.vectorize(float)
np.vectorize() turns a regular Python function like float() into a NumPy-style function that can operate element-wise over arrays. Though not as fast as true vectorized operations, it's flexible.
Python
import numpy as np
a = np.array(["15", "16", "17"])
res = np.vectorize(float)(a)
print(res)
Explanation: Here, np.vectorize(float) applies float() to each element of a, converting the string array to float element-wise.
Related Articles
Similar Reads
How to convert NumPy array to list ? This article will guide you through the process of convert a NumPy array to a list in Python, employing various methods and providing detailed examples for better understanding. Convert NumPy Array to List There are various ways to convert NumPy Array to List here we are discussing some generally us
4 min read
How to get element-wise true division of an array using Numpy? True Division in Python3 returns a floating result containing the remainder of the division. To get the true division of an array, NumPy library has a function numpy.true_divide(x1, x2). This function gives us the value of true division done on the arrays passed in the function. To get the element-w
2 min read
Convert Python List to numpy Arrays NumPy arrays are more efficient than Python lists, especially for numerical operations on large datasets. NumPy provides two methods for converting a list into an array using numpy.array() and numpy.asarray(). In this article, we'll explore these two methods with examples for converting a list into
4 min read
How to Convert NumPy Matrix to Array In NumPy, a matrix is essentially a two-dimensional NumPy array with a special subclass. In this article, we will see how we can convert NumPy Matrix to Array. Also, we will see different ways to convert NumPy Matrix to Array. Convert Python NumPy Matrix to an ArrayBelow are the ways by which we can
3 min read
How to Convert images to NumPy array? Pictures on a computer are made of tiny dots called pixels. To work with them in Python, we convert them into numbers using a NumPy array is a table of numbers showing each pixelâs color. In this article, weâll learn how to do this using popular Python tools.Loading the images via Pillow LibraryLet
5 min read
How to convert a list and tuple into NumPy arrays? In this article, let's discuss how to convert a list and tuple into arrays using NumPy. NumPy provides various methods to do the same using Python. Example: Input: [3, 4, 5, 6]Output: [3 4 5 6]Explanation: Python list is converted into NumPy ArrayInput: ([8, 4, 6], [1, 2, 3])Output: [[8 4 6] [1 2 3]
2 min read