如何在Python的某个键处以另一个数组填充NPArray? [英] How to fill an NPArray with another array starting at a key in Python?

查看:101
本文介绍了如何在Python的某个键处以另一个数组填充NPArray?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为x的数据框.这由2列组成,看起来像这样(712,2):

I have a dataframe, called x. This consists of 2 columns which looks like this (712, 2):

     SibSp  Parch
731      0      0
230      1      0
627      0      0
831      1      1
391      0      0
.................

由于需要'自由权重'的逻辑回归,我构建了newX变量,其形状与x数据框的形状相同,但值为空白.

Due to logistic regression needing a 'free weight', I build a newX variable with the shape of my x data frame but blank values.

newX = np.zeros(shape=(x.shape[0], x.shape[1] + 1))

这会生成一个(712,3)np数组:

This generates a (712, 3) np array:

[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]
 ...
 [0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]

由于第一个索引(0)是自由权重,所以我现在想将x值分配给索引1和2.

Since the first index (0) is a free weight, I want to now assign my x values to index 1 and 2.

newX[:, 1:] = x

但是,它给了我这个错误:

However, it gives me this error:

例外:点积形状不匹配(712,)vs(3,712)

Exception: Dot product shape mismatch, (712,) vs (3, 712)

如何用键1-2中的x数组填充newX NPArray,但将所有键0保持不变?

How can I fill my newX NPArray with my x array from keys 1-2 but keep all keys at 0 the same?

推荐答案

您可能需要在数据框后添加values

You may need adding values after the dataframe

newX[:, 1:] = x.values
newX
Out[171]: 
array([[0., 0., 0.],
       [0., 1., 0.],
       [0., 0., 0.],
       [0., 1., 1.],
       [0., 0., 0.]])

这篇关于如何在Python的某个键处以另一个数组填充NPArray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆