【问题标题】:Compare values in numpy array and Pandas dataframe比较 numpy 数组和 Pandas 数据框中的值
【发布时间】:2019-06-27 16:30:46
【问题描述】:

我有一个 numpy.ndarray 和 pandas DataFrame 类型的数组,需要一种方法来相互比较每个值。

以下是我尝试过的方法之一。我还使用pd.get(labels) 来提取值并返回None。 y_test 是一个 pandas 数据帧,preds 是一个 numpy 预测数组。

尝试将它们都转换为列表以及 numpy 数组进行比较:

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='binary_crossentropy',
              optimizer='adam',metrics=['accuracy'])

model.fit(x_train, y_train, epochs=5, batch_size=2000)

preds = model.predict(x_test)
preds[preds>=0.5] = 1
preds[preds<0.5] = 0
print(type(preds))
print(y_test.get('labels'))
total = 0
for i in range(len(preds)):
    if int(preds[i]) == y_test[i]:
        total = total + 1

这就是我得到的 - TypeError: 'NoneType' object is not callable - KeyError: 0

【问题讨论】:

    标签: pandas numpy dataframe


    【解决方案1】:

    所以如果 y_test 是一个数据框,那么你可以只要求这样的值来获得一个 numpy 数组:

    y_test_array = y_test["labels"].values
    

    然后打印出来知道有多少项是相等的:

    sum(y_test_array == preds)# number of items with same value
    sum(y_test_array == preds)/len(preds)# %
    

    我希望是你问的。

    【讨论】:

      【解决方案2】:

      我猜你想找到你的模型的性能,所以只需使用.evaluate() 方法,我假设你在这里使用的是 keras 模型。

      sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
      model.compile(loss='binary_crossentropy',
                    optimizer='adam',metrics=['accuracy'])
      
      model.fit(x_train, y_train, epochs=5, batch_size=2000)
      
      score= model.evaluate(x_test,y_test)
      

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-08
        • 2018-10-10
        • 1970-01-01
        • 1970-01-01
        • 2019-07-24
        • 2016-10-15
        • 1970-01-01
        • 2016-12-03
        相关资源
        最近更新 更多