python - mlxtendplot_decision_regions 模型适合 Pandas DataFrame?

标签 python pandas machine-learning data-visualization mlxtend

我是 mlxtend 的 plot_decision_regions 函数的忠实粉丝,( http://rasbt.github.io/mlxtend/#exampleshttps://stackoverflow.com/a/43298736/1870832 )

它接受一个X(一次仅两列)、y和(拟合的)分类器clf对象,然后提供模型预测、真实 y 值和一对自变量之间关系的非常精彩的可视化。

一些限制: Xy 必须是 numpy 数组,并且 clf 需要有一个 predict() 方法。很公平。我的问题是,就我而言,我想要可视化的分类器 clf 对象已经安装在 Pandas DataFrame 上......

import numpy as np
import pandas as pd
import xgboost as xgb

import matplotlib
matplotlib.use('Agg')
from mlxtend.plotting import plot_decision_regions
import matplotlib.pyplot as plt


# Create arbitrary dataset for example
df = pd.DataFrame({'Planned_End': np.random.uniform(low=-5, high=5, size=50),
                   'Actual_End':  np.random.uniform(low=-1, high=1, size=50),
                   'Late':        np.random.random_integers(low=0,  high=2, size=50)}
)

# Fit a Classifier to the data
# This classifier is fit on the data as a Pandas DataFrame
X = df[['Planned_End', 'Actual_End']]
y = df['Late']

clf = xgb.XGBClassifier()
clf.fit(X, y)

所以现在当我尝试使用 plot_decision_regions 将 X/y 作为 numpy 数组传递时......

# Plot Decision Region using mlxtend's awesome plotting function
plot_decision_regions(X=X.values,
                      y=y.values,
                      clf=clf,
                      legend=2)

我(可以理解)收到一个错误,即模型无法找到其训练数据集的列名称

ValueError: feature_names mismatch: ['Planned_End', 'Actual_End'] ['f0', 'f1']
expected Planned_End, Actual_End in input data
training data did not have the following fields: f1, f0

在我的实际情况中,避免在 Pandas DataFrames 上训练我们的模型将是一件大事。有没有办法仍然为在 Pandas DataFrame 上训练的分类器生成决策区域图?

最佳答案

尝试改变:

X = df[['Planned_End', 'Actual_End']].values
y = df['Late'].values

然后继续:

clf = xgb.XGBClassifier()
clf.fit(X, y)

plot_decision_regions(X=X,
                      y=y,
                      clf=clf,
                      legend=2)

fit & plot使用X.valuesy.values

关于python - mlxtendplot_decision_regions 模型适合 Pandas DataFrame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49167961/

相关文章:

python 比较两个大列表并处理一个

python - Flask Dispatcher 中间件 session

python - 根据列标准过滤行的问题

machine-learning - LibSVM 中的特征值缩放是否有必要?

python - 查找 ANOVA 第一个线性模型的 F 统计值

python - 在不使用类的情况下减去 Python 中的两个列表

python - javascript 的 _.where 在 python 中

Python dataframe - 3 列中的数据值映射到其他 3 列下

python - Pandas 中的多索引 fillna

python - python中的文本生成算法