我正在尝试使用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)
我怎样才能解决这个问题?
最佳答案
您的错误就在附近:
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"])
在这里,您将熊猫数据帧中的一列作为第一个参数传递,但是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。
关于python - Tensorflow TypeError:'系列'对象是可变的,因此不能被散列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45259726/