TypeError:在绘制s​​eaborn.regplot时,无法根据规则``安全''将数组数据从dtype('int64')转换为dtype('int32') [英] TypeError: Cannot cast array data from dtype('int64') to dtype('int32') according to the rule 'safe' while plotting a seaborn.regplot

查看:375
本文介绍了TypeError:在绘制s​​eaborn.regplot时,无法根据规则``安全''将数组数据从dtype('int64')转换为dtype('int32')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用seaborn绘制一个regplot,但我却无法绘制它并面对 TypeError:无法根据以下说明将数组数据从dtype('int64')转换为dtype('int32')规则安全



我的数据有731行和16列-

 > >> bike_df.info()

<类‘pandas.core.frame.DataFrame’>
RangeIndex:731个条目,0到730
数据列(总共16列):
#列Non-Null Count Dtype
--------- ------------ -----
0瞬时731非空int64
1 dteday 731非空对象
2季节731非空int64
3年731非空int64
4个月731非空int64
5假期731非空int64
6个工作日731非空int64
7个工作日731非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非空float64
13临时731非空int64
14已注册731非空int64
15 cnt 731非空int64
dtypes:float64 (4),int64(11),对象(1)
内存使用量:88.6+ KB

这里是数据
的代码段
当我尝试使用seaborn绘制regplot时-

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

------------------------------------------ ---------------------------------
TypeError追溯(最近一次通话)
< ; ipython-input-54-68533af96906>在< 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,数据,x_estimator,x_bins,x_ci,散点图,fit_reg,ci,n_boot,单位,种子,顺序,物流,最低,健壮,logx,x_partial,y_partial,截断,dropna,x_jitter,y_jitter,标签,颜色,标记,scatter_kws, line_kws,ax)
816 scatter_kws [ marker] =标记
817 line_kws = {}如果line_kws为其他,则copy.copy(line_kws)
-> 818 plotter.plot(ax,scatter_kws,line_kws)
819返回斧头
820

〜\AppData\Local\Continuum\anaconda3\envs\rstudio图中的lib库站点包seabornregression.py(self,ax,scatter_kws,line_kws)
363
364 if self.fit_reg:
-> 365 self.lineplot(ax,line_kws)
366
367#标记轴

〜\AppData\Local\Continuum\anaconda3\envs\rstudio lineplot中的​​ lib站点包 seaborn regression.py(自身,ax,kws)
406绘制模型。
407#拟合回归模型
-> 408 grid,yhat,err_bands = self.fit_regression(ax)
409 edge = grid [0],grid [-1]
410

〜\AppData\Local在fit_regression(self,ax,x_range,grid)中的 continuum anaconda3 envs rstudio lib站点包 seaborn regression.py
214 yhat,yhat_boots = self。 fit_logx(grid)
215 else:
-> 216 yhat,yhat_boots = self.fit_fast(grid)
217
218#计算每个网格点的置信区间

〜\AppData\Local\Continuum\ anaconda3(envs\rstudio\lib现场站点包\seaborn\regression.py in fit_fast(自身,网格)
239 n_boot = self.n_boot,
240个单位= self。单位
-> 241 seed = self.seed).T
242 yhat_boots = grid.dot(beta_boots).T
243 return yhat,yhat_boots

〜\AppData\Local\ bootstrap(* args,** kwargs)中的seaborn\algorithms.py的continuum\anaconda3\envs\rstudio\lib\site-packages
83 for i in range(int(n_boot) ):
84重采样器=整数(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)

〜< listcomp>(。中的AppData\Local\Continuum\anaconda3\envs\rstudio\lib\site-packages\seaborn\algorithms.py。 0)i在range(int(n_boot))中的
83:
84重采样器=整数(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:根据规则 safe


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

 >> bike_df ['cnt'] = bike_df ['cnt']。astype(np.int32)

但是



任何建议都会受到赞赏。



谢谢

解决方案

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



我遇到了同样的问题。在Seaborn的github上是 issue 1950 。与运行32位版本的numpy有关。



要变通解决此问题,我更改了Seaborn算法本地版本的第84行。

 重采样器=整数(0,n,n,dtype = np.int_)

发生这种情况的原因是:




  • numpy版本:1.18.1


  • 季节性版本:0.10.0



I'm trying to plot a regplot using seaborn and i'm not unable to plot it and facing TypeError: Cannot cast array data from dtype('int64') to dtype('int32') according to the rule 'safe' .

My data has 731 rows and 16 column -

>>> 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

Here is a snippet of the data And when i'm trying to plot regplot using seaborn -

>>> 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'

I tried changing the datatypes using dtypes for all the rows like below -

>>> bike_df['cnt'] = bike_df['cnt'].astype(np.int32)

but this did not help and got the same error again while plotting.

Any suggestions are appreciated.

Thanks in advance.

解决方案

Update: this bug is solved in Seaborn version 0.10.1 (April 2020).

I encountered the same problem. It is issue 1950 at Seaborn's github. Related to running a 32-bit version of numpy. It will be solved in the next release.

To work around the problem, I changed line 84 of my local version of Seaborn's algorithm.py:

resampler = integers(0, n, n, dtype=np.int_)

This happened with:

  • numpy version: 1.18.1

  • seaborn version: 0.10.0

这篇关于TypeError:在绘制s​​eaborn.regplot时,无法根据规则``安全''将数组数据从dtype('int64')转换为dtype('int32')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆