Either you find a way to get the output with the correct type or you use astype, see the docs, in order to change the type of an array
In your case the following example gives you an array of type np.int
c=random.randn(5,5).astype(np.int)
Answer from plonser on Stack OverflowEither you find a way to get the output with the correct type or you use astype, see the docs, in order to change the type of an array
In your case the following example gives you an array of type np.int
c=random.randn(5,5).astype(np.int)
Also: np.castint, which doesn't presume it's argument is a NumPy array.
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.
You can use astype(int) to convert it to an int array after the calculation
import numpy as np
array1 = np.array([1, 2, 3])
print(array1.dtype)
#output: int64
array2 = np.array([2, 3, 4])
print(array2.dtype)
#output: int64
array3 = array1 / array2
print(array3.dtype)
#output: float64
array4 = array3.astype(int)
print(array3.dtype)
#output: int64
You could also do that inside of your calculation by working with brackets:
array3 = (array1 / array2).astype(int)