无论对数据框进行了什么操作或选择,熊猫数据框的列是否都按字母数字顺序排序? 使用def filter_by_threshold(dict_value_series, remove_low = True, dict_thresholds = {"default":np.nan}): lst_failers = [] dict_exceptions = {} if remove_low == True: try:

问题描述

我用于此问题的数据集是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的特征和行/观察数与我的训练数据集相同。然后,我得到了特征的整数位置索引列表,这些特征的统计观测值小于或等于从不相关数据集中获得的最大观测值。

  1. 我将f_regressionmutual_info_regression的{​​{1}}和df_sparse_uncorr统计信息取最大值。我使用了一种用户定义的方法,该方法最初旨在对多个数据集执行df_train-将传递功能的限制设置为“全部”。这是 最终输出的嵌套字典的格式:
SelectKBest
  1. 我使用一个用户定义的函数来获取每个数据帧作为系列的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
  1. 对于每个系列,我获取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
  1. 我使用返回的整数列表作为整数顺序索引来选择“失败”列的列名。

问题是,当我使用整数位置索引列表获取失败特征的名称时,得到了不同的结果。

使用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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...