ham_fields = np.array([], dtype=float)

ham_fields = data[data[:, 0] == 0] 
ham_sum = np.delete((ham_fields.sum(0)),0)  

This line assigns a new array object to ham_fields. The first assignment did nothing for you. In Python variables are not declared at the start.

If data has a int dtype, then so does ham_fields. You could change that with a another assignment

ham_fields = ham_fields.astype(float)

ham_sum has the same dtype as ham_fields, from which it's derived.

Assigning a float to an element of a int dtype array will not change the dtype.

    for i in range(ham_len):
        ham_sum[i] = (ham_sum[i] + self.alpha) / (ham_total + (ham_len * self.alpha))

If self.alpha, ham_total are scalar then you should be able to do

ham_sum = (ham_sum + self.alpha)/(ham_toal + (ham_len * self.alpha))

This makes a new array, which will be float, and assigns it to ham_sum variable. It's a new assignment (not modification) so the float dtype is preserved. Or to make things clear, assign it to a new variable name.

Answer from hpaulj on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › numpy › change-numpy-array-data-type
Change the Data Type of the Given NumPy Array - GeeksforGeeks
We will use the astype() function of the NumPy library to change the data type of the NumPy array.
Published   July 11, 2025
🌐
W3Schools
w3schools.com › python › numpy › numpy_data_types.asp
NumPy Data Types
The astype() function creates a copy of the array, and allows you to specify the data type as a parameter. The data type can be specified using a string, like 'f' for float, 'i' for integer etc. or you can use the data type directly like float ...
🌐
w3resource
w3resource.com › python-exercises › numpy › python-numpy-exercise-39.php
NumPy: Change the data type of an array - w3resource
# Importing the NumPy library with an alias 'np' import numpy as np # Creating a NumPy array 'x' with specified data type 'int32' x = np.array([[2, 4, 6], [6, 8, 10]], np.int32) # Printing the array 'x' print(x) # Printing the data type of array 'x' print("Data type of the array x is:", x.dtype) # Changing the data type of array 'x' to 'float' y = x.astype(float) # Printing the new data type of array 'y' print("New Type: ", y.dtype) # Printing the modified array 'y' with the new data type print(y)
🌐
Educative
educative.io › answers › how-to-convert-data-types-of-arrays-using-numpy-in-python
How to convert data types of arrays using NumPy in Python
We assign this to a variable we call my_array. We print the my_array variable. We use the dtype property to check for the data type. We make a copy of the array and use the array.astype() method to change its data type.
🌐
TutorialsPoint
tutorialspoint.com › change-data-type-of-given-numpy-array-in-python
Change data type of given numpy array in Python
January 2, 2020 - We have a method called astype(data_type) to change the data type of a numpy array. If we have a numpy array of type float64, then we can change it to int32 by giving the data type to the astype() method of numpy array.
🌐
GeeksforGeeks
geeksforgeeks.org › change-data-type-of-given-numpy-array
Change data type of given numpy array - GeeksforGeeks
August 9, 2021 - Solution : We will use numpy.astype() function to change the data type of the underlying data of the given numpy array.
Find elsewhere
🌐
Medium
mr-amit.medium.com › changing-data-type-dtype-in-numpy-arrays-a-step-by-step-guide-ea7e323e1959
Changing Data Type (dtype) in NumPy Arrays: A Step-by-Step Guide | by It's Amit | Medium
March 6, 2025 - What’s the fix? Clean your data before attempting the conversion. Remove or replace invalid entries to avoid these errors. 3. Can I Change dtype In-Place Without Creating a New Array? Unfortunately, no. NumPy doesn’t allow changing the dtype of an array in-place.
🌐
NumPy
numpy.org › devdocs › user › basics.types.html
Data types — NumPy v2.5.dev0 Manual
NumPy supports a much greater variety of numerical types than Python does. This section shows which are available, and how to modify an array’s data-type.
🌐
woteq
woteq.com › home › how to change the data type of a numpy array in python
How to Change the Data Type of a NumPy Array in Python - Woteq Zone
August 16, 2025 - For example, viewing an int32 array as a float64 array might not give the expected results, since each float would need twice the memory of each integer. You can also implicitly change the data type by performing mathematical operations. For instance, adding a floating-point number to an integer array will result in a floating-point array. import numpy as np arr = np.array([1, 2, 3]) print(f"Original array: {arr}, Data type: {arr.dtype}") float_arr = arr + 0.0 print(f"Converted array: {float_arr}, Data type: {float_arr.dtype}")
🌐
Scaler
scaler.com › home › topics › numpy › manipulating data types in numpy
Manipulating Data Types in NumPy - Scaler Topics
November 9, 2022 - ... A NumPy array in Python has a data type called dtype, which can be specified when creating an array using the np.array() function. To change the data type of an array, we use the astype() function that is provided by NumPy in Python.
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: astype() to change dtype of an array | note.nkmk.me
February 4, 2024 - However, in some cases, the result might differ in type from the original arrays. For processes where bit size is crucial, it is safer to explicitly convert to the desired type with astype() beforehand. print((a_int16 + a_float16).dtype) # float32 print((a_int32 + a_float32).dtype) # float64 ... Note that when assigning values to elements, the dtype does not change.
🌐
Vultr Docs
docs.vultr.com › python › third-party › numpy › array › astype
Python Numpy array astype() - Convert Data Type | Vultr Docs
November 8, 2024 - Use the astype() method to convert the array to another data type. ... import numpy as np original_array = np.array([1, 2, 3, 4]) converted_array = original_array.astype(float) print(converted_array) Explain Code · This code converts an array ...
🌐
Codecademy
codecademy.com › docs › python:numpy › ndarray › .astype()
Python:NumPy | ndarray | .astype() | Codecademy
July 18, 2024 - The .astype() function in NumPy allows changing the data type of the elements in an array. It is beneficial for tasks such as converting floating-point numbers to integers or changing integers to strings, ensuring that the data is in the desired ...
🌐
Moonbooks
en.moonbooks.org › Articles › How-to-change-matrix-type-using-numpy-on-python-
How to change array (or matrix) type with numpy in python ?
To change the type, a solution is to use astype (see numpy.ndarray.dtype) A = A.astype('float64') print(A) print(A.dtype) returns · [[10. 20. 30.] [60. 20. 10.] [50. 30. 90.]] and · float64 · It is also possible to specify the type of a matrix during the creation: import numpy as np A = ...