Rambatino / CHAID

A python implementation of the common CHAID algorithm
Apache License 2.0
149 stars 50 forks source link

No Attribute "from_numpy" #109

Closed diogoalvesderesende closed 4 years ago

diogoalvesderesende commented 4 years ago

Hi!

I think it is really great that you did this package. CHAID is a wonderful and useful technique. I tried to use errors but I get the following error:

model1 = tree.from_numpy(X, Y)
Traceback (most recent call last):

  File "<ipython-input-65-2de6107b2484>", line 1, in <module>
    model1 = tree.from_numpy(X, Y)

AttributeError: module 'CHAID.tree' has no attribute 'from_numpy'

Do you know what I am doing wrong?

Thanks in advance!

Rambatino commented 4 years ago

In the README.md =>

from CHAID import Tree

## create the data
ndarr = np.array(([1, 2, 3] * 5) + ([2, 2, 3] * 5)).reshape(10, 3)
df = pd.DataFrame(ndarr)
df.columns = ['a', 'b', 'c']
arr = np.array(([1] * 5) + ([2] * 5))
df['d'] = arr

>>> df
   a  b  c  d
0  1  2  3  1
1  1  2  3  1
2  1  2  3  1
3  1  2  3  1
4  1  2  3  1
5  2  2  3  2
6  2  2  3  2
7  2  2  3  2
8  2  2  3  2
9  2  2  3  2

## set the CHAID input parameters
independent_variable_columns = ['a', 'b', 'c']
dep_variable = 'd'

## create the Tree via pandas
tree = Tree.from_pandas_df(df, dict(zip(independent_variable_columns, ['nominal'] * 3)), dep_variable)
## create the same tree, but without pandas helper
tree = Tree.from_numpy(ndarr, arr, split_titles=['a', 'b', 'c'], min_child_node_size=5)

Most likely you just need to:

from CHAID import Tree
Tree.from_numpy(X, Y)

emphasis on the capital 'T'.

Let me know if this helps. If not, feel free to send a worked example over.

Rambatino commented 4 years ago

(it looks like you've already created your tree in that example - it's a class method so only applies to the Tree class, not the instance variable)

diogoalvesderesende commented 4 years ago

It was the capital T missing. Newbie mistake. Thanks a lot for the follow up!