Cast string to float is not supported in Linear Model

南笙酒味 提交于 2019-12-01 03:21:29

I had the exact same problem, you need to make sure that the input data you are feeding the model is in the right format. ( not just the features but also the label column)

My problem was that i was not skipping the first row in the data file, so i was trying to convert the titles to float format.Something as simple as adding

skiprows=1

When reading the csv:

df_test = pd.read_csv(test_file, names=COLUMNS_TEST, skipinitialspace=True, skiprows=1, engine="python")

I would recommend you to check:

df_test.dtypes

You should get something like

Feature1      int64
Feature2      int64
Feature3      int64
Feature4      object
Feature5      object
Feature6      float64
dtype: object

If you are not getting the correct dtype then the model.fit is going to fail

You can't literally cast a string to a number, particularly "y","n" to 1.0/0.0.

If you have numeric strings (e.g. "0") you could try tf.string_to_number(..)

The problem is that you probably had indicated the feature like a real type but in your data frame is still string or when set in tf.constant you didn't cast to correct type.

Confirm the types of your columns. You can check just type (df is your dataframe):

df.info()

And you can see all columns and types, some like that:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 178932 entries, 0 to 178931
Data columns (total 64 columns):
d_prcp                      178932 non-null float64
d_stn                       178932 non-null int64
ws_lat                      178932 non-null float64
ws_lon                      178932 non-null float64
d_year                      178932 non-null int64
d_temp                      178932 non-null float64
...

You can use this bellow function in order to convert your data in correct type in tensorflow. (this code is from a repo google/training-data-analyst: link here)

def make_input_fn(df):
  def pandas_to_tf(pdcol):
    # convert the pandas column values to float
    t = tf.constant(pdcol.astype('float32').values)
    # take the column which is of shape (N) and make it (N, 1)
    return tf.expand_dims(t, -1)

  def input_fn():
    # create features, columns
    features = {k: pandas_to_tf(df[k]) for k in FEATURES}
    labels = tf.constant(df[TARGET].values)
    return features, labels
  return input_fn

def make_feature_cols():
  input_columns = [tf.contrib.layers.real_valued_column(k) for k in FEATURES]
  return input_columns

I am using W10, Python3 and Tensorflow 1.9

The source of the error in my code was in the feature definition. I had a boolean feature with a default_value of -1 like this:

tf.feature_column.categorical_column_with_vocabulary_list( 
    key='partial_funding_indicator', vocabulary_list=['True', 'False'],
    dtype=tf.string, **default_value=-1**, num_oov_buckets=None)

The issue did not arise when the default_value was changed to 0:

tf.feature_column.categorical_column_with_vocabulary_list(
    key='partial_funding_indicator', vocabulary_list=['True', 'False'],
    dtype=tf.string, **default_value=0**, num_oov_buckets=None)

default_value is the integer ID value to return for out-of-vocabulary feature values. For example, in a list/file of value like ['True', 'False'] to make default_value == True, it would be default_value=0; the list index.

Your classes are probably in string forms and they need to be numeric (1 and 0 only for this specific tutorial)

Normally this error is because m.evaluate is somehow empty.

Since you load your data from csv file, it is very likely that your data was stored as string instead of float or int inside the array. I suggest you check it manually to make sure.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!