问题描述
我用于此问题的数据集是Ames Iowa House定价数据集,特别是制表符分隔的版本。我要问的是,尽管执行了以下操作,但数据框对象的列是否始终按相同的顺序排序:
- 使用无序的列名列表进行选择。
- 使用
DataFrame.select_dtypes()
方法进行选择。
我也做了一个重要的假设。我假设SelectKBest
方法的功能统计信息的顺序与输入数据框的功能的顺序相同,即列。正确吗?
假定的问题原因
假设:
- 在添加列,串联,选择列或切片之后,将数据帧列设置为正确的顺序。
- 由于
SalePrice
是数字,因此数据框列的整数顺序已更改。当使用df_train
选择列时,DataFrame.select_dtypes
个功能的有序列表将移动一个。 - 整数位置是基于
df_train
而没有SalePrice
的。因此,整数位置是其背后的功能之一。
此假设的问题在于SalePrice
位于原始完整数据帧df_housing
的末尾。此外,如果根据Python的默认字母数字排序器正确排序,则这些列都应该排在SalePrice
之前。
特定代码
在尝试手动比较Scikit-Learn的SelectKBest
方法的结果时遇到了这个问题。我使用df_sparse_uncorr
结果的第五个特征创建了一个合成的“稀疏不相关”数据帧,称为make_sparse_uncorrelated
。该数据帧将用作对机器学习无用的不相关数据的替代。
我还创建了一个字符串列表,在df_train
中给出了“真数字”列。 df_sparse_uncorr
的特征和行/观察数与我的训练数据集相同。然后,我得到了特征的整数位置索引列表,这些特征的统计观测值小于或等于从不相关数据集中获得的最大观测值。
- 我将
f_regression
和mutual_info_regression
的{{1}}和df_sparse_uncorr
统计信息取最大值。我使用了一种用户定义的方法,该方法最初旨在对多个数据集执行df_train
-将传递功能的限制设置为“全部”。这是 最终输出的嵌套字典的格式:
SelectKBest
- 我使用一个用户定义的函数来获取每个数据帧作为系列的SelectKBest对象的结果(使用
{ "df_synthetic_data": { { "f_regression":pd.Series(scores),"mutual_info_regression":pd.Series(scores) } },"df_train":{ { "f_regression":pd.Series(scores),"mutual_info_regression":pd.Series(scores) } } }
和f_regression
)。
mutual_info_regression functions
- 对于每个系列,我获取
def extract_result_dict(dict_result): extracted = {} for dataset,dataset_results in dict_result.items(): #print("\n=====\n","Start","\n=====\n",dataset) for method,result_dict in dataset_results.items(): #print("\n-----\n",method) for result_name,result_value in result_dict.items(): item_name = method + '-' + result_name try: sr_values = pd.Series(data = result_value) extracted[item_name] = sr_values.sort_values(ascending = False) except Exception as e: print("Error occured processing:",item_name,e) continue return extracted
的所有特征统计的索引,这些索引低于使用df_train
计算的最大KBest统计。
df_sparse_uncorr
- 我使用返回的整数列表作为整数顺序索引来选择“失败”列的列名。
问题是,当我使用整数位置索引列表获取失败特征的名称时,得到了不同的结果。
使用def filter_by_threshold(dict_value_series,remove_low = True,dict_thresholds = {"default":np.nan}):
lst_failers = []
dict_exceptions = {}
if remove_low == True:
try:
for alias,series in dict_value_series.items():
for feature in series.index.tolist():
if (series[feature] <= dict_thresholds[alias]) and (feature not in lst_failers):
lst_failers.append(feature)
except Exception as e:
dict_exceptions["alias"] = str(e)
else:
for alias,series in dict_value_series.items():
try:
for feature in series.index.tolist():
if (series[feature] <= dict_thresholds[alias]) and (feature not in lst_failers):
lst_failers.append(feature)
except Exception as e:
dict_exceptions["alias"] = str(e)
return lst_failers
输出
select_dtypes
使用列表lst_manyset_failers = list(df_train.select_dtypes(include = "number").columns.values[ list(null_surrogate_failers) ])
lst_manyset_failers.sort()
lst_manyset_failers
#Output:
['Bsmt Half Bath','Fireplaces','Garage Cars','Lot Area','Low Qual Fin SF','Wood Deck SF']
输出
lst_numerics
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)