开发者社区> 问答> 正文

Sklearn与Keras的比较— Keras带来的小错误

我正在测试下面的代码。

#%matplotlib inline
import seaborn as sns
import pandas as pd
import numpy as np
from sklearn.model_selection import cross_validate
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegressionCV


iris = sns.load_dataset("iris")
iris.head()

sns.pairplot(iris, hue='species')

X = iris.values[:, 0:4]
y = iris.values[:, 4]

train_X, test_X, train_y, test_y = train_test_split(X, y, train_size=0.5, random_state=0)

lr = LogisticRegressionCV()
lr.fit(train_X, train_y)

pred_y = lr.predict(test_X)
print("Test fraction correct (Accuracy) = {:.2f}".format(lr.score(test_X, test_y)))
# Test fraction correct (Accuracy) = 0.93


import keras
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.utils import np_utils


train_y_ohe = pd.get_dummies(train_y)
test_y_ohe = pd.get_dummies(test_y)


model = Sequential()
model.add(Dense(16, input_shape=(4,)))
model.add(Activation('sigmoid'))
model.add(Dense(3))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam')


loss, accuracy = model.evaluate(test_X, test_y_ohe, show_accuracy=True, verbose=0)
print("Test fraction correct (Accuracy) = {:.2f}".format(accuracy))

一切正常,直到代码的倒数第二行。

当我尝试运行此命令时:

loss, accuracy = model.evaluate(test_X, test_y_ohe, show_accuracy=True, verbose=0)

我收到此错误:

TypeError: evaluate() got an unexpected keyword argument 'show_accuracy'

我做了一些研究,发现'show_accuracy = True'可能在不久前已贬值。现在还有其他方法吗?我如何评估和打印模型的准确性?

我在这里找到了代码示例:

https://blog.fastforwardlabs.com/2016/02/24/hello-world-in-keras-or-scikit- 学习,versus.html

问题来源:stackoverflow

展开
收起
is大龙 2020-03-24 12:13:32 1519 0
1 条回答
写回答
取消 提交回答
  • 在新版本的keras中,不推荐使用show_accuracy参数,将其从model.evaluate()中删除,而在model.compile()中使用metrics [[accuracy]]

    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    
    # fit model
    train_y_ohe = pd.get_dummies(train_y)
    model.fit(train_X, train_y_ohe,epochs=1000,batch_size=20)
    
    loss, accuracy = model.evaluate(test_X, test_y_ohe, verbose=0)
    print("Test fraction correct (Accuracy) = {:.2f}".format(accuracy))
    
    #Test fraction correct (Accuracy) = 0.97
    

    回答来源:stackoverflow

    2020-03-24 12:13:39
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载