Home > other >  IndexError: list index out of range save the children
IndexError: list index out of range save the children

Time:09-21

The import pandas as pd
The import numpy as np
The from graphviz import Source
The from IPython. Display the import SVG
The from IPython. Display the import display

Df=pd read_csv (AIQ. CSV, encoding='GBK')
data=https://bbs.csdn.net/topics/df.iloc [: :]. Values# vector form
Df. Head ()


By introducing a random function from numpy. Random import shuffle #
The shuffle (data) # random disturb data

Data_train=data [, int len (0.8 * (data)), :] # top 80% for training data selection
Data_test=data [int (0.8 * len (data) :, :] # top 20% for test data selection

# tectonic characteristics and tag
X_train=data_train [: : 6]
Y_train=data_train [:, 6]
X_test=data_test [: : 6]
Y_test=data_test [:, 6]

# import model of related functions, and training model
The from sklearn import tree
CLF=tree. DecisionTreeClassifier (criterion='gini')
CLF. Fit (x_train y_train)

# set the parameters of the output
Graph=Source (tree. Export_graphviz (
CLF, # (decision tree model)
Out_file=None,
Feature_names=[' PM2.5 and PM10, 'NO', 'SO2', 'CO', 'O3], # model corresponding to the tag name
Class_names=[' 1 ', '2', '3', '4', '5', '6'],
Filled=True,
Impurity=False,
Rounded=True,
Special_characters=True
))

The display (SVG (graph. Pipe (the format='SVG')))

Results:
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
IndexError Traceback (the most recent call last)

8 impurity=False,
9 rounded=True,
---> 10 special_characters=True
11))
12

D: \ ProgramData \ Anaconda3 \ lib \ site - packages \ sklearn \ tree \ _export py in export_graphviz (decision_tree, out_file max_depth, feature_names, class_names, label, filled, leaves_parallel, impurity, node_ids, proportion, rotate, rounded, special_characters, precision)
762 rounded=rounded, special_characters=special_characters,
763 precision=precision)
- & gt; 764 exporter. Export (decision_tree)
765
766 if return_string:

D: \ ProgramData \ Anaconda3 \ lib \ site - packages \ sklearn \ tree \ _export py in export (self, decision_tree)
405 else:
406 self. Recurse (decision_tree. Tree_, 0,
- & gt; 407 criterion=decision_tree. Criterion)
408
409 self. Tail ()

D: \ ProgramData \ Anaconda3 \ lib \ site - packages \ sklearn \ tree \ _export py in recurse (self, tree, node_id, criterion, the parent, the depth)
465 self. Out_file. Write (
466 '[label=% d % s' % (node_id, self node_to_str (tree, node_id,
- & gt; 467 criterion)))
468
469 if self. Filled:

D: \ ProgramData \ Anaconda3 \ lib \ site - packages \ sklearn \ tree \ _export py in node_to_str (self, tree, node_id, criterion)
335 node_string +="class="
336 if self. Class_names is not True:
- & gt; 337 class_name=self. Class_names [np argmax (value)]
338 else:
339 class_name="y % s % s % s" % (characters [1],

IndexError: list index out of range


  • Related