tensorflow scaler.inverse_transform ValueError:操作数不能与形状一起广播(342,22)(23,)(342,22)

时间:2019-02-20 02:20:41

标签: python tensorflow keras

在这里寻找帮助...卡住了。.下面是我的代码和出现的错误。感谢您的所有帮助。

def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):
    """
    Frame a time series as a supervised learning dataset.
    Arguments:
        data: Sequence of observations as a list or NumPy array.
        n_in: Number of lag observations as input (X).
        n_out: Number of observations as output (y).
        dropnan: Boolean whether or not to drop rows with NaN values.
    Returns:
        Pandas DataFrame of series framed for supervised learning.
    """
    n_vars = 1 if type(data) is list else data.shape[1]
    df = DataFrame(data)
    cols, names = list(), list()
    # input sequence (t-n, ... t-1)
    for i in range(n_in, 0, -1):
        cols.append(df.shift(i))
        names += [('var%d(t-%d)' % (j+1, i)) for j in range(n_vars)]
    # forecast sequence (t, t+1, ... t+n)
    for i in range(0, n_out):
        cols.append(df.shift(-i))
        if i == 0:
            names += [('var%d(t)' % (j+1)) for j in range(n_vars)]
        else:
            names += [('var%d(t+%d)' % (j+1, i)) for j in range(n_vars)]
    # put it all together
    agg = concat(cols, axis=1)
    agg.columns = names
    # drop rows with NaN values
    if dropnan:
        agg.dropna(inplace=True)
    return agg

# load dataset
dataset = pd.read_csv('newdf2.csv', header=0, index_col=0)
dataset = dataset.drop('Monthday.Key', axis = 1)
dataset.head()

values = dataset.values
# integer encode direction
encoder = LabelEncoder()
values[:,4] = encoder.fit_transform(values[:,4])
# ensure all data is float
values = values.astype('float32')

# normalize features
scaler = MinMaxScaler(feature_range=(0, 1))
scaled = scaler.fit_transform(values)
# frame as supervised learning
reframed = series_to_supervised(scaled, 1, 1)
# drop columns we don't want to predict
reframed.drop(reframed.columns[[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,20,21,22,23,24]], axis=1, inplace=True)
print(reframed.head())

# split into train and test sets
values = reframed.values
n_train_hours = round(len(dataset) *.7)
train = values[:n_train_hours, :]
test = values[n_train_hours:, :]
# split into input and outputs
train_X, train_y = train[:, :-1], train[:, -1]
test_X, test_y = test[:, :-1], test[:, -1]
# reshape input to be 3D [samples, timesteps, features]
train_X = train_X.reshape((train_X.shape[0], 1, train_X.shape[1]))
test_X = test_X.reshape((test_X.shape[0], 1, test_X.shape[1]))
print(train_X.shape, train_y.shape, test_X.shape, test_y.shape)

#(799, 1, 22) (799,) (342, 1, 22) (342,)

# make a prediction
yhat = model.predict(test_X)
test_X = test_X.reshape((test_X.shape[0], test_X.shape[2]))
inv_yhat = concatenate((yhat, test_X[:, 1:]), axis=1)

inv_yhat = scaler.inverse_transform(inv_yhat)

这是我得到的错误:

  

ValueError跟踪(最近的呼叫   最后)   ----> 1个inv_yhat = scaler.inverse_transform(inv_yhat)

     

〜\ Anaconda3 \ lib \ site-packages \ sklearn \ preprocessing \ data.py在   inverse_transform(X)       第383章(许我倾城)       384   -> 385 X-= self.min_       386 X / = self.scale_       387返回X

     

ValueError:操作数不能与形状一起广播   (342,22)(23,)(342,22)

1 个答案:

答案 0 :(得分:0)

这听起来很奇怪,但确实可以帮助我纠正错误。

如果万一您使用“ excel”篡改“ csv训练数据”文件,并且要从excel中删除列,

您在csv数据中最终会出现一个空白的“,”值,这将对我造成问题。猜猜有帮助。

删除它或确保您没有手动篡改csv数据文件有助于为我解决问题