Python 从字典中洗牌数据以获取测试和训练数据

Python 从字典中洗牌数据以获取测试和训练数据,python,machine-learning,scikit-learn,data-mining,Python,Machine Learning,Scikit Learn,Data Mining,我想将字典和单独数组中的数据拆分为训练数据和测试数据。我试过各种方法,但都没有达到目的。由于这些特性在我的管道中是如何预处理的,我需要将它们作为字典保留下来。社区里有人对此有什么建议吗 字典功能值: 目标值 创建字典 拆分数据 或 错误消息 输入1: 输入2: 模型构建 将您的词典放入一个可保留数据维度并可根据需要拆分的文档中: df=pd.DataFrame{input1:original_model_input1], input2:listoriginal_模型_输入[input2]} X_

我想将字典和单独数组中的数据拆分为训练数据和测试数据。我试过各种方法,但都没有达到目的。由于这些特性在我的管道中是如何预处理的,我需要将它们作为字典保留下来。社区里有人对此有什么建议吗

字典功能值:

目标值

创建字典

拆分数据

错误消息

输入1:

输入2:

模型构建


将您的词典放入一个可保留数据维度并可根据需要拆分的文档中:

df=pd.DataFrame{input1:original_model_input1], input2:listoriginal_模型_输入[input2]} X_列,X_测试,y_列,y_测试=列车测试,y 转换回原始格式:

X_train = X_train.to_dict("list")
X_test = X_test.to_dict("list")
编辑

为了使管道保持正常运行,您可能需要添加以下两行:

X_train = {k:np.array(v) for k,v in X_train.items()}
X_test = {k:np.array(v) for k,v in X_test.items()}

调用train_test_split时,您正在将一个嵌套列表作为X输入,这会引发错误。相反,您可以从字典中构建一个2D特性数组,然后拆分为训练和测试。例如:

d = {'input1': np.random.random((10,)),
     'input2': np.random.random((10,3))}
y = np.random.choice([0,1],10)
如果字典中的一个数组具有单个维度,我们可以添加一个轴,然后将结果连接到二维数组中:

X = [a[:,None] if len(a.shape)==1 else a for a in d.values()]
X_train, X_test, y_train, y_test = train_test_split(np.concatenate(X, axis=1), y)

这些输入看起来像什么?字典数组大小都一样吗?@yatu-hey!是的,它们都一样大。我刚刚更新了问题,以便您可以查看实际输入。更新后,现在应该可以了,感谢您让我知道@SergeyTanks的答案,我正在使用多输入ANN,因此我需要分离这些值。但这对我来说是一个很好的工作方向!所以我知道我刚刚更新了我的帖子,我是如何构建模型的,采用了这两个独立的输入。很抱歉没有提供信息,是的,我刚刚投了赞成票:对于回复的延迟,我只是想测试一些东西。是否可以将这些值转换为numpy数组。数组或类似的东西?而不是np.arrayx_train['input1'],只访问字典中的一个输入。所有值都可以访问。这些数据结构与作为输入的数据结构完全相同。您能否更清楚地说明当前解决方案无法实现的目标?可能的例子是?我刚刚更新了我的帖子,我是如何构建模型的,采用了这两个独立的输入。AttributeError:“列表”对象没有属性“形状”。当我转换为numpy数组时,模型确实工作正常。很抱歉,当我用文本中提到的代码运行模型时,没有澄清model=build_modelnp.arrayx_train['input1'],np.arrayx_train['input2']。这是完美的工作。我没有想到这种类型的foor循环。非常整洁!!我很感激:
x_train, x_test, y_train, y_test = train_test_split(original_model_inputs, y, test_size = 0.2, random_state = 6)
ValueError: Found input variables with inconsistent numbers of samples: [2, 3000]
[55., 46., 46., ..., 60., 60., 45.]

Shape: (3000,)
[[-2.00370455, -2.35689664, -1.96147382, ...,  2.11014128,
         2.59383321,  1.24209607],
       [-1.97130549, -2.19063663, -2.02996445, ...,  2.32125568,
         2.27316046,  1.48600614],
       [-2.01526666, -2.40440917, -1.94321752, ...,  2.15266657,
         2.68460488,  1.23534095],
       ...,
       [-2.1359458 , -2.52428007, -1.75701785, ...,  2.25480819,
         2.68114281,  1.75468981],
       [-1.95868206, -2.23297167, -1.96401751, ...,  2.07427239,
         2.60306072,  1.28556955],
       [-1.80507278, -2.62199521, -2.08697271, ...,  2.34080577,
         2.48254585,  1.52028871]]

Shape: (3000, 3840)
input1= Input(shape = (1, ))
input2= Input(shape = (3840, ))

# The first branch operates on the first input
x = Dense(units = 128, activation="relu")(input1)
x = BatchNormalization()(x)
x = Dense(units = 128, activation="relu")(x)
x = BatchNormalization()(x)
x = Model(inputs=input1, outputs=x)

# The second branch operates on the second input (Embeddings)
y = Dense(units = 128, activation="relu")(input2)
y = BatchNormalization()(y)
y = Dense(units = 128, activation="relu")(y)
y = BatchNormalization()(y)  
y = Model(inputs=input2, outputs=y)

# combine the output of the two branches
combined = Concatenate()([x.output, y.output])

out = Dense(128, activation='relu')(combined)
out = Dropout(0.5)(out)
out = Dense(1)(out)

# The model will accept the inputs of the two branches and then output a single value
model = Model(inputs = [x.input, y.input], outputs = out)
model.compile(loss='mse', optimizer = Adam(lr = 0.001), metrics = ['mse'])

model.fit([X1,X2], Y, epochs=3)
X_train = X_train.to_dict("list")
X_test = X_test.to_dict("list")
X_train = {k:np.array(v) for k,v in X_train.items()}
X_test = {k:np.array(v) for k,v in X_test.items()}
d = {'input1': np.random.random((10,)),
     'input2': np.random.random((10,3))}
y = np.random.choice([0,1],10)
X = [a[:,None] if len(a.shape)==1 else a for a in d.values()]
X_train, X_test, y_train, y_test = train_test_split(np.concatenate(X, axis=1), y)