- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个从 csv 中读取的 Pandas DataFrame,它有一些带有字符串值的列,但实际上是 object
类型。因为它们是分类的,所以我将它们转换为 category
,然后转换为整数表示,然后我拟合了一个随机森林回归器。
for col in df_raw.select_dtypes(include='object'):
df_raw[col] = df_raw[col].astype('category')
df_raw[col] = df_raw[col].cat.codes #not 'category' type anymore.
问题是如果我这样做,dtype
会立即转换为 int
而我会丢失我需要的 cat
信息之后。
例如,在循环的第一行之后,我可以运行 df_raw[col].cat
,我会按预期获得索引类别。但是一旦执行第二行,dtype
列变为 int8
,我将得到错误:
Can only use .cat accessor with a 'category' dtype`
这在某种程度上非常合理,因为它的 dtype 是 int8
。
是否可以在同一个 DataFrame 中保留类别编码信息,同时使用整数编码来适应回归器?怎么办?
最佳答案
<强>1。简单的想法
为什么不在回归器拟合中使用派生列,例如:
df_raw[col + '_calculated'] = df_raw[col].cat.codes
通过这种方式,您同时拥有:一个不会更改此功能的分类列 col
和一个根据需要使用 int
进行进一步处理的“计算”列?
<强>2。更聪明的方法
另一种方法是在将数据帧传递给 fit
方法之前包装数据帧,这样回归器可以访问 .cat.codes
而不是直接访问分类值:
def access_wrapper(dframe, col):
yield from dframe[col].cat.codes
fit(..., access_wrapper(df, col))
通过这种方式,您根本不会影响数据帧,也不会以调用 dframe[col].cat.codes< 为代价从
每次访问该值(应该相当快)。df[col]
复制值
关于python - 在 Pandas DataFrame 列中进行整数编码后将 dtype 保留为类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54716954/
我有两个数据框,它们都有一个 Order ID 和一个 date。 我想在第一个数据帧 df1 中添加一个标志:如果具有相同 order id 和 date 的记录在数据帧 df2,然后添加一个Y:
我正在运行 Python 2.6。我有以下示例,我试图连接 csv 文件中的日期和时间字符串列。根据我设置的 dtype(无与对象),我发现一些我无法解释的行为差异,请参阅帖子末尾的问题 1 和 2。
当尝试通过以下代码将 sklearn 数据集转换为 pandas 数据帧时,出现此错误“ufunc 'add' 不包含签名匹配类型 dtype(' import numpy as np from sk
我正在尝试使用我的代码计算周期图 from scipy import signal import numpy as np import matplotlib.pyplot as plt x = [li
我有 pandas 数据框 df,我想打印出变量列表以及类型和缺失字段的数量(NaN、NA)。 def var_desc(df,dt): print('====================
这个数据类型是如何工作的,我对这个东西很着迷。 1:首先使用python的默认类型:无法工作,引发错误 bins = pd.DataFrame(dtype=[str, int, int], colum
尝试获取小型玩具数据集的直方图时,通过 matplotlib 来自 numpy 的奇怪错误。我只是不确定如何解释错误,这让我很难知道接下来要做什么。 虽然没有找到太多相关信息,但this nltk q
我在减去数据表的两列时遇到问题,我是Python新手,在尝试研究如何解决这个问题失败后,我想知道是否有人有任何见解。我的代码是这样的: response = qc.query(token, sql=q
我运行我的代码,它在第 79 行抛出错误: numpy.core._exceptions.UFuncTypeError: ufunc 'add' did not contain a loop with
我正在尝试创建一个非常简单的程序,它将绘制一条抛物线图,其中 v 是速度,a 是加速度,x是时候了。用户将输入 v 和 a 的值,然后是 v 和 a 以及 x 将确定 y。 我试图用这个来做到这一点:
我构建了一个槽填充(一种序列分类)模型,其结构为:自定义 ELMo 嵌入层 - BiLSTM - CRF。 它训练得很好。但根据预测我得到: 'TypeError: ufunc 'add' did n
是否有比以下方法更优雅的方法来为可能复杂的 dtype 获取相应的真实 numpy dtype? import numpy as np def dtype_to_real(rvs_dtype: np.
对于 jupyter 中的以下 pandas 代码,我试图获取数据类型信息。tab 在 jupyter 中为我提供了有两个属性的信息它同时具有 dtype 和 dtypes import pandas
我有一个用 pandas 加载的 csv 文件,如下所示: classes_dataset2=pd.read_csv("labels.csv") classes_dataset2[0:10] 0
我有一个类似于以下内容的 numpy.dtype: dtype([('value1','>> d = np.dtype([('value1','>> [x[0] for x in d.descr] [
我正在使用 scipy 的 curve_fit 来拟合一些数据的函数,并收到以下错误; Cannot cast array data from dtype('O') to dtype('float64
好吧,似乎在堆栈溢出中提出了几个类似的问题,但似乎没有一个回答正确或正确,也没有描述确切的示例。 我在将数组或列表保存到 hdf5 时遇到问题... 我有几个文件包含 (n, 35) 维度的列表,其中
目前我得到的数组是 arr = array([array([ 2, 7, 8, 12, 14]), array([ 3, 4, 5, 6, 9, 10]), array([0, 1]
我有一个 Pandas 系列。我想检查该系列的数据类型是否在数据类型列表中。像这样的东西: series.dtype not in [pd.dtype('float64'), pd.dtype('fl
我有一个 numpy 数组,我想将其从对象转换为复数。如果我将该数组作为 dtype 字符串并进行转换,则没有问题: In[22]: bane Out[22]: array(['1.000027337
我是一名优秀的程序员,十分优秀!