Environnement d'exploitation
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 14.04 LTS desktop amd64
TensorFlow v0.11
cuDNN v5.1 for Linux
CUDA v8.0
Python 2.7.6
IPython 5.1.0 -- An enhanced Interactive Python.
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
GNU bash, version 4.3.8(1)-release (x86_64-pc-linux-gnu)
Je veux lire la valeur d'un fichier comme le suivant.
$ head IntField-Y
x y z |E|^2 Ex.r Ex.i Ey.r Ey.i Ez.r Ez.i
-0.2094395102 -1.466076572 -5.235987756 1.117036835 -0.02405501404 0.01851210121 0.4265089085 0.9619413614 0.0789243352 -0.05143356113
0.2094395102 -1.466076572 -5.235987756 1.117036835 0.02405501406 -0.01851210123 0.4265089085 0.9619413614 0.0789243352 -0.05143356113
-1.047197551 -1.047197551 -5.235987756 1.080829273 -0.03701212691 0.09467516163 0.4467058558 0.9303997369 0.0629824475 -0.0365979255
-0.6283185307 -1.047197551 -5.235987756 0.8888575386 -0.01687903824 -0.0712841231 0.4031262231 0.8443906708 0.08152662539 -0.0365822794
-0.2094395102 -1.047197551 -5.235987756 1.355786123 0.000532696637 -0.07408332312 0.2568592009 1.131470229 0.05970715325 -0.02304404718
0.2094395102 -1.047197551 -5.235987756 1.355786123 -0.0005326966253 0.0740833231 0.256859201 1.131470229 0.05970715325 -0.02304404718
0.6283185307 -1.047197551 -5.235987756 0.8888575386 0.01687903825 0.07128412309 0.4031262231 0.8443906708 0.08152662538 -0.0365822794
1.047197551 -1.047197551 -5.235987756 1.080829273 0.03701212691 -0.09467516164 0.4467058558 0.9303997368 0.06298244747 -0.03659792549
-1.047197551 -0.6283185307 -5.235987756 1.699276704 -0.02108930862 -0.06464253993 0.3399972729 1.255779421 0.04140882396 -0.01893399186
Le délimiteur est un blanc, pas une virgule.
http://www.mwsoft.jp/programming/numpy/csv.html
test_read_170418a.py
import numpy as np
data = np.loadtxt('IntField-Y', delimiter=' ')
print(data[:,0]) # 1st columne
print(data[:,1]) # 2nd columne
run
$ python test_read_170418a.py
Traceback (most recent call last):
File "test_read_170418a.py", line 9, in <module>
data = np.loadtxt('IntField-Y', delimiter=' ')
File "/home/user/tensorflow-GPU/local/lib/python2.7/site-packages/numpy/lib/npyio.py", line 930, in loadtxt
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "/home/user/tensorflow-GPU/local/lib/python2.7/site-packages/numpy/lib/npyio.py", line 659, in floatconv
return float(x)
ValueError: could not convert string to float: x
[1]+Terminer le sous-test_read_170418a.py
Ligne du nom de l'élément(x y z |E|^2 Ex.r Ex.i Ey.r Ey.i Ez.r Ez.i
)J'obtiens une erreur.
test_read_170419a.py
import numpy as np
#data = np.loadtxt('IntField-Y', delimiter=' ')
data = np.genfromtxt('IntField-Y', delimiter=' ')
print(data[:,0]) # 1st columne
print(data[:,1]) # 2nd columne
run
$ python test_read_170419a.py
[ nan -0.20943951 0.20943951 ..., 1.04719755 -0.20943951
0.20943951]
[ nan -1.46607657 -1.46607657 ..., 1.04719755 1.46607657
1.46607657]
En utilisant genfromtxt (), j'ai pu lire un fichier de données séparé par des blancs avec une ligne de noms d'éléments. La chaîne de caractères dans la ligne du nom de l'élément est nan, mais il n'y a pas de problème car elle n'est pas traitée comme des données.
Recommended Posts