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.
Videos
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)
Update: This function only avoids copy if it can, hence this is not the correct answer for this question. unutbu's answer is the right one.
a = a.astype(numpy.float32, copy=False)
numpy astype has a copy flag. Why shouldn't we use it ?
You can make a view with a different dtype, and then copy in-place into the view:
import numpy as np
x = np.arange(10, dtype='int32')
y = x.view('float32')
y[:] = x
print(y)
yields
array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.], dtype=float32)
To show the conversion was in-place, note that copying from x to y altered x:
print(x)
prints
array([ 0, 1065353216, 1073741824, 1077936128, 1082130432,
1084227584, 1086324736, 1088421888, 1090519040, 1091567616])
Providing a simple example will explain it better.
>>> a = np.array([[1,2,3],[4,5,6]])
array([[1, 2, 3],
[4, 5, 6]])
>>> a[:,1]
array([2, 5])
>>> a[:,1].astype('str') # This generates copy and then cast.
array(['2', '5'], dtype='<U21')
>>> a # So the original array did not change.
array([[1, 2, 3],
[4, 5, 6]])
Let me answer the second question since I have met the same problem.
As dinarkino mentioned, just assign the type back won't work.
>>> X = np.array([[1.1,2.2],[3.3,4.4]])
>>> print(X[:,1].dtype)
<class 'numpy.float64'>
>>> X[:,1] = X[:,1].astype('str')
>>> print(X[:,1].dtype)
<class 'numpy.float64'>
So my approach is to assign the dtypes of the whole matrix to 'object' first, then assign the str datatype back.
>>> X = X.astype('object')
>>> print(type(X[0,1]))
<class 'float'>
>>> X[:,1] = X[:,1].astype('str')
>>> print(type(X[0,1]))
<class 'str'>