Python Forum
Why do floats get turned to scientific notation when used to populate an array?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why do floats get turned to scientific notation when used to populate an array?
#1
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?
Reply
#2
Try this

dat = np.zeros((len(data), len(data[0])+1), dtype=np.float)
Reply
#3
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
>>>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert Strings to Floats samalombo 2 2,547 Jun-08-2020, 10:45 AM
Last Post: hussainmujtaba
  How to use the excel filename as a value to populate new column, using Pandas? Dequanharrison 5 6,499 Jun-26-2019, 11:11 PM
Last Post: Dequanharrison

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020