0

cutting right to the chase, here's what happened:

I accidentally saved a numpys array using .tofile() (rather than .save()). Then, I couldn't (but should have) load it using .load(), but only using .fromfile().
The problem occurred when I tried to convert it, as I've forgotten to load the array specifying the type int (which left it to default - float).

What should have happened

import numpy as np

PATH = 'test.npy'

x = np.array([[100, 200, 300], [400, 500, 600]])
print('x', x)
x.tofile(PATH)

# pay close attention to `dtype=int`
y = np.fromfile(PATH, dtype=int)
print('y', y)
np.save(PATH, y)

z = np.load(PATH)
print('z', z)

The output:

x [[100 200 300]
 [400 500 600]]
y [100 200 300 400 500 600]
z [100 200 300 400 500 600]

What actually happened:

import numpy as np

PATH = 'test.npy'

x = np.array([[100, 200, 300], [400, 500, 600]])
print('x', x)
x.tofile(PATH)

# here, no `dtype` is specified
y = np.fromfile(PATH)
print('y', y)
np.save(PATH, y)

z = np.load(PATH)
print('z', z)
print('z', z.astype(int))

The output:

x [[100 200 300]
 [400 500 600]]
y [4.941e-322 9.881e-322 1.482e-321 1.976e-321 2.470e-321 2.964e-321]
z [4.941e-322 9.881e-322 1.482e-321 1.976e-321 2.470e-321 2.964e-321]
z [0 0 0 0 0 0]

I tried raising the number to the power, with no success.

Is there any solution to saving the file?
The original file was lost, I have the overwritten (damaged) file only.

user5618793
  • 101
  • 5
  • 13

1 Answers1

0

Answer (courtesy of @hpaulj): z.view(int)

Full solution:

import numpy as np

PATH = 'test.npy'

x = np.array([[100, 200, 300], [400, 500, 600]])
print('x', x)
x.tofile(PATH)

# here, no `dtype` is specified
y = np.fromfile(PATH)
print('y', y)
np.save(PATH, y)

z = np.load(PATH)
# solution
print('z', z.view(int))
print('z', z.view(int).reshape(-1, 3))

With the output:

x [[100 200 300]
 [400 500 600]]
y [4.941e-322 9.881e-322 1.482e-321 1.976e-321 2.470e-321 2.964e-321]
z [100 200 300 400 500 600]
z [[100 200 300]
 [400 500 600]]
user5618793
  • 101
  • 5
  • 13