Pandas :TypeError: float() argument must be a string or a number, not 'pandas._libs.interval.Interval'
Pandas :TypeError: float() argument must be a string or a number, not 'pandas._libs.interval.Interval'
我正在尝试做心脏病的机器学习练习题,来自 kaggle 的数据集。
然后我尝试将数据分成训练集和测试集,然后将模型组合成单个函数并进行预测,这个错误出现在 jupyter notebook 中。
这是我的代码:
# Split data into X and y
X = df.drop("target", axis=1)
y = df["target"]
拆分
# Split data into train and test sets
np.random.seed(42)
# Split into train & test set
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2)
预测函数
# Put models in a dictionary
models = {"Logistic Regression": LogisticRegression(),
"KNN": KNeighborsClassifier(),
"Random Forest": RandomForestClassifier()}
# Create a function to fit and score models
def fit_and_score(models, X_train, X_test, y_train, y_test):
"""
Fits and evaluates given machine learning models.
models : a dict of differetn Scikit-Learn machine learning models
X_train : training data (no labels)
X_test : testing data (no labels)
y_train : training labels
y_test : test labels
"""
# Set random seed
np.random.seed(42)
# Make a dictionary to keep model scores
model_scores = {}
# Loop through models
for name, model in models.items():
# Fit the model to the data
model.fit(X_train, y_train)
# Evaluate the model and append its score to model_scores
model_scores[name] = model.score(X_test, y_test)
return model_scores
当我 运行 这段代码时,出现了那个错误
model_scores = fit_and_score(models=models,
X_train=X_train,
X_test=X_test,
y_train=y_train,
y_test=y_test)
model_scores
这是错误
您的 X_train
、y_train
或两者似乎包含非浮点数的条目。
在代码中的某个位置,尝试使用
X_train = X_train.astype(float)
y_train = y_train.astype(float)
X_test = X_test.astype(float)
y_test = y_test.astype(float)
这将起作用并且错误将消失,或者其中一个转换将失败,此时您将需要决定如何(或是否)将数据编码为浮点数。
我正在尝试做心脏病的机器学习练习题,来自 kaggle 的数据集。 然后我尝试将数据分成训练集和测试集,然后将模型组合成单个函数并进行预测,这个错误出现在 jupyter notebook 中。
这是我的代码:
# Split data into X and y
X = df.drop("target", axis=1)
y = df["target"]
拆分
# Split data into train and test sets
np.random.seed(42)
# Split into train & test set
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2)
预测函数
# Put models in a dictionary
models = {"Logistic Regression": LogisticRegression(),
"KNN": KNeighborsClassifier(),
"Random Forest": RandomForestClassifier()}
# Create a function to fit and score models
def fit_and_score(models, X_train, X_test, y_train, y_test):
"""
Fits and evaluates given machine learning models.
models : a dict of differetn Scikit-Learn machine learning models
X_train : training data (no labels)
X_test : testing data (no labels)
y_train : training labels
y_test : test labels
"""
# Set random seed
np.random.seed(42)
# Make a dictionary to keep model scores
model_scores = {}
# Loop through models
for name, model in models.items():
# Fit the model to the data
model.fit(X_train, y_train)
# Evaluate the model and append its score to model_scores
model_scores[name] = model.score(X_test, y_test)
return model_scores
当我 运行 这段代码时,出现了那个错误
model_scores = fit_and_score(models=models,
X_train=X_train,
X_test=X_test,
y_train=y_train,
y_test=y_test)
model_scores
这是错误
您的 X_train
、y_train
或两者似乎包含非浮点数的条目。
在代码中的某个位置,尝试使用
X_train = X_train.astype(float)
y_train = y_train.astype(float)
X_test = X_test.astype(float)
y_test = y_test.astype(float)
这将起作用并且错误将消失,或者其中一个转换将失败,此时您将需要决定如何(或是否)将数据编码为浮点数。