Why Is the Following Giving Me Zero

Why is the following giving me zero?

This is because of integer division. Change Double(a/b * 100) to Double(a) / Double(b) * 100.

Why does i = i + i give me 0?

The issue is due to integer overflow.

In 32-bit twos-complement arithmetic:

i does indeed start out having power-of-two values, but then overflow behaviors start once you get to 230:

230 + 230 = -231

-231 + -231 = 0

...in int arithmetic, since it's essentially arithmetic mod 2^32.

Why does i = i + i give me 0?

The issue is due to integer overflow.

In 32-bit twos-complement arithmetic:

i does indeed start out having power-of-two values, but then overflow behaviors start once you get to 230:

230 + 230 = -231

-231 + -231 = 0

...in int arithmetic, since it's essentially arithmetic mod 2^32.

How is this code giving me 0 as value of i, instead of 20?

Java keeps the default constructor if no constructor is present, Hence let us keep the default constructor in the code to understand the code flow and value of i and j at each step:

See Below code:

package com.java;

public class InvalidValue {
private int i = giveMeJ();
private int j = 20;

// Default Constructor
public InvalidValue() {
super();
// Second Print: inside Constructor[i= 0, j= 20]
System.out.println("inside Constructor[i= " + i + ", j= " + j + "]");
}

private int giveMeJ() {
// First Print: inside giveMeJ[i= 0, j= 0]
System.out.println("inside giveMeJ[i= " + i + ", j= " + j + "]");
return j;
}

public static void main(String[] args) {
// Third Print: i: 0
System.out.println("i: " + new InvalidValue().i);
}
}
  1. Step 1: void main initiate the call by new InvalidValue().i,
  2. Step 2: i and j will be initiated by default value 0 then InvalidValue() constructor will be invoked and then super(); will be
    called,
  3. Step 3: Now the constructor (assign value to instance attributes) invokes private int i = giveMeJ(),
  4. Step 4: giveMeJ() method will be invoked and here value of i is 0 also value of j is 0, hence this method will return 0,
  5. step 5: i initialize by 0, now flow will go to private int j = 20,
  6. step 6: 20 value will be assigned to j,
  7. step 7: now the compiler will return to constructor (Step 3) and then it will print sysout statement (inside Constructor[i= 0, j=
    20]
    ),
  8. step 8: now compiler will return to void main() (Step 1) and it will print sysout statement (i: 0)

Why is casting to int giving me 8 instead of 0?

That happens because first is casting the value of a to int and then doing the module.

What you want to do is:

public class Main {
public static void main(String[] args) {
long a = 10000000000L;
System.out.println((int)(a % 10));
}
}

Why is this ML model giving me zero accuracy?

I faced the same issue while practicing with multi-class classification. Where I had 7 features and the model classifies into 7 classes. I tried encoding the labels and it fixed the issue.

First import LabelEncoder class from sklearn and import to_categorical from tensorflow

from sklearn.preprocessing import LabelEncoder
from tensorflow.keras.utils import to_categorical

Then, initialize an object to the LabelEncoder class and transform your labels before fitting and training the model.

encoder = LabelEncoder()
encoder.fit(y)
y = encoder.transform(y)
y = to_categorical(y)

Note that you have to use np.argmax for getting the actual predicted classification. in my case, the prediction is stored in variable called res

res = np.argmax(res, axis=None, out=None)

You can get your actual predicted class after this line. Looking forward to help you. Hope it solved your problem.



Related Topics



Leave a reply



Submit