【问题标题】:Pandas :TypeError: float() argument must be a string or a number, not 'pandas._libs.interval.Interval'Pandas :TypeError: float() 参数必须是字符串或数字,而不是 'pandas._libs.interval.Interval'
【发布时间】:2021-03-30 01:03:18
【问题描述】:

我正在尝试做心脏病的机器学习实践问题,来自 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

这是错误

【问题讨论】:

  • 哪里出错了?
  • 在我上一个代码之后
  • 重新阅读 How to ask,因为您第一次阅读时似乎错过了一些关键点,即“DO NOT发布代码、数据、错误消息等的图像。 - 将文本复制或输入到问题中”(强调原文)。

标签: python pandas machine-learning jupyter-notebook kaggle


【解决方案1】:

您的X_trainy_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)

这将起作用并且错误将消失,或者其中一个转换将失败,此时您需要决定如何(或是否)将数据编码为浮点数。

【讨论】:

    猜你喜欢
    • 2019-09-24
    • 2023-02-18
    • 2020-09-18
    • 2020-04-24
    • 1970-01-01
    • 2018-01-07
    • 1970-01-01
    • 2018-02-26
    • 2022-08-13
    相关资源
    最近更新 更多