3

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

任何帮助将不胜感激。

4

2 回答 2

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)
于 2015-12-09T15:24:41.870 回答
0

感谢您使用 skflow!我们很久以前就添加了对 pandas 的支持。可以在 skflow 中找到具体的实现 io/pandas_io.py

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

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

于 2016-02-15T05:58:49.143 回答