我有一个字典,其中每个键都是一个行索引,每个值都是一个虚拟值列表。例如:
my_dict = {'row1': ['a', 'b'], 'row2': ['a'], 'row3': ['b', 'c']}
Run Code Online (Sandbox Code Playgroud)
我可以用上述方法有效地创建一个虚拟数据框吗?
>>> df
a b c
row1 True True False
row2 True False False
row3 False True True
Run Code Online (Sandbox Code Playgroud) 我有一个数据集,它以虚拟变量的形式代表每个客户的一个篮子.
例如:
P1 P2 P3 P4 P5
0 2 0 0 0
0 1 0 0 0
0 0 0 3 0
0 0 0 0 0
0 0 5 0 0
1 1 0 0 0
Run Code Online (Sandbox Code Playgroud)
其中P1代表产品1,依此类推.
本质上,我想运行一个简单的查询,在其中我可以转换所有超过1到1的值.这样我的数据中只有1和0.我能够使用以下几行来完成它:
df[(df$P1>1] <- 1
Run Code Online (Sandbox Code Playgroud)
是否适用所有功能?
我有一个带有双索引(日期、时间)的数据框,如果索引日在正确的日期,我想创建等于 1 的新列 'Monday'、'Tuesday'、'Wednesday' 等。
我的原始数据框:
Visitor
Date Time
2017-09-11 4:45 0
5:00 1
5:15 26
....
2017-09-12 4:45 0
5:00 1
5:15 26
....
Run Code Online (Sandbox Code Playgroud)
我想要什么:
Visitor Monday Tuesday
Date Time
2017-09-11 4:45 0 1 0
5:00 1 1 0
5:15 26 1 0
....
2017-09-12 4:45 0 0 1
5:00 1 0 1
5:15 26 0 1
....
Run Code Online (Sandbox Code Playgroud)
这是我尝试过的:
df['Monday'] = (df.index.get_level_values(0).weekday() == 0)
Run Code Online (Sandbox Code Playgroud)
但是,我收到一条错误消息,指出“'Int64Index' 对象不可调用”。
提前致谢!
我在R中设计一个神经网络.为此,我必须准备我的数据并导入一个表.
例如:
time hour Money day
1: 20000616 1 9.35 5
2: 20000616 2 6.22 5
3: 20000616 3 10.65 5
4: 20000616 4 11.42 5
5: 20000616 5 10.12 5
6: 20000616 6 7.32 5
Run Code Online (Sandbox Code Playgroud)
现在我需要一个虚拟化.我的决赛桌应该是这样的:
time Money day 1 2 3 4 5 6
1: 20000616 9.35 5 1 0 0 0 0 0
2: 20000616 6.22 5 0 1 0 0 0 0
3: 20000616 10.65 5 0 0 1 0 0 0
4: 20000616 11.42 5 …
Run Code Online (Sandbox Code Playgroud) 我知道在训练机器学习算法之前我们必须对分类数据进行一次性编码。但我的问题是我们需要手动删除一列还是 sklearn 会做到这一点?
如何从互斥虚拟变量(取值 0/1)创建分类变量?
基本上我正在寻找与此解决方案完全相反的解决方案:(https://subscription.packtpub.com/book/big_data_and_business_intelligence/9781787124479/1/01lvl1sec22/creating-dummies-for-categorical-variables)。
希望有一个基本的 R 解决方案。
例如,我有以下数据:
dummy.df <- structure(c(1L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L,
0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L,
0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 1L),
.Dim = c(10L, 4L),
.Dimnames = list(NULL, c("State.NJ", "State.NY", "State.TX", "State.VA")))
Run Code Online (Sandbox Code Playgroud)
State.NJ State.NY State.TX State.VA
[1,] 1 0 0 0
[2,] 0 1 0 0
[3,] 1 0 0 …
Run Code Online (Sandbox Code Playgroud) 我有包含主题列表(主题 1-5;0 表示未分配主题)及其值的数据。我想为每个主题创建一个新列并用值填充该列。这是桌子的样子......
reviewId topic value
01 2 -4
02 2 9
03 0 -7
04 5 -1
05 1 38
Run Code Online (Sandbox Code Playgroud)
我应该怎么做才能创建一个像这样的表?
评论ID | 话题 | 价值 | t1 | t2 | t3 | t4 | t5 |
---|---|---|---|---|---|---|---|
01 | 2 | -4 | 0 | -4 | 0 | 0 | 0 |
02 | 2 | 9 | 0 | 9 | 0 | 0 | 0 |
03 | 0 | -7 | 0 | 0 | 0 | 0 | 0 |
04 | 5 | -1 | 0 | 0 | 0 | 0 | -1 |
05 | 1 | 38 | 38 | 0 | 0 | 0 | 0 |
这里有可重复的数据可供处理。
raw_df = pd.DataFrame({
'reviewId': ['01', '02', '03', '04', '05'], …
Run Code Online (Sandbox Code Playgroud) 我正在尝试为分类变量创建虚拟变量。但是,当我创建它们时,我收到“ValueError:列重叠但未指定后缀”。这是代码:
dummy2 = pd.get_dummies(data['Teaching'], prefix='Teach')
dummy2.head ()
dummy2.columns = ['Small/Rural','Teaching']
data = data.join(dummy2)
##################
dummy3 = pd.get_dummies(data['Gender'], prefix='Gender_')
dummy3.head()
dummy3.columns = ['Male','Female']
data = data.join(dummy3)
#####################
dummy4 = pd.get_dummies(data['PositionTitle'], prefix='pos_')
dummy4.head()
dummy4.columns = ['Acting Director','RegioReresentative']
data = data.join(dummy4)
#####################
dummy5 = pd.get_dummies(data['Compensation'], prefix='COMP')
dummy5.head()
dummy5.columns = ['23987','46978','89473','248904']
data = data.join(dummy5)
#################3
dummy6 = pd.get_dummies(data['TypeControl'], prefix='Type')
dummy6.head()
dummy6.columns = ['City/country','District','Investor','Non Profit']
data = data.join(dummy6)
Run Code Online (Sandbox Code Playgroud) 我正在尝试了解此二进制编码器背后的逻辑。
它会自动获取分类变量并对它们进行虚拟编码(类似于sklearn上的一键编码),但是减少的输出列数等于唯一值长度的log2。
基本上,当我使用该库时,我注意到我的虚拟变量仅限于少数唯一值。在进一步研究中,我注意到了这一点@staticmethod
,它在分类变量中采用了唯一值len的log2。
我的问题是为什么?我意识到这降低了输出数据的维数,但是这样做的背后逻辑是什么?使用log2如何确定表示数据所需的位数?
def calc_required_digits(X, col):
"""
figure out how many digits we need to represent the classes present
"""
return int( np.ceil(np.log2(len(X[col].unique()))) )
Run Code Online (Sandbox Code Playgroud)
完整的源代码:
"""Binary encoding"""
import copy
import pandas as pd
import numpy as np
from sklearn.base import BaseEstimator, TransformerMixin
from category_encoders.ordinal import OrdinalEncoder
from category_encoders.utils import get_obj_cols, convert_input
__author__ = 'willmcginnis'
[docs]class BinaryEncoder(BaseEstimator, TransformerMixin):
"""Binary encoding for categorical variables, similar to onehot, but stores categories as binary bitstrings.
Parameters
----------
verbose: int
integer indicating …
Run Code Online (Sandbox Code Playgroud) binary-data categorical-data dummy-variable one-hot-encoding
我已经导入了一个 json 文件,现在有一个数据框,其中一列(代码)是一个列表。
index year gvkey code
0 1998 15686 ['TAX', 'ENVR', 'HEALTH']
1 2005 15372 ['EDUC', 'TAX', 'HEALTH', 'JUST']
2 2001 27486 ['LAB', 'TAX', 'HEALTH']
3 2008 84967 ['HEALTH','LAB', 'JUST']
Run Code Online (Sandbox Code Playgroud)
我想要得到的是如下内容:
index year gvkey TAX ENVR HEALTH EDUC JUST LAB
0 1998 15686 1 1 1 0 0 0
1 2005 15372 1 0 1 0 1 0
2 2001 27486 1 0 1 0 1 0
3 2008 84967 0 0 1 0 1 1
Run Code Online (Sandbox Code Playgroud)
在Pandas 将一列列表转换为虚拟对象之后,我尝试了以下代码(其中 …
dummy-variable ×10
pandas ×5
python ×5
dataframe ×3
r ×3
binary-data ×1
data.table ×1
dayofweek ×1
list ×1
time-series ×1