Python 3.x 为什么在建立线性回归模型时会出现值误差?

Python 3.x 为什么在建立线性回归模型时会出现值误差?,python-3.x,linear-regression,Python 3.x,Linear Regression,我试图为数据集建立一个线性回归模型。将数据拆分为训练和测试后,我得到以下错误: ValueError:无法将字符串转换为浮点:'?' 这是否意味着数据集中存在null值或float值 由于我是Python新手,我不知道如何纠正这一点。有人能帮我吗 import pandas as pd from sklearn.model_selection import train_test_split from sklearn import linear_model df = pd.read_csv('ht

我试图为数据集建立一个线性回归模型。将数据拆分为训练和测试后,我得到以下错误:

ValueError:无法将字符串转换为浮点:'?' 这是否意味着数据集中存在null值或float值

由于我是Python新手,我不知道如何纠正这一点。有人能帮我吗

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn import linear_model
df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data', names = ['ID Number', 'Clump Thickness', 'Uniformity of Cell Size', 'Uniformity of Cell Shape', 'Marginal Adhesion', 'Single Epithelial Cell Size', 'Bare Nuclei', 'Bland Chromatin', 'Normal Nucleoli', 'Mitoses', 'Class'])
X = df.iloc[:, 0:9].values
y = df.iloc[:, 10].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.4, random_state = 4)
print(X_train.shape)
print(y_train.shape)
print(X_test.shape)
print(y_test.shape)
lr = linear_model.LinearRegression()
lr.fit(X_train, y_train)

您正在使用的breast-cancer-wisconsin.data数据集在第7列中有一些行的值为“?”。 因此,当创建X和Y时,不要将行以“?”作为值。


我希望这有帮助

看起来其中一列的类型为
object
。键入
X.dtype
并检查数据中每列的数据类型。是的,一列的数据类型为“Object”。我在删除该列后得到了输出。谢谢。我删除了列,然后再次进行分析,得到了输出。谢谢