RuntimeWarning: invalid value encountered in cast quantized_data = ((data - min_value) / quantization_step).astype(int)

25 views Asked by At

I was running a scaling code in python for a weight matrix in Ubuntu but this OS had a crash so I run this code in windows now. The problem is that it pops this warning 16: RuntimeWarning: invalid value encountered in cast quantized_data = ((data - min_value) / quantization_step).astype(int) This was not happening in Linux and the code does give the expected results for the negative values.

import numpy as np

def quantize_data(data, num_levels):
    # Find the maximum and minimum values
    min_value = np.min(data)
    max_value = np.max(data)

    # Calculate the width of each quantization interval (bin)
    quantization_step = (max_value - min_value) / (num_levels - 1)  # -1 to ensure covering the entire range

    # Handle division by zero
    if np.isnan(data).any():
        print("Warning: NaN values encountered in data.")

    # Quantize the data
    quantized_data = ((data - min_value) / quantization_step).astype(int)
    print("Quatized Data",quantized_data)

    # Handle NaN values
   # quantized_data[np.isnan(quantized_data)] = 0  # Replace NaN values with 0

    return quantized_data


def count_binary_digits(quantized_data):
    # Initialize counters
    num_ones = 0
    num_zeros = 0

    # Iterate through each value in quantized_data
    for value in quantized_data.flat:
        # Convert the value to its binary representation
        binary_string = np.binary_repr(value)
        print("binary",binary_string)
        # Count the number of '1' and '0' digits in binary representation
        num_ones += binary_string.count('1')
        num_zeros += binary_string.count('0')

    return num_ones, num_zeros


# Example usage:
file_path = 'D:/Διπλωματική Μεταπτυχιακού/Diplomatiki/code_for_wait_plot/results_trainLen_1000/Wout_rc_1000.npy'
data = np.load(file_path)
print(data)
num_levels = 2**32  # Number of quantization levels

# Quantize the data
quantized_data = quantize_data(data, num_levels)

print(quantized_data)
# Count binary digits
num_ones, num_zeros = count_binary_digits(quantized_data)

print("Number of '1' digits:", num_ones)
print("Number of '0' digits:", num_zeros)

This is the line 16 quantized_data = ((data - min_value) / quantization_step).astype(int)

I have tried an online editor and it worked but is to slow

0

There are 0 answers