Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG][Experimental] AttributeError: 'numpy.ndarray' object has no attribute 'to_numpy' #67

Open
sbobek opened this issue Feb 6, 2025 · 0 comments
Labels
bug Something isn't working experimental

Comments

@sbobek
Copy link
Owner

sbobek commented Feb 6, 2025

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[5], line 6
      3 from lux.pyuid3.data import Data
      4 data = Data.parse_dataframe(test[features+[target]])
----> 6 predictions = [int(x.get_name()) for x in lux.uid3.predict(data.instances)]
      7 print(f'Accuracy : {accuracy_score(predictions, test[target])}')

File ~/Software/lux/src/lux/pyuid3/uid3.py:554, in UId3.predict(self, X)
    552 def predict(self, X: pd.DataFrame):
    553     predictions = []
--> 554     for instance in X.to_numpy():
    555         att_stats = self.tree.predict(instance, list(X.columns))
    556         predictions.append(att_stats.get_most_probable())

AttributeError: 'numpy.ndarray' object has no attribute 'to_numpy'

Code to reproduce:

from lux.lux import LUX
from sklearn import datasets
from sklearn.model_selection import train_test_split
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
from sklearn.ensemble import RandomForestClassifier
wine = datasets.load_wine()


features = [f.replace('/','_') for  f in wine['feature_names']]
target = 'class'
rs=42
fraction=0.1

#create daatframe with columns names as strings (LUX accepts only DataFrames with string columns names)
df_wine = pd.DataFrame(wine.data,columns=features)
df_wine[target] = wine.target

#train classifier
train, test = train_test_split(df_wine, random_state=rs)
clf = RandomForestClassifier(random_state=42)#svm.SVC(probability=True, random_state=rs)
clf.fit(train[features],train[target])
clf.score(test[features],test[target])
## Without predictor uncertainty
#pick some instance from datasetr
i2e = train[features].sample(1, random_state=42).values
i2e

#train lux on neighbourhood equal 30% instances
lux = LUX(predict_proba = lambda x: np.round(clf.predict_proba(x)), neighborhood_size=int(len(train)*fraction),max_depth=2,  node_size_limit = 1, grow_confidence_threshold = 0 )
lux.fit(train[features], train[target], instance_to_explain=i2e,class_names=[0,1,2])

#see the justification of the instance being classified for a given class
lux.justify(np.array(i2e))
#calculate decision tree score over this dataset
from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score
from lux.pyuid3.data import Data
data = Data.parse_dataframe(test[features+[target]])
    
predictions = [int(x.get_name()) for x in lux.uid3.predict(data.instances)]
print(f'Accuracy : {accuracy_score(predictions, test[target])}')
@sbobek sbobek added bug Something isn't working experimental labels Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working experimental
Projects
None yet
Development

No branches or pull requests

1 participant