Dec-10-2017, 05:47 PM
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:
Why does this happen and how can it be prevented?
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 datAs 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?