""" Processing the data """ import numpy as np import pandas as pd from sklearn.preprocessing import StandardScaler, MinMaxScaler def process_data(train, test, lags): """Process data Reshape and split train\test data. # Arguments train: String, name of .csv train file. test: String, name of .csv test file. lags: integer, time lag. # Returns X_train: ndarray. y_train: ndarray. X_test: ndarray. y_test: ndarray. scaler: StandardScaler. """ attr = 'volumn' df1 = pd.read_csv(train, encoding='utf-8').fillna(0) df2 = pd.read_csv(test, encoding='utf-8').fillna(0) # scaler = StandardScaler().fit(df1[attr].values) scaler = MinMaxScaler(feature_range=(0, 1)).fit(df1[attr].values.reshape(-1, 1)) flow1 = scaler.transform(df1[attr].values.reshape(-1, 1)).reshape(1, -1)[0] flow2 = scaler.transform(df2[attr].values.reshape(-1, 1)).reshape(1, -1)[0] train, test = [], [] for i in range(lags, len(flow1)): train.append(flow1[i - lags: i + 1]) for i in range(lags, len(flow2)): test.append(flow2[i - lags: i + 1]) train = np.array(train) test = np.array(test) np.random.shuffle(train) X_train = train[:, :-1] y_train = train[:, -1] X_test = test[:, :-1] y_test = test[:, -1] return X_train, y_train, X_test, y_test, scaler
时间: 2024-03-31 22:38:17 浏览: 102
这段代码主要是用来对数据进行预处理,包括数据读取、缺失值填充、归一化、数据切分等操作。其中,train和test是数据集的文件名,lags是时间滞后的长度。在函数内部,首先读入train和test文件,并对缺失值进行填充。然后使用MinMaxScaler将原始数据归一化到0到1之间。接着,将数据按时间滞后的长度lags进行切分,生成训练集和测试集,并将其转换成numpy数组格式。最后,将训练集和测试集随机打乱顺序,并将输入和输出分别存储到X_train、y_train、X_test和y_test中,同时返回scaler对象,以便在后续预测时对数据进行还原操作。
阅读全文