Jan van der Hoef
Jan van der Hoef

Reputation: 53

Numpy load.txt does not convert value to integer

If I run the following code on a windows laptop with numpy 1.16.2, it works. However, when I run this code on a mac with numpy 1.16.4 or 1.16.2, it doesn't and gives the following error:

invalid literal for int() with base 10: '34.623659...'

We have tried installing different versions of numpy on the mac device, however, the same thing happens. Changing int to float works, however, we would like to display the values in integer form.

The command we are using:

A = np.loadtxt('ex2data1.txt', delimiter=',', dtype ='int', max_rows = 5)
print(A)

With the first lines of the text file being:

34.62365962451697,78.0246928153624,0
30.28671076822607,43.89499752400101,0
35.84740876993872,72.90219802708364,0
60.18259938620976,86.30855209546826,1
79.0327360507101,75.3443764369103,1
45.08327747668339,56.3163717815305,0
61.10666453684766,96.51142588489624,1
75.02474556738889,46.55401354116538,1
76.09878670226257,87.42056971926803,1
84.43281996120035,43.53339331072109,1
95.86155507093572,38.22527805795094,0
75.01365838958247,30.60326323428011,0
82.30705337399482,76.48196330235604,1
69.36458875970939,97.71869196188608,1
39.53833914367223,76.03681085115882,0
53.9710521485623,89.20735013750205,1

We expected it to return an array with the same values as in the text file but in integer form, however, we receive the error above.

Upvotes: 5

Views: 6245

Answers (1)

U13-Forward
U13-Forward

Reputation: 71580

You need float:

A = np.loadtxt('ex2data1.txt', delimiter=',', dtype =float, max_rows = 5).astype(int)

Upvotes: 6

Related Questions