Python Forum

Full Version: Why do floats get turned to scientific notation when used to populate an array?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to populate an array with a data set of numbers that are imported as strings. I start by converting them to floats, and then put the floats into an array.

After the stage of float conversion, they appear correctly as floats.
However, after the stage of insertion into the numpy array, they appear as scientific notation.

The code is here:

import csv
import numpy as np 
import matplotlib.pyplot as plt

with open('ex1data2.txt', 'r') as f:
	data = list(csv.reader(f))

dat = np.zeros((len(data), len(data[0])+1))

for i in xrange(len(dat)):
	dat[i][0] = 1.
	for j in xrange(1, len(dat[i])):
		val = float(data[i][j-1])
		print val
		dat[i][j] = val

print dat
As can be seen if the code is run on a .txt data set, when 'val' is printed they are clear floats. However, when they are inserted into the array, using dat[i][j] = val, they are converted into scientific notation automatically, which can be seen from printing the array.

Why does this happen and how can it be prevented?
Try this

dat = np.zeros((len(data), len(data[0])+1), dtype=np.float)
This is probably just a representation issue.  Even if it displays in scientific notation it is still a float.  Scientific notation is not a datatype unto itself:
>>> a = 0.0000006
>>> a
6e-07
>>> type(a)
<class 'float'>
Of course if you need to you can force this to display however you desire:
>>> print("{:.10f}".format(a))
0.0000006000
>>>