首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sns.jointplot中的TypeError

sns.jointplot中的TypeError
EN

Stack Overflow用户
提问于 2020-03-14 11:17:13
回答 1查看 152关注 0票数 0

我正在尝试对来自熊猫数据帧的一组数据进行简单的线性拟合。这是我的代码:

代码语言:javascript
复制
import numpy as np
import pandas as pd
import sklearn
import seaborn as sns
import sys
import matplotlib.pyplot as plt

df = pd.read_csv("../Machine Learning/housing.data", header=None,
                 delim_whitespace=True)

col_name = ['Crime', 'ZN', 'Indus', 'Chas', 'Nox','RM', 'Age', 'Dis', 
            'Rad', 'Tax', 'PTRatio', 'B', 'LSTAT', 'MEDV']

df.columns = col_name

X = df.RM.values.reshape(-1,1)
y = df.MEDV.values.reshape(-1,1)

from sklearn.linear_model import LinearRegression

model = LinearRegression()

model.fit(X,y)

plt.figure(figsize=(12,10))
sns.jointplot(x='RM', y='MEDV', data=df, kind='reg', height=10)
plt.show()

我收到以下错误:

代码语言:javascript
复制
Traceback (most recent call last):
  File "regression_scipy.py", line 74, in <module>
    sns.jointplot(x='RM', y='MEDV', data=df, kind='reg', height=10) ...
TypeError: Cannot cast array data from dtype('int64') to dtype('int32') according to the rule 'safe'

我检查了我的数组x和y,它们是numpy.ndarrays

我哪里错了?

EN

回答 1

Stack Overflow用户

发布于 2020-03-14 12:53:18

出现TypeError是因为数据帧df类型是64位int,而预期类型为32位int。从64位到32位整数向下转换是不安全的-它会带来溢出的危险,因此不会自动执行。如果您确信您的数据可以安全地存储为32位整数,那么解决错误的一种方法应该是通过pandas.DataFrame.astype方法显式强制转换。也就是说。

代码语言:javascript
复制
sns.jointplot(x='RM', y='MEDV', data=df.astype('int32'), kind='reg', height=10)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60679435

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档