Carmelo Velez Senior














Sign up
Course Notes: Machine Learning with Tree-Based Models in Python
  • AI Chat
  • Code
  • Report
  • Beta
    Spinner

    Course Notes

    Use this workspace to take notes, store code snippets, or build your own interactive cheatsheet! For courses that use data, the datasets will be available in the datasets folder.

    # Import any packages you want to use here
    

    Take Notes

    Add notes here about the concepts you've learned and code cells with code you want to keep.

    Add your notes here

    # Add your code snippets here
    

    Classification and Regression Trees

    Decision tree for classification

    Train your first classification tree

    # Import DecisionTreeClassifier from sklearn.tree
    from sklearn.tree import DecisionTreeClassifier
    
    # Instantiate a DecisionTreeClassifier 'dt' with a maximum depth of 6
    dt = DecisionTreeClassifier(max_depth=6, random_state=SEED)
    
    # Fit dt to the training set
    dt.fit(X_train, y_train)
    
    # Predict test set labels
    y_pred = dt.predict(X_test)
    print(y_pred[0:5])

    Evaluate the classification tree

    # Import accuracy_score
    from sklearn.metrics import accuracy_score
    
    # Predict test set labels
    y_pred = dt.predict(X_test)
    
    # Compute test set accuracy  
    acc = accuracy_score(y_test, y_pred)
    print("Test set accuracy: {:.2f}".format(acc))

    Logistic regression vs classification tree

    # Import LogisticRegression from sklearn.linear_model
    from sklearn.linear_model import  LogisticRegression
    
    # Instatiate logreg
    logreg = LogisticRegression(random_state=1)
    
    # Fit logreg to the training set
    logreg.fit(X_train, y_train)
    
    # Define a list called clfs containing the two classifiers logreg and dt
    clfs = [logreg, dt]
    
    # Review the decision regions of the two classifiers
    plot_labeled_decision_regions(X_test, y_test, clfs)

    Classification tree Learning

    Using entropy as a criterion