【问题标题】:KeyError:"['class']" not found in axisKeyError:“['class']”在轴中找不到
【发布时间】:2019-11-15 10:36:39
【问题描述】:

我找到了一个关于使用 pyxll add-in for excel 的决策树算法的教程,并尝试执行。我收到一个错误:KeyError:"['class']" not found in axis.

from pyxll import xl_func
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
import pandas as pd
import os

@xl_func("float, int, int: object")
def ml_get_zoo_tree_2(train_size=0.75, max_depth=5, random_state=245245):
    # Load the zoo data
    dataset = pd.read_csv(os.path.join(os.path.dirname(__file__), "zoo.csv"))

    # Drop the animal names since this is not a good feature to split the data on
    dataset = dataset.drop("animal_name", axis=1)

    # Split the data into a training and a testing set
    features = dataset.drop("class", axis=1)
    targets = dataset["class"]

    train_features, test_features, train_targets, test_targets = \
        train_test_split(features, targets, train_size=train_size, random_state=random_state)

    # Train the model
    tree = DecisionTreeClassifier(criterion="entropy", max_depth=max_depth)
    tree = tree.fit(train_features, train_targets)

    # Add the feature names to the tree for use in predict function
    tree._feature_names = features.columns

    return tree

如果我删除了类代码的第 17 行和第 18 行,则会收到错误 NameError: name 'features' is not defined,然后当我删除功能时,我会收到错误,因为必须定义目标。

【问题讨论】:

  • 你确定列名是“class”吗?只需打印dataset.columns 并查看名称是否匹配。
  • 没有列类,如何定义特征和目标来分割数据集?
  • 只需将“类”替换为目标库的名称即可。
  • 我在数据集中没有目标列
  • 如果没有因变量即如何应用决策树您的目标列?

标签: python machine-learning scikit-learn sklearn-pandas pyxll


【解决方案1】:

您需要正确的数据集才能使用该教程。你可以从这里https://github.com/pyxll/pyxll-examples/tree/master/machine-learning下载它(和代码)。

【讨论】:

    猜你喜欢
    • 2020-04-24
    • 2019-12-17
    • 2022-06-16
    • 2023-03-09
    • 2018-11-08
    • 2018-12-06
    • 2018-04-25
    • 2017-02-01
    • 2018-11-10
    相关资源
    最近更新 更多