2017-04-23 4 views
2

Ich möchte die Float-Werte der Liste speichern. Die Werte werden aus der CSV-Datei extrahiert.Python - Konvertieren von String-Werten der Liste in Float-Werte

Der Code, den ich geschrieben habe:

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)) 

Der Fehler begegnete ich bin mit ist:

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: '.' 
+0

Willkommen bei Stackoverflow. Bitte lesen Sie [Wie stelle ich eine gute Frage?] (Http://stackoverflow.com/help/how-to-ask) und [Wie erstelle ich ein minimales, vollständiges und überprüfbares Beispiel] (http: // stackoverflow. com/help/mcve), dann komm zurück und umformuliere deine Frage –

+0

Ich würde sagen, dass "e" ist schon eine Zeichenfolge, die einen Float darstellt, so dass die Umwandlung (Art) funktioniert, bis es auf dem Punkt erstickt. Versuchen Sie 'x1 = [float (n) für n in data1]' –

+0

Same Error wieder –

Antwort

3

wenn Sie dies tun:

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

data1 ist eine Liste von Zeichenketten

So

dann tun Sie dies:

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

Sie auf Strings sind laufen, und dann auf die Zeichen der Zeichenfolge. Also Umwandlung (Art von) funktioniert für die ersten Ziffern des ersten Schwimmers, dann Drosseln auf . (ex: "3.1416": 3 wird in einen Float umgewandelt (funktioniert, auf eine lustige Weise), aber dann begegnen Sie . und es zum Glück scheitert) .

Sie haben gerade vergessen, Ihre Zeile nach dem CSV-Trennzeichen zu teilen.

würde ich das csv Modul verwenden, um die Zeilen in Zeilen für mich zu spalten und zu tun:

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] 
+0

Es funktionierte thanx –

Verwandte Themen