我刚刚安装了 skflow 和 TensorFlow,我在使用 skflow 附带的示例时遇到了问题。示例代码是:
import random
import pandas
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.cross_validation import train_test_split
import tensorflow as tf
import skflow
data = pandas.read_csv('tf_examples/data/titanic_train.csv')
# Use SciKit Learn
y, X = data['Survived'], data[['Age', 'SibSp', 'Fare']].fillna(0)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
lr = LogisticRegression()
lr.fit(X_train, y_train)
print accuracy_score(lr.predict(X_test), y_test)
# 3 layer neural network with rectified linear activation.
random.seed(42)
classifier = skflow.TensorFlowDNNClassifier(hidden_units=[10, 20, 10],
n_classes=2, batch_size=128, steps=500,
learning_rate=0.05)
classifier.fit(X_train, y_train)
print accuracy_score(classifier.predict(X_test), y_test)
当我运行这个例子时,我得到:
python Example1.py
0.664804469274
Traceback (most recent call last):
File "Example1.py", line 27, in <module>
classifier.fit(X_train, y_train)
File "//anaconda/lib/python2.7/site-packages/skflow/__init__.py", line 119, in fit
self._setup_data_feeder(X, y)
File "//anaconda/lib/python2.7/site-packages/skflow/__init__.py", line 71, in _setup_data_feeder
self.n_classes, self.batch_size)
File "//anaconda/lib/python2.7/site-packages/skflow/data_feeder.py", line 61, in __init__
x_dtype = np.int64 if X.dtype == np.int64 else np.float32
File "//anaconda/lib/python2.7/site-packages/pandas/core/generic.py", line 2246, in __getattr__
(type(self).__name__, name))
AttributeError: 'DataFrame' object has no attribute 'dtype'
故障发生在:
classifier.fit(X_train, y_train)
任何帮助将不胜感激。
最佳答案
我认为这是 skflow 和 pandas 之间的接口(interface)问题。在将数据帧传递给 skflow 之前,尝试在数据帧上调用 .values
:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
lr = LogisticRegression()
lr.fit(X_train.values, y_train.values)
print accuracy_score(lr.predict(X_test.values), y_test.values)
# 3 layer neural network with rectified linear activation.
random.seed(42)
classifier = skflow.TensorFlowDNNClassifier(hidden_units=[10, 20, 10],
n_classes=2, batch_size=128, steps=500,
learning_rate=0.05)
classifier.fit(X_train.values, y_train.values)
print accuracy_score(classifier.predict(X_test.values), y_test.values)
关于tensorflow - 运行 skflow 示例时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34168437/