我正在尝试使用seaborn绘制regplot,但我无法绘制它并面临TypeError:无法根据以下方式将数组数据从dtype('int64')转换为dtype('int32')规则“安全”。
我的数据有 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
这是数据的片段 当我尝试使用 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'
我尝试使用 dtypes 更改所有行的数据类型,如下所示 -
>>> bike_df['cnt'] = bike_df['cnt'].astype(np.int32)
但这并没有帮助,并且在绘图时再次出现相同的错误。
如有任何建议,我们将不胜感激。
提前致谢。
最佳答案
更新:此错误已在 Seaborn 版本 0.10.1(2020 年 4 月)中解决。
我也遇到了同样的问题。是issue 1950在 Seaborn 的 github 上。与运行 32 位版本的 numpy 相关。将在下一个版本中解决。
为了解决这个问题,我更改了本地版本 Seaborn's algorithm.py 的第 84 行:
resampler = integers(0, n, n, dtype=np.int_)
这发生在:
numpy 版本:1.18.1
seaborn 版本:0.10.0
关于python - TypeError:在绘制 seaborn.regplot 时,无法根据规则 ('int64' 将数组数据从 dtype ('int32' )转换为 dtype 'safe' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60060301/