numpy.genfromtxt返回NaN值

时间:2019-01-27 19:08:32

标签: python numpy matplotlib genfromtxt

我正在尝试绘制csv文件的第二列,但第二列返回nan值。如果我设置dtype = None或dtype = str,将返回最后的字符串值,导致在Y轴上以错误的方式绘制。任何评论将不胜感激。

csv中的第二列包含80-99%的值,这些值之前保存为该代码段

 numpy.savetxt('loss_acc_exp4.csv', numpy.c_[losses, ACCes], fmt=['%.4f', '%d %%'], header= "loss, acc", comments='', delimiter = ",")                    

以下是用于绘制csv文件的代码段

import numpy
import matplotlib.pyplot as plt
acc_history = numpy.genfromtxt("loss_acc_exp4.csv", delimiter=",", 
skip_header=1, usecols=(1))


num_epochs = 151
epochs = range(1, num_epochs)

plt.figure(figsize=(10,6))
plt.plot(epochs, acc_history[::2], '-b', label='Training accuracy') #plot odd rows
plt.plot(epochs, acc_history[1::2], '-r', label='Validation accuracy') # plot even rows
plt.legend()
plt.xlabel('Epoch')
plt.ylabel('accuracy')
plt.xlim(0.01,151)
plt.show()

csv中的数据如下所示

enter image description here

import pandas as pd


def convert_percent(val):
"""
Convert the percentage string to an actual floating point percent
- Remove %
- Divide by 100 to make decimal
"""
new_val = val.replace('%', '')
new_val = pd.Series([new_val]).astype(float)
return (new_val) / 100

acc_history = pd.read_csv("loss_acc_exp4.csv", delimiter=",", header=0, 
usecols=[1], dtype=None)
acc_history[:].apply(convert_percent)

它正在返回ValueError: ('setting an array element with a sequence.', 'occurred at index acc')

1 个答案:

答案 0 :(得分:0)

使用转换器:

def foo(astr):
    return float(astr[:-1])
In [296]: np.genfromtxt('test.csv', delimiter=',', converters={1:foo})
Out[296]: 
array([[ 0.8469, 99.    ],
       [ 0.3569, 98.    ],
       [ 0.9622, 97.    ],
       [ 0.4774, 96.    ],
       [ 0.381 , 95.    ]])