类型错误:在绘制 seaborn.regplot 时,无法根据规则“安全”将数组数据从 dtype('int64') 转换为 dtype('int32')

Jee*_* NH 10 python data-visualization typeerror pandas seaborn

我正在尝试使用 seaborn 绘制 regplot 并且我无法绘制它并面临TypeError: Cannot cast array data from dtype('int64') to dtype('int32') 根据规则 'safe'

我的数据有 731 行和 16 列 -

>>> bike_df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 731 entries, 0 to 730
Data columns (total 16 columns):
 #   Column      Non-Null Count  Dtype  
---  ------      --------------  -----  
 0   instant     731 non-null    int64  
 1   dteday      731 non-null    object 
 2   season      731 non-null    int64  
 3   yr          731 non-null    int64  
 4   mnth        731 non-null    int64  
 5   holiday     731 non-null    int64  
 6   weekday     731 non-null    int64  
 7   workingday  731 non-null    int64  
 8   weathersit  731 non-null    int64  
 9   temp        731 non-null    float64
 10  atemp       731 non-null    float64
 11  hum         731 non-null    float64
 12  windspeed   731 non-null    float64
 13  casual      731 non-null    int64  
 14  registered  731 non-null    int64  
 15  cnt         731 non-null    int64  
dtypes: float64(4), int64(11), object(1)
memory usage: 88.6+ KB
Run Code Online (Sandbox Code Playgroud)

这是数据的一个片段 数据片段 当我尝试使用 seaborn 绘制 regplot 时 -

>>> sns.regplot(x="casual", y="cnt", data=bike_df);

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-54-68533af96906> in <module>
----> 1 sns.regplot(x="casual", y="cnt", data=bike_df);

~\AppData\Local\Continuum\anaconda3\envs\rstudio\lib\site-packages\seaborn\regression.py in regplot(x, y, data, x_estimator, x_bins, x_ci, scatter, fit_reg, ci, n_boot, units, seed, order, logistic, lowess, robust, logx, x_partial, y_partial, truncate, dropna, x_jitter, y_jitter, label, color, marker, scatter_kws, line_kws, ax)
    816     scatter_kws["marker"] = marker
    817     line_kws = {} if line_kws is None else copy.copy(line_kws)
--> 818     plotter.plot(ax, scatter_kws, line_kws)
    819     return ax
    820 

~\AppData\Local\Continuum\anaconda3\envs\rstudio\lib\site-packages\seaborn\regression.py in plot(self, ax, scatter_kws, line_kws)
    363 
    364         if self.fit_reg:
--> 365             self.lineplot(ax, line_kws)
    366 
    367         # Label the axes

~\AppData\Local\Continuum\anaconda3\envs\rstudio\lib\site-packages\seaborn\regression.py in lineplot(self, ax, kws)
    406         """Draw the model."""
    407         # Fit the regression model
--> 408         grid, yhat, err_bands = self.fit_regression(ax)
    409         edges = grid[0], grid[-1]
    410 

~\AppData\Local\Continuum\anaconda3\envs\rstudio\lib\site-packages\seaborn\regression.py in fit_regression(self, ax, x_range, grid)
    214             yhat, yhat_boots = self.fit_logx(grid)
    215         else:
--> 216             yhat, yhat_boots = self.fit_fast(grid)
    217 
    218         # Compute the confidence interval at each grid point

~\AppData\Local\Continuum\anaconda3\envs\rstudio\lib\site-packages\seaborn\regression.py in fit_fast(self, grid)
    239                                     n_boot=self.n_boot,
    240                                     units=self.units,
--> 241                                     seed=self.seed).T
    242         yhat_boots = grid.dot(beta_boots).T
    243         return yhat, yhat_boots

~\AppData\Local\Continuum\anaconda3\envs\rstudio\lib\site-packages\seaborn\algorithms.py in bootstrap(*args, **kwargs)
     83     for i in range(int(n_boot)):
     84         resampler = integers(0, n, n)
---> 85         sample = [a.take(resampler, axis=0) for a in args]
     86         boot_dist.append(f(*sample, **func_kwargs))
     87     return np.array(boot_dist)

~\AppData\Local\Continuum\anaconda3\envs\rstudio\lib\site-packages\seaborn\algorithms.py in <listcomp>(.0)
     83     for i in range(int(n_boot)):
     84         resampler = integers(0, n, n)
---> 85         sample = [a.take(resampler, axis=0) for a in args]
     86         boot_dist.append(f(*sample, **func_kwargs))
     87     return np.array(boot_dist)

TypeError: Cannot cast array data from dtype('int64') to dtype('int32') according to the rule 'safe'
Run Code Online (Sandbox Code Playgroud)

我尝试使用 dtypes 更改所有行的数据类型,如下所示 -

>>> bike_df['cnt'] = bike_df['cnt'].astype(np.int32)
Run Code Online (Sandbox Code Playgroud)

但这并没有帮助,并且在绘图时再次出现相同的错误。

任何建议表示赞赏。

提前致谢。

Joh*_*anC 16

更新:此错误已在 Seaborn 0.10.1 版(2020 年 4 月)中解决。

我遇到了同样的问题。这是Seaborn 的 github 上的第 1950 期。与运行 32 位版本的 numpy 相关。下个版本会解决。

为了解决这个问题,我更改了 Seaborn 的 algorithm.py 本地版本的第 84 行:

resampler = integers(0, n, n, dtype=np.int_)
Run Code Online (Sandbox Code Playgroud)

这发生在:

  • numpy 版本:1.18.1

  • 海洋版本:0.10.0


小智 5

我的机器也有这个问题!!

我已经尝试像 JohanC 提到的那样修改 Seaborn 的 algorithm.py 代码,但它没有用......

我意识到我的 python 版本是 32 位的,所以我安装了一个更新的 python 64 位版本并运行相同的代码。

我下载并安装的版本是这个链接的64 位 (3.8.2) 。

这使我的 python 运行脚本没有问题!!