首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用连续数值目标类可视化回归树模型?

用连续数值目标类可视化回归树模型?
EN

Stack Overflow用户
提问于 2022-05-25 05:19:48
回答 1查看 86关注 0票数 0

我正在练习这个来自Kaggle (https://www.kaggle.com/datasets/kumarajarshi/life-expectancy-who?select=Life+Expectancy+Data.csv)的预期寿命数据集,我想训练和可视化一个分类和回归树模型。但是,我一直收到一个错误,上面写着"InvocationException: GraphViz的可执行文件未找到“。我想知道这是否是因为连续的数值目标数据集类型的性质?我怎样才能将模型可视化?

代码:

代码语言:javascript
复制
import warnings
warnings.filterwarnings('ignore') 

import pandas as pd
import numpy as np
import seaborn as sn
from sklearn import datasets
from sklearn import metrics
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder
from sklearn.tree import export_graphviz
import matplotlib.pyplot as plt,pydotplus
from IPython.display import Image,display

data = pd.read_csv('Life Expectancy Data.csv')
data = data.dropna(how = 'any')

#feature selection
data = data.drop(columns=['infant deaths', ' thinness 5-9 years', 'Alcohol', 'percentage expenditure', 'Hepatitis B', 'Total expenditure', 'Population', ' thinness 5-9 years', 'Year', 'Country'])

# Creating a instance of label Encoder.
le = LabelEncoder()

# Using .fit_transform function to fit label
# encoder and return encoded label
label = le.fit_transform(data['Status'])

# removing the column 'Status' from df
data.drop('Status', axis=1, inplace=True)

# Appending the array to our dataFrame
# with column name 'Status'
data['Status'] = label

#training model
model_data = data
X = data.drop(columns=['Life expectancy '])
y = data['Life expectancy ']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)

model = DecisionTreeRegressor()
model.fit(X_train, y_train)

#visualizing tree
LEtree = tree.export_graphviz(model, 
                feature_names = ['Adult Mortality', 'Measles', ' BMI', 'under-five deaths', 'Polio', 'Diphtheria', ' HIV/AIDS', 'GDP', ' thinness  1-19 years', 'Income composition of resources', 'Schooling', 'Status'],
               class_names = y,
               label = 'all',
               rounded = True,
               filled = True)

graph=pydotplus.graph_from_dot_data(LEtree)
display(Image(graph.create_png()))

完全错误消息:

代码语言:javascript
复制
InvocationException                       Traceback (most recent call last)
Input In [27], in <cell line: 2>()
      1 graph=pydotplus.graph_from_dot_data(LEtree)
----> 2 display(Image(graph.create_png()))

File ~\Anaconda3\lib\site-packages\pydotplus\graphviz.py:1797, in Dot.__init__.<locals>.<lambda>(f, prog)
   1792 # Automatically creates all the methods enabling the creation
   1793 # of output in any of the supported formats.
   1794 for frmt in self.formats:
   1795     self.__setattr__(
   1796         'create_' + frmt,
-> 1797         lambda f=frmt, prog=self.prog: self.create(format=f, prog=prog)
   1798     )
   1799     f = self.__dict__['create_' + frmt]
   1800     f.__doc__ = (
   1801         '''Refer to the docstring accompanying the'''
   1802         ''''create' method for more information.'''
   1803     )

File ~\Anaconda3\lib\site-packages\pydotplus\graphviz.py:1959, in Dot.create(self, prog, format)
   1957     self.progs = find_graphviz()
   1958     if self.progs is None:
-> 1959         raise InvocationException(
   1960             'GraphViz\'s executables not found')
   1962 if prog not in self.progs:
   1963     raise InvocationException(
   1964         'GraphViz\'s executable "%s" not found' % prog)

InvocationException: GraphViz's executables not found
EN

回答 1

Stack Overflow用户

发布于 2022-07-21 09:59:53

尝试在适当的目录中安装Graphviz

您可以从conda命令提示符在Anaconda中安装以下命令-

conda安装-c conda-伪造python

并替换以前安装的graphviz目录,这可能会帮助您解决问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72372296

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档