运行skflow示例时出错

时间:2015-12-09 00:02:52

标签: tensorflow skflow

我刚安装了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)

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:4)

我认为这是skflow和pandas之间接口的问题。尝试在数据框上调用.values,然后再将它们传递给skflow:

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)

答案 1 :(得分:0)

感谢您使用skflow!我们很久以前就加入了熊猫支持。您可以找到具体的实施io/pandas_io.py in skflow

我们的更多示例现在使用pandas来加载数据,例如文本分类示例,如this one

希望这对使用skflow有帮助和快乐!