0
$\begingroup$

I do have the following error: AttributeError: 'DataFrame' object has no attribute 'feature_names'

appreciate your input

from sklearn.tree import DecisionTreeClassifier, export_graphviz
from sklearn import tree
from sklearn.datasets import load_wine
from IPython.display import SVG
from graphviz import Source
from IPython.display import display                               
from ipywidgets import interactive
import pandas as pd

df1 = pd.read_csv("/Users/dean/Desktop/AI TECNOMATIX EXCEL/cartest1.csv")
#print(df1)
inputs = df1.drop('StatNumOut',axis='columns')
target = df1['StatNumOut']
from sklearn.preprocessing import LabelEncoder
le_company = LabelEncoder()
le_job = LabelEncoder()
le_degree = LabelEncoder()
inputs['name_n'] = le_company.fit_transform(inputs['name'])
inputs['MachineAvailability_n'] = le_company.fit_transform(inputs['MachineAvailability'])
inputs['MachineExitLocked_n'] = le_job.fit_transform(
    inputs['MachineExitLocked'])
inputs['MachineEntranceLocked_n'] = le_degree.fit_transform(
    inputs['MachineEntranceLocked'])
inputs['MachineMTTR_n'] = le_degree.fit_transform(inputs['MachineMTTR'])
inputs['StatNumIn_n'] = le_degree.fit_transform(inputs['StatNumIn'])
inputs_n = inputs.drop(['name',
                        'MachineAvailability',
                        'MachineEntranceLocked',
                        'MachineExitLocked',
                        'MachineMTTR',
                        'StatNumIn'],
                       axis='columns')
inputs_n
target

model.score(inputs_n,target)

labels = df1.feature_names
def plot_tree(crit, split, depth, min_split, min_leaf=0.2):
    estimator = DecisionTreeClassifier(random_state = 0 
       , criterion = crit
       , splitter = split
       , max_depth = depth
       , min_samples_split=min_split
       , min_samples_leaf=min_leaf)
    estimator.fit(inputs_n, target)
    graph = Source(tree.export_graphviz(estimator
       , out_file=None
       , feature_names=labels
       , class_names=['0', '1', '2']
       , filled = True))

    display(SVG(graph.pipe(format='svg')))
    return estimator

inter=interactive(plot_tree,
                  crit = ["gini", "entropy"],
                  split = ["best", "random"],
                  depth=[1,2,3,4],
                  min_split=(0.1,1),
                  min_leaf=(0.1,0.5))
display(inter)
```
$\endgroup$
  • $\begingroup$ --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-88-da5106eae202> in <module> 1 # class labels 2 ----> 3 labels = df1.feature_names AttributeError: 'DataFrame' object has no attribute 'feature_names' $\endgroup$ – Dean Nov 21 at 17:11
  • $\begingroup$ Can you add a sample of your CSV file? $\endgroup$ – aminrd Nov 21 at 19:46
  • $\begingroup$ Its df1.columns $\endgroup$ – Cini09 Nov 22 at 6:12
1
$\begingroup$

As pointed out in the error message, a pandas.DataFrame object has no attribute named feature names. You probably meant something like df1.columns.

$\endgroup$
  • $\begingroup$ Thank you for your response I have changed it and it worked. I just got this error now which is regarding the input number of input in feature name. I am new to programing and any help is appreciated thanks. ValueError: Length of feature_names, 7 does not match number of features, 6 $\endgroup$ – Dean Nov 22 at 10:23
  • $\begingroup$ It is likely that df1 contains the features and the target variable, while the feature_names parameters accepts only the list of features. Try feature_names=labels[:-1] $\endgroup$ – Romain Reboulleau Nov 22 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.