2

I want to store the float values of list. The values are extracted from csv file.

The code I have written:

import numpy as np

import csv
from sklearn import datasets, metrics
from sklearn.model_selection import train_test_split
from neupy import algorithms, environment

environment.reproducible()

data1 = open('data.csv','r').read().split("\n")

target1 = open('target.csv','r').read().split("\n")

x1 = [[float(n) for n in e] for e in data1 ]
y1 = [[float(s) for s in f] for f in target1 ]
x_train, x_test, y_train, y_test = train_test_split(x1,y1,train_size=0.7)

pnn = algorithms.PNN(std=10,verbose=False)

pnn.train(x_train, y_train)
y_predicted = pnn.predict(x_test)
print(metrics.accuracy_score(y_test, y_predicted))

The error I am encountered with is:

WARNING (theano.configdefaults): g++ not detected ! Theano will be 
unable to execute optimized C-implementations (for both CPU and GPU) 
and will default to Python implementations. Performance will be 
severely degraded. To remove this warning, set Theano flags cxx to
an empty string.

Traceback (most recent call last):
  File "C:\Users\pc\AppData\Local\Programs\Python\Python36-32\pnn-3.py", line 16, in <module>
    x1 = [[float(n) for n in e] for e in data1 ]
  File "C:\Users\pc\AppData\Local\Programs\Python\Python36-32\pnn-3.py", line 16, in <listcomp>
    x1 = [[float(n) for n in e] for e in data1 ]
  File "C:\Users\pc\AppData\Local\Programs\Python\Python36-32\pnn-3.py", line 16, in <listcomp>
    x1 = [[float(n) for n in e] for e in data1 ]
ValueError: could not convert string to float: '.'
3

1 Answer 1

3

when you do this:

data1 = open('data.csv','r').read().split("\n")

data1 is a list of strings

So then you do this:

x1 = [[float(n) for n in e] for e in data1 ]

you're iterating on strings, and then on the characters of the string. So conversion (kind of) works for the first digits of the first float, then chokes on . (ex: "3.1416": 3 is converted to a float (works, in a funny way), but then you encounter . and it fortunately fails).

You just forgot to split your line according to the csv separator.

I would use the csv module to split the lines into rows for me and do:

with open('data.csv','r') as f:
   cr = csv.reader(f,delimiter=";")  # change to whatever your delimiter is
   x1 = [[float(n) for n in row] for row in cr]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.