Я пытаюсь использовать tf.estimator.DNNClassifier, чтобы предсказать ежемесячную цену для набора данных.Однако при запуске программы я получаю сообщение об ошибке с DNNClassifier о том, что метки не <= n_classes -1.</p>
InvalidArgumentError (see above for traceback): assertion failed: [Labels must <= n_classes - 1] [Condition x <= y did not hold element-wise:x (dnn/head/ToFloat:0) = ] [[51][58][50]...] [y (dnn/head/assert_range/Const:0) = ] [1]
Кажется, что он не распознает мои столбцы функций в классификаторе DNN.Следующий код - это то, что я запускаю.
import tensorflow as tf
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
df = pd.read_csv('./csv/lmtensordata.csv')
deep_feat = df.drop(columns=['monthly_price'], axis=1)
deep_label = df['monthly_price']
df.info()
df.head()
numerical_columns = [col for col in df.columns if (df[col].dtype=='int64' or df[col].dtype=='float64')]
categorical_columns = [col for col in deep_feat.columns if len(deep_feat[col].unique()) == 2 or deep_feat[col].dtype == 'O']
continuous_columns = [col for col in deep_feat.columns if len(deep_feat[col].unique()) > 2 and (deep_feat[col].dtype == 'int64' or deep_feat[col].dtype=='float64')]
# making a train test split
X_T, X_t, y_T, y_t = train_test_split(deep_feat, deep_label, test_size=0.3)
cols_to_scale = continuous_columns[:]
# scaling the listed columns
scaler = StandardScaler()
X_T.loc[:,cols_to_scale] = scaler.fit_transform(X_T.loc[:,cols_to_scale])
X_t.loc[:,cols_to_scale] = scaler.fit_transform(X_t.loc[:,cols_to_scale])
continuous_feat_cols = [tf.feature_column.numeric_column(key=col) for col in continuous_columns]
feat_cols = continuous_feat_cols
input_fun = tf.estimator.inputs.pandas_input_fn(X_T,y_T,batch_size=50,num_epochs=1000,shuffle=True)
pred_input_fun = tf.estimator.inputs.pandas_input_fn(X_t,batch_size=50,shuffle=False)
DNN_model = tf.estimator.DNNClassifier(hidden_units=[10,10,10], feature_columns=feat_cols, n_classes=2)
DNN_model.train(input_fn=input_fun, steps=5000)
predictions = DNN_model.predict(pred_input_fun)
res_pred = list(predictions)
print(res_pred[0])
Когда я распечатываю свои feat_cols, это то, что я получаю.
[
_NumericColumn(key='car_year',
shape=(1,),
default_value=None,
dtype=tf.float32,
normalizer_fn=None),
_NumericColumn(key='make_model_bucket',
shape=(1,
),
default_value=None,
dtype=tf.float32,
normalizer_fn=None),
_NumericColumn(key='mileage',
shape=(1,
),
default_value=None,
dtype=tf.float32,
normalizer_fn=None),
_NumericColumn(key='years_licensed_bucket',
shape=(1,
),
default_value=None,
dtype=tf.float32,
normalizer_fn=None),
_NumericColumn(key='zip_bucket',
shape=(1,
),
default_value=None,
dtype=tf.float32,
normalizer_fn=None)
]
Здесь также вывод df.head () набора данных, с которым я работаю.
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 33588 entries, 0 to 33587
Data columns (total 6 columns):
car_year 33588 non-null int64
make_model_bucket 33588 non-null int64
mileage 33588 non-null int64
years_licensed_bucket 33588 non-null int64
zip_bucket 33588 non-null float64
monthly_price 33588 non-null int64
dtypes: float64(1), int64(5)
memory usage: 1.5 MB
Я не уверен, что мне следует вводить для n_classes или что-то не так с моими столбцами функций.
Любая помощь очень ценится!