小编Eis*_*sen的帖子

将 one-hot 编码变量转换为一列

我有像这样的年龄列,它们是虚拟编码的。如何使用 dplyr 将这些列转换为一列?

输入:

  age_0-10 age_11-20 age_21-30 age_31-40 age_41-50 age_51-60 gender
1 0        1         0         0         0         0         0
2 0        0         1         0         0         0         1
3 0        0         0         1         0         0         0
4 0        1         0         0         0         0         1
5 0        0         0         0         0         1         1
Run Code Online (Sandbox Code Playgroud)

预期输出:

age         gender
1 11-20     0
2 21-30     1
3 31-40     0
4 11-20     1
5 51-60     1
Run Code Online (Sandbox Code Playgroud)

r dplyr

11
推荐指数
1
解决办法
947
查看次数

基于R dplyr中的条件折叠一个热编码列

我有一张表,其中包含热编码的变量。我想将这些变量折叠到一列中。例如,任何具有“high”、“med”或“low”的列,我希望成为具有 high = 0、med = 1 和 low = 2 的数字编码的一列。我如何在 R 中执行此dplyr操作?我怀疑转向会有所帮助,但我不知道从哪里开始。生成的列名称应包含三列的名称,不带 high、med、low 名称。例如,我会将 columns d-high_cm1d-med_cm1、转换d-low_cm1d-cm1数字编码。

输入:

sex age    cost_cm  d-high_cm1 d-med_cm1 d-low_cm1 c-high_cm1 c-med_cm1 c-low_cm1
f   old    1        1           0         0           1           0         0
m   young  0        1           0         0           1           0         0
m   old    0        0           1         0           0           1         0
f   young  0        1           0         0           0           0         1
m   old    1        0           0         1           0           0 …
Run Code Online (Sandbox Code Playgroud)

r dplyr

6
推荐指数
1
解决办法
144
查看次数

使用相同颜色绘制多条线,但使用色调来分隔线

我有这些数据,我想为每个观测值绘制一条年龄与预测概率的线。

Age    Predicted Probability  Obs
18.0   0.206                  3.0
15.0   0.206                  3.0
34.0   0.154                  3.0
52.0   0.124                  3.0
46.0   0.146                  3.0
18.0   0.000                  2.0
15.0   0.000                  2.0
34.0   0.000                  2.0
52.0   0.098                  2.0
46.0   0.002                  2.0
18.0   0.000                  1.0
15.0   0.000                  1.0
34.0   0.004                  1.0
52.0   0.078                  1.0
46.0   0.006                  1.0
18.0   0.038                  0.0
15.0   0.032                  0.0
34.0   0.384                  0.0
52.0   0.318                  0.0
46.0   0.348                  0.0
Run Code Online (Sandbox Code Playgroud)

我通过seaborn中的以下代码完成了此操作:

sns.lineplot(data=df, x="Age", y="Predicted Probability", hue = 'Obs')       
Run Code Online (Sandbox Code Playgroud)

使用色调参数。但我不希望每条线都有不同的颜色。事实上,我想要每个观察点都有一条黑线。我怎样才能做到这一点?

python pandas seaborn

5
推荐指数
1
解决办法
3709
查看次数

如果列的唯一计数大于某个值,则更改数据框中的值

IP Routing Banking
1  1        6
2  1        6
3  1        7
3  3        8
4  5        9
5  9        7
Run Code Online (Sandbox Code Playgroud)

对于每一列,如果相同的值出现 2 次或更多次,我想将其更改为“其他”。我怎样才能在 pandas python 中做到这一点?

预期输出:

IP       Routing      Banking
1        Other        Other
2        Other        Other
Other    Other        Other
Other    3            8
4        5            9
5        9            Other
Run Code Online (Sandbox Code Playgroud)

python pandas

5
推荐指数
1
解决办法
72
查看次数

Tidymodels Logistic 回归获取系数和标准误差

有没有办法在 tidy 模型中获得逻辑回归的标准误差和 p 值?

我可以通过下面的代码获得系数..但我想计算每个特征的优势比,我还需要标准误差..

glm.fit <- 
  logistic_reg(mode = "classification") %>%
  set_engine(engine = "glm") %>% 
  fit(Species ~ ., data = iris)


glm.fit$fit$coefficients
Run Code Online (Sandbox Code Playgroud)

通常你可以通过调用summary()glm 对象来做到这一点,但我在这里尝试使用 tidymodels。

r machine-learning tidymodels

3
推荐指数
1
解决办法
561
查看次数

计算 pandas 中组切换的次数

我有以下数据框:

我想创建一个新列来计算 IP 切换域的​​次数。

输入:

    domain      ip      timestamp              next_domain    next_next_domain
0   Google      101     2020-04-01 23:01:41    Facebook       N/A
1   Google      101     2020-04-01 23:01:59    Facebook       N/A
2   Google      101     2020-04-02 12:01:41    Facebook       N/A
3   Facebook    101     2020-04-02 13:11:33    N/A            N/A
4   Facebook    101     2020-04-02 13:11:35    N/A            N/A
5   Youtube     103     2020-04-21 13:01:41    Google         Facebook
6   Youtube     103     2020-04-21 13:11:46    Google         Facebook
7   Youtube     103     2020-04-22 01:01:01    Google         Facebook
8   Google      103     2020-04-22 02:11:23    Facebook       Youtube
9   Facebook    103     2020-04-23 14:11:13    Youtube        N/A …
Run Code Online (Sandbox Code Playgroud)

python pandas

3
推荐指数
1
解决办法
128
查看次数

将列表列转换为熊猫中的字符串

我有一个 df 这样叫df。tag_position 是字符串或列表。但我希望它们都是字符串。我怎样才能做到这一点?我还想删除最后的空格。

输入

id  tag_positions
1   center
2   right
3   ['left']
4   ['center ']
5   [' left']
6   ['right']
7   left

Run Code Online (Sandbox Code Playgroud)

预期产出

id  tag_positions
1   center
2   right
3   left
4   center
5   left
6   right
7   left

Run Code Online (Sandbox Code Playgroud)

python pandas

2
推荐指数
1
解决办法
58
查看次数

获取R dplyr中每列中1的百分比

我有一个像这样的 DF:

row_id   stn_1 stn_2 stn_3 stn_4 stn_5
1        1     0     1     0     1
2        0     1     0     0     0
3        1     0     0     0     0
4        1     0     1     0     0
5        0     0     0     1     0
Run Code Online (Sandbox Code Playgroud)

我想获得数据中出现的 stn 的百分比。基本上是除 row_id 之外每列中 1 的百分比。

预期输出:

stn    percentage
stn_1  .60
stn_2  .20
stn_3  .40
stn_4  .20
stn_5  .20

Run Code Online (Sandbox Code Playgroud)

我怎样才能在 dplyr 中做到这一点?

r dplyr

2
推荐指数
1
解决办法
62
查看次数

标签 统计

pandas ×4

python ×4

r ×4

dplyr ×3

machine-learning ×1

seaborn ×1

tidymodels ×1