我有一个Python类,我用它来加载和处理Spark中的一些数据.在我需要做的各种事情中,我正在生成一个从Spark数据帧中的各个列派生的虚拟变量列表.我的问题是我不确定如何正确定义用户定义函数来完成我需要的东西.
我做目前有,当映射了潜在的数据帧RDD,解决了问题的一半(记住,这是在一个更大的方法等data_processor类):
def build_feature_arr(self,table):
# this dict has keys for all the columns for which I need dummy coding
categories = {'gender':['1','2'], ..}
# there are actually two differnt dataframes that I need to do this for, this just specifies which I'm looking at, and grabs the relevant features from a config file
if table == 'users':
iter_over = self.config.dyadic_features_to_include
elif table == 'activty':
iter_over = self.config.user_features_to_include
def _build_feature_arr(row):
result = []
row = row.asDict()
for …Run Code Online (Sandbox Code Playgroud) python apache-spark apache-spark-sql apache-spark-ml apache-spark-mllib
如何声明我的给定列中DataFrame包含分类信息?
我有一个DataFrame从数据库加载的 Spark SQL 。其中的许多列DataFrame都有分类信息,但它们被编码为 Longs(为了隐私)。
我希望能够告诉 spark-ml,即使此列是数值,但信息实际上是分类的。类别的索引可能有一些漏洞,这是可以接受的。(例如,一列可能具有值 [1, 0, 0 ,4])
我知道存在 ,StringIndexer但我更愿意避免编码和解码的麻烦,特别是因为我有许多具有这种行为的列。
我会寻找类似于以下内容的东西
train = load_from_database()
categorical_cols = ["CategoricalColOfLongs1",
"CategoricalColOfLongs2"]
numeric_cols = ["NumericColOfLongs1"]
## This is what I am looking for
## this step detects the min and max value of both columns
## and adds metadata to indicate this as a categorical column
## with (1 + max - min) categories
categorizer = ColumnCategorizer(columns = categorical_cols,
autoDetectMinMax = True) …Run Code Online (Sandbox Code Playgroud) 在随机森林的Mllib版本中,可以用参数指定具有名义特征(数字但仍为分类变量)categoricalFeaturesInfo
的列ML随机森林有什么用?在用户指南中,有一个使用VectorIndexer 的示例,该示例也可以转换vector中的分类特征,但是它写为“自动识别分类特征并对其进行索引”
在关于同一问题的其他讨论中,我发现在随机森林中无论如何数字索引都被视为连续特征,建议进行一次热编码以避免这种情况,在这种算法的情况下似乎没有意义,并且特别是考虑到上述官方示例!
我还注意到,当分类列中有很多类别(> 1000)时,一旦用StringIndexer对其进行了索引,随机森林算法就会要求我设置MaxBin参数,该参数应该与连续功能一起使用。这是否意味着如官方示例中所指定的那样,将要将超过箱数的特征视为连续特征,因此对于我的分类列,StringIndexer是可以的,还是意味着整个具有数字标称特征的列都将是假设变量是连续的,进行分类?
random-forest apache-spark apache-spark-ml apache-spark-mllib feature-engineering
在我使用VectorAssembler()来合并一些OneHotEncoded分类功能之前...我的数据框看起来像这样:
| Numerical| HotEncoded1| HotEncoded2
| 14460.0| (44,[5],[1.0])| (3,[0],[1.0])|
| 14460.0| (44,[9],[1.0])| (3,[0],[1.0])|
| 15181.0| (44,[1],[1.0])| (3,[0],[1.0])|
Run Code Online (Sandbox Code Playgroud)
第一列是数字列,另外两列表示OneHotEncoded分类特征的转换数据集.应用VectorAssembler()后,我的输出变为:
[(48,[0,1,9],[14460.0,1.0,1.0])]
[(48,[0,3,25],[12827.0,1.0,1.0])]
[(48,[0,1,18],[12828.0,1.0,1.0])]
Run Code Online (Sandbox Code Playgroud)
我不确定这些数字是什么意思,也无法理解这个转换后的数据集.关于这个输出意味着什么的一些澄清会很棒!
apache-spark apache-spark-sql apache-spark-ml apache-spark-mllib