我只是想让一些代码工作,但我一直在收到错误。
代码是
import tensorflow.contrib.learn as skflow
from sklearn import datasets, metrics
iris = datasets.load_iris()
classifer = skflow.LinearClassifier(n_classes=3)
classifier.fit(iris.data, iris.target)
score = metrics.accuracy_score(iris.target, classifier.predict(iris.data))
print ("Accuracy: %f" % score)
但我收到此错误(研究和修改后的最新错误)
Traceback (most recent call last):
File "E:\Users\black\Python\machine-learning\MLappIris.py", line 6, in <module>
classifer = skflow.LinearClassifier(n_classes=3)
TypeError: __init__() missing 1 required positional argument: 'feature_columns'
非常感谢任何帮助。
答案 0 :(得分:0)
您必须定义feature_columns
- 这是完整的脚本:
import tensorflow as tf
import tensorflow.contrib.learn as skflow
from sklearn import datasets, metrics
iris = datasets.load_iris()
feature_columns = skflow.infer_real_valued_columns_from_input(iris.data)
# WARNING:tensorflow:float64 is not supported by many models, consider casting to float32.
classifier = skflow.LinearClassifier(feature_columns=feature_columns, n_classes=3)
classifier.fit(x=iris.data, y=iris.target, steps=20000)
predictions = list(classifier.predict(iris.data, as_iterable=True))
score = metrics.accuracy_score(iris.target, predictions)
print ("Accuracy: %f" % score)
# Accuracy: 0.980000
或者,您也可以使用evaluate
方法使用单个命令进行评估:
accuracy_score = classifier.evaluate(iris.data, iris.target)["accuracy"]
print ("Accuracy: %f" % accuracy_score)
# Accuracy: 0.980000
警告是由于iris.data
为float64
- 它似乎不会影响此处的任何内容,但您始终可以将其投放到float32
。
tf.contrib.learn
quickstart以及此简短tutorial也可能有用。
经过测试: