我正在尝试使用Tensor Flow构建一个简单的逻辑回归,但是我收到了这个错误:
TypeError: 'Series' objects are mutable, thus they cannot be hashed
这是我的代码:
data = pd.read_csv(data_file,sep=";",names=header)
...
n = data.shape[0]
n_training_set = int(.7*n)
df_train = data[30:n_training_set]
df_test = data[n_training_set:]
LABEL_COLUMN = 'action'
CONTINUOUS_COLUMNS = ['rsi','stochk','stochd']
CATEGORICAL_COLUMNS = []
def input_fn(df):
# Creates a dictionary mapping from each continuous feature column name (k) to
# the values of that column stored in a constant Tensor.
continuous_cols = {k: tf.constant(df[k].values)
for k in CONTINUOUS_COLUMNS}
# Creates a dictionary mapping from each categorical feature column name (k)
# to the values of that column stored in a tf.SparseTensor.
categorical_cols = {k: tf.SparseTensor(
indices=[[i, 0] for i in range(df[k].size)],
values=df[k].values,
dense_shape=[df[k].size, 1])
for k in CATEGORICAL_COLUMNS}
# Merges the two dictionaries into one.
feature_cols = dict(continuous_cols.items() + categorical_cols.items())
# Converts the label column into a constant Tensor.
label = tf.constant(df[LABEL_COLUMN].values)
# Returns the feature columns and the label.
return feature_cols, label
def train_input_fn():
return input_fn(df_train)
def eval_input_fn():
return input_fn(df_test)
model_dir = tempfile.mkdtemp()
CONTINUOUS_COLUMNS = ['rsi','stochk','stochd']
rsi = tf.contrib.layers.real_valued_column(df_train["rsi"])
stochk = tf.contrib.layers.real_valued_column(df_train["stochk"])
stochd = tf.contrib.layers.real_valued_column(df_train["stochd"])
### defining the model
m = tf.contrib.learn.LinearClassifier(feature_columns=[
rsi,
stochk,
stochd
],
model_dir=model_dir)
m.fit(input_fn=train_input_fn, steps=200)
我该如何解决这个问题?
答案 0 :(得分:1)
你的错误就在附近:
rsi = tf.contrib.layers.real_valued_column(df_train["rsi"])
stochk = tf.contrib.layers.real_valued_column(df_train["stochk"])
stochd = tf.contrib.layers.real_valued_column(df_train["stochd"])
这里您从pandas数据框中作为第一个参数传递一个列,但第一个参数tot real_valued_column应该是该列的名称。因此将上面的行替换为:
rsi = tf.contrib.layers.real_valued_column("rsi")
stochk = tf.contrib.layers.real_valued_column("stochk")
stochd = tf.contrib.layers.real_valued_column("stochd")
应该诀窍。
另请参阅本教程的section。