收集数据并按如下方式组织:
一个观测被组织成一个数据帧
一个数据帧由100行按顺序组成。
一排有6个功能和1个标签
我将如何为LSTM模型重塑它?
my observation
idx prod period qty label
0 Customer10 FG1 2483 200.000000 'A'
1 Customer11 FG2 2484 220.000000 'B'
2 Customer12 FG3 2485 240.000000 'C'
3 Customer13 FG1 2485 240.000000 'C'
...
100 Customer99 FG1 2485 240.000000 'A'
第一步是将数据帧分离为x和y变量。
你可以用一个简单的for循环来做这件事,我不知道具体的数据格式,所以我只能给出一个类似样板的例子。
x = []
y = []
for i in dataframe:
if Is_Label:
y.append(i)
else:
x.append(i)
接下来,您需要将数组转换为numpy数组。使用作为np
导入的numpy,你可以这样做。
x = np.array(x)
y = np.array(y)
使用numpy,您可以使用.reshape
方法来根据您的需求重塑数据。
所以基本上你的问题是在数据集上进行特征工程,这样你就可以构建LSTM模型或任何其他模型(你在reshaping
上的问题也属于特征工程(:
在这里查看数据时要遵循的原型或基本程序是:
- 分离自变量(X(和因变量(y(
- 根据您的选择,使用一个热编码或标签编码等将分类变量转换为数字变量,这将为您完成所有繁重的工作
- 做一个测试列车分割,这也解决了形状问题
#split X and y
X, y=df.iloc[:, :-1], df.iloc[:, -1]
#label encode your
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
X[['idx', 'prod','period']] = le.fit_transform(X[['idx', 'prod','period']]) #however id or other columns can be dropped but its upto your EDA
y = le.fit_transform(y)
#split into test and train set and try running the model which you intend
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
然后试着运行你的模型。使用.reshape
,您可以随时根据需要重塑数据