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

如何解决无论对数据框进行了什么操作或选择,熊猫数据框的列是否都按字母数字顺序排序? 使用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 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-