<jupyter_start><jupyter_text>Read into data<jupyter_code># Read csv
all_players = pd.read_csv('../../datasets/ASG_train.csv') 
all_stars = all_players.loc[all_players['Selected?'] == 1]
all_players.head(10)
all_players.columns<jupyter_output><empty_output><jupyter_text>Setup target and feature names<jupyter_code>target = all_players['Selected?']
target_names = ['no', 'yes']
data = all_players.drop(['Selected?', 'Year', 'Avg. Pace', 'PLAYER', 'TEAM', 'AS Last Year?'], axis=1)
feature_names = data.columns

feature_names
# [['Team Conference Rank', 'GP', 'W', 'PTS', 'REB', 'AST', 'STL', 'BLK', 'TOV', 'TS%', '3PM', 'DEFWS', 'USG%', 'PIE', 'Prior ASG Appearances']]<jupyter_output><empty_output><jupyter_text>Decision Tree Classifier<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(data, target, random_state=42)
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X_train, y_train)
clf.score(X_test, y_test)
tree.plot_tree(clf)
import graphviz 
dot_data = tree.export_graphviz(clf, out_file=None, 
...                      feature_names=feature_names,  
...                      class_names=target_names,  
...                      filled=True, rounded=True,  
...                      special_characters=True)  
graph = graphviz.Source(dot_data)  
graph<jupyter_output><empty_output><jupyter_text>Random Forest Classifier<jupyter_code>from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(n_estimators=200)
rf = rf.fit(X_train, y_train)
rf.score(X_test, y_test)
sorted(zip(rf.feature_importances_, feature_names), reverse=True)<jupyter_output><empty_output><jupyter_text>Setup Decision Tree for D3<jupyter_code>def rules(clf, features, labels, node_index=0):
    """Structure of rules in a fit decision tree classifier

    Parameters
    ----------
    clf : DecisionTreeClassifier
        A tree that has already been fit.

    features, labels : lists of str
        The names of the features and labels, respectively.

    """
    node = {}
    if clf.tree_.children_left[node_index] == -1:  # indicates leaf
        count_labels = zip(clf.tree_.value[node_index, 0], labels)
        node['name'] = ', '.join(('{} of {}'.format(int(count), label)
                                  for count, label in count_labels))
    else:
        feature = features[clf.tree_.feature[node_index]]
        threshold = clf.tree_.threshold[node_index]
        node['name'] = '{} > {}'.format(feature, threshold)
        left_index = clf.tree_.children_left[node_index]
        right_index = clf.tree_.children_right[node_index]
        node['children'] = [rules(clf, features, labels, right_index),
                            rules(clf, features, labels, left_index)]
    return node
clf = DecisionTreeClassifier(max_depth=3)
clf.fit(data, target)

rules(clf, feature_names, target_names)
import json

r = rules(clf, feature_names, target_names)
with open('rules.json', 'w') as f:
    f.write(json.dumps(r))<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup