2017-04-23 241 views
2

我想存儲列表的浮點值。這些值是從csv文件中提取的。Python - 將列表的字符串值轉換爲浮點值

我寫的代碼:

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

我所遇到的錯誤是:

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

歡迎StackOverflow上。請閱讀[我如何問一個好問題?](http://stackoverflow.com/help/how-to-ask)和[如何創建最小,完整和可驗證的示例](http:// stackoverflow。 com/help/mcve),然後回來重新編寫你的問題 –

+0

我會說'e'已經是一個表示浮點數的字符串了,所以轉換(種類)一直工作,直到它在點上扼殺。嘗試'x1 = [float(n)for n in data1]' –

+0

再次出現同樣錯誤 –

回答

3

,當你這樣做:

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

data1列表字符串

,那麼你這樣做:

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

你遍歷對字符串,然後在字符的字符串。因此,轉換(種類)適用於第一個浮點數的第一個數字,然後在.(例如:"3.1416"3)上的扼流圈被轉換爲浮點數(有趣的工作方式),但後來遇到.,幸運的是失敗) 。

你只是忘了根據csv分隔符分割你的行。

我會用csv模塊到線到行分割爲我做:

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

它比thanx –

相關問題