-
Notifications
You must be signed in to change notification settings - Fork 51
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
Feature/accuracy #73
Closed
Closed
Feature/accuracy #73
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0583e8e
Actually put v4.0.0 in the changelog
bce32c5
Added way of calculating accuracy
46f7d4f
Refactored the frequency determination to column
819b008
Added working accuracy method
0e53de8
Fixed bug with differeing order of columns to variable types. Thank-y…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
import numpy as np | ||
import pandas as pd | ||
from treelib import Tree as TreeLibTree | ||
from .node import Node | ||
from .split import Split | ||
from .column import NominalColumn, OrdinalColumn, ContinuousColumn | ||
from .stats import Stats | ||
from .invalid_split_reason import InvalidSplitReason | ||
from collections import OrderedDict | ||
|
||
class Tree(object): | ||
""" | ||
|
@@ -111,13 +113,14 @@ def from_pandas_df(df, i_variables, d_variable, alpha_merge=0.05, max_depth=2, | |
the type of dependent variable. Supported variable types are 'categorical' or | ||
'continuous' | ||
""" | ||
ind_df = df[list(i_variables.keys())] | ||
df_ordered_keys = [x for x in df.columns if x in i_variables.keys()] | ||
ind_df = df[df_ordered_keys] # preserve df column order | ||
ind_values = ind_df.values | ||
dep_values = df[d_variable].values | ||
weights = df[weight] if weight is not None else None | ||
return Tree(ind_values, dep_values, alpha_merge, max_depth, min_parent_node_size, | ||
min_child_node_size, list(ind_df.columns.values), split_threshold, weights, | ||
list(i_variables.values()), dep_variable_type) | ||
[i_variables[key] for key in df_ordered_keys], dep_variable_type) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here ☝️ |
||
|
||
def node(self, rows, ind, dep, depth=0, parent=None, parent_decisions=None): | ||
""" internal method to create a node in the tree """ | ||
|
@@ -226,6 +229,38 @@ def classification_rules(self, node=None, stack=None): | |
else: | ||
return self.classification_rules(self.get_node(node.parent), stack) | ||
|
||
def tree_predictions(self): | ||
""" | ||
Calculates the row criteria that give rise | ||
to a particular terminal node | ||
""" | ||
tree_predictions = pd.DataFrame() | ||
for node in self: | ||
if node.is_terminal: | ||
sliced_arr = np.array([x.original_vector for x in self.vectorised_array]).T[node.indices] | ||
unique_set = np.vstack({ tuple(row) for row in sliced_arr }) | ||
index = pd.MultiIndex.from_arrays(np.transpose(unique_set)) | ||
if tree_predictions.empty: | ||
tree_predictions = pd.DataFrame([[node.node_id, node.predict]] * len(index), index=index) | ||
else: | ||
tree_predictions = tree_predictions.append(pd.DataFrame([[node.node_id, node.predict]] * len(index), index=index)) | ||
tree_predictions.columns = ['node_id', 'prediction'] | ||
# need to retroactively fill missing values | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dataset may not contain all the other i variables at that node, and thus they will be missed off of the node |
||
return tree_predictions | ||
|
||
def accuracy(self, ndarr, arr): | ||
""" | ||
Calculates the accuracy of predicting the | ||
dependent variable based upon the node | ||
predictions | ||
""" | ||
tree_predictions = self.tree_predictions() | ||
index = pd.MultiIndex.from_arrays(np.transpose(ndarr)) | ||
series = pd.Series(arr, index=index, name='dep') | ||
join = tree_predictions.join(series) | ||
true_set = (join['prediction'] == join['dep']).sum() | ||
return true_set / float(len(arr)) | ||
|
||
def model_predictions(self): | ||
""" | ||
Determines the highest frequency of | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is kinda gross. Will need to discuss this with you @xulaus, it's logic that seems to be the best in a given situation, but isn't foolproof. Actually, I don't think this can every go into prod.