我正在尝试对来自熊猫数据帧的一组数据进行简单的线性拟合。这是我的代码:
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()我收到以下错误:
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。
我哪里错了?
发布于 2020-03-14 12:53:18
出现TypeError是因为数据帧df类型是64位int,而预期类型为32位int。从64位到32位整数向下转换是不安全的-它会带来溢出的危险,因此不会自动执行。如果您确信您的数据可以安全地存储为32位整数,那么解决错误的一种方法应该是通过pandas.DataFrame.astype方法显式强制转换。也就是说。
sns.jointplot(x='RM', y='MEDV', data=df.astype('int32'), kind='reg', height=10)https://stackoverflow.com/questions/60679435
复制相似问题