我正在做一项流量分析任务,但我的代码中出现了一些错误。我的数据行是这样的:
qurter | DOW (Day of week)| Hour | density | speed | label (predicted speed for another half an hour)
值是这样的:
1, 6, 19, 23, 53.32, 45.23
这意味着在 15 点钟的某个特定街道1st
上19
,Friday
测量交通密度23
和当前速度53.32
。预测的速度将是45.23
。
任务是通过上面给出的预测器预测另外半小时的速度。
我正在使用此代码DNNRegressor
为数据构建 TensorFlow:
import pandas as pd
data = pd.read_csv('dataset.csv')
X = data.iloc[:,:5].values
y = data.iloc[:, 5].values
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2, random_state=0)
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaler.fit(X_train)
X_train = pd.DataFrame(data=scaler.transform(X_train),columns = ['quarter','DOW','hour','density','speed'])
X_test = pd.DataFrame(data=scaler.transform(X_test),columns = ['quarter','DOW','hour','density','speed'])
y_train = pd.DataFrame(data=y_train,columns = ['label'])
y_test = pd.DataFrame(data=y_test,columns = ['label'])
import tensorflow as tf
speed = tf.feature_column.numeric_column('speed')
hour = tf.feature_column.numeric_column('hour')
density = tf.feature_column.numeric_column('density')
quarter= tf.feature_column.numeric_column('quarter')
DOW = tf.feature_column.numeric_column('DOW')
feat_cols = [h_percentage, DOW, hour, density, speed]
input_func = tf.estimator.inputs.pandas_input_fn(x=X_train,y=y_train ,batch_size=10,num_epochs=1000,shuffle=False)
model = tf.estimator.DNNRegressor(hidden_units=[5,5,5],feature_columns=feat_cols)
model.train(input_fn=input_func,steps=25000)
predict_input_func = tf.estimator.inputs.pandas_input_fn(
x=X_test,
batch_size=10,
num_epochs=1,
shuffle=False)
pred_gen = model.predict(predict_input_func)
predictions = list(pred_gen)
final_preds = []
for pred in predictions:
final_preds.append(pred['predictions'])
from sklearn.metrics import mean_squared_error
mean_squared_error(y_test,final_preds)**0.5
当我运行这段代码时,它会以这个结尾抛出一个错误:
TypeError: Failed to convert object of type <class 'dict'> to Tensor. Contents: {'label': <tf.Tensor 'fifo_queue_DequeueUpTo:6' shape=(?,) dtype=float64>}. Consider casting elements to a supported type.
首先错误的概念是什么?由于错误原因,我找不到处理它的来源。以及如何修改解决方案的代码?
其次,使用 tensorflowcategorical_column_with_identity
代替numeric_columns
表示星期几的 DOW 是否会提高模型性能?
我还想知道合并quarter
和hour
作为单个列是否有用day time
(quarter
是一小时中的分钟数,它将在 0 和 1 之间标准化)?