3

I am trying to calculate information from an array that contains integers, however when I do a calculation the results are foat's. How do I change the ndarry to accept 0.xxx numbers as a input. Currently I am only getting 0's. Here is the code I have been trying to get working:

        ham_fields = np.array([], dtype=float)   # dtype specifies the type of the elements
        ham_total = np.array([], dtype=float)  # dtype specifies the type of the elements
        ham_fields = data[data[:, 0] == 0]  # All the first column of the dataset doing a check if they are true or false
        ham_sum = np.delete((ham_fields.sum(0)),0)  # Boolean indices are treated as a mask of elements to remove none Ham items
        ham_total = np.sum(ham_sum)
        ham_len = len(ham_sum)



        for i in range(ham_len):
            ham_sum[i] = (ham_sum[i] + self.alpha) / (ham_total + (ham_len * self.alpha))
3
  • 2
    Your code is problematic. It seems you have sliced the main code without correcting it to be runnable. For example self which relate to a class definition. what is data? Commented Oct 23, 2021 at 15:10
  • Please show an MCVE as properly formatted code in the question. Commented Oct 23, 2021 at 15:15
  • you're not specifying a dtype when you create ham_sum: ham_sum = np.delete((ham_fields.sum(0)),0).astype(float) Commented Oct 23, 2021 at 15:18

2 Answers 2

5
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.

Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.