使用公共类对数组进行分组以便在CNN中进行分类

时间:2017-08-21 15:28:06

标签: python-3.x numpy dataframe machine-learning sklearn-pandas

我有一个包含三列的数据集,前两列是功能,第三列包含类,有4个类,部分可以在这里看到。

enter image description here

数据集很大,可以说100,000行和3列(两列功能和一列类),所以我在训练我的深度学习模型之前在数据集上使用长度为50的移动窗口。到目前为止,我已经尝试了两种不同的方法来对数据集进行切片而没有很好的结果,我很确定我的数据集是好的。 我首先在我的整个数据集上使用了一个移动窗口,产生了2000个数据样本,包含50行和2列(2000,50,2)。由于一些数据样本包含混合类,我只选择具有公共类的数据样本,并找到类的平均值,仅将该特定数据样本分配到单个类中,我没有得到结果。这是我的代码,`< / p>

def moving_window(data_, length, step=1):
    streams = it.tee(data_, length)
    return zip(*[it.islice(stream, i, None, step * length) for stream, i in zip(streams, it.count(step=step))])


data = list(moving_window(data_, 50))
data = np.asarray(data)
# print(len(data))
for i in data:
    label=np.all(i==i[0,2],axis=0)

    if label[2]==True:
        X.append(i[:,0:2])
        Y.append(sum(i[:,2])/len(i[:,2]))`

我尝试了另一种方法,只收集与特定类相对应的特征,将值放入单独的列表中(4个列表,因为我有4个类)然后使用移动窗口分别对每个列表进行切片并分配给它的类。也没有好结果。这是我的代码。

for i in range(5):
    labels.append(i)
yy= pd.get_dummies(labels)
yy= yy.values
yy= yy.astype(np.float32)


def moving_window(x, length, step=1):
    streams = it.tee(x, length)
    return zip(*[it.islice(stream, i, None, step * length) for stream, i in zip(streams, it.count(step=step))])


x_1 = list(moving_window(x1, 50))
x_1 = np.asarray(x_1)
y_1 = [yy[0]] * len(x_1)
X.append(x_1)
Y.append(y_1)
# print(x_1.shape)

x_2 = list(moving_window(x2, 50))
x_2 = np.asarray(x_2)
# print(yy[1])
y_2 = [yy[1]] * len(x_2)
X.append(x_2)
Y.append(y_2)
# print(x_2.shape)

x_3 = list(moving_window(x3, 50))
x_3 = np.asarray(x_3)
# print(yy[2])
y_3 = [yy[2]] * len(x_3)
X.append(x_3)
Y.append(y_3)
# print(x_3.shape)

x_4 = list(moving_window(x4, 50))
x_4 = np.asarray(x_4)
# print(yy[3])
y_4 = [yy[3]] * len(x_4)
X.append(x_4)
Y.append(y_4)
# print(x_4.shape)

我正在努力训练的模型的架构与其他数据集完美配合。所以我想我错过了处理数据的方法。在开始培训之前,我在处理数据方面缺少什么?,还有其他方法吗?所有的工作都是在python中完成的。

1 个答案:

答案 0 :(得分:0)

我终于成功培养了我的CNN模型,并获得了良好的培训,验证和测试准确性。我添加的唯一内容是使用以下行标准化输入数据

minmax_scale = preprocessing.MinMaxScaler().fit(x)
X = minmax_scale.transform(x)

其余的保持不变。