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
def evaltree(root,xTe): | |
"""Evaluates xTe using decision tree root. | |
Input: | |
root: TreeNode decision tree | |
xTe: n x d matrix of data points | |
Output: | |
pred: n-dimensional vector of predictions | |
""" |
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
def all_same(items): | |
if len(items) > 0: | |
return np.all(items == items[0]) | |
elif len(items) is 0: | |
return False | |
# This funciton basically checks majority of labels in the list | |
def majority_vote(y): |
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
def cart(xTr,yTr): | |
"""Builds a CART tree. | |
The maximum tree depth is defined by "maxdepth" (maxdepth=2 means one split). | |
Each example can be weighted with "weights". | |
Args: | |
xTr: n x d matrix of data | |
yTr: n-dimensional vector |
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 is TreeNode class | |
class TreeNode(object): | |
"""Tree class. | |
(You don't need to add any methods or fields here but feel | |
free to if you like. The tests will only reference the fields | |
defined in the constructor below, so be sure to set these | |
correctly.) | |
""" | |
def __init__(self, left, right, feature, cut, prediction): |
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
def sqsplit(xTr, yTr): | |
"""Finds the best feature, cut value, and loss value. | |
Input: | |
xTr: n x d matrix of data points | |
yTr: n-dimensional vector of labels | |
Output: | |
feature: index of the best cut's feature | |
cut: cut-value of the best cut |
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
def sqimpurity(yTr): | |
"""Computes the weighted variance of the labels | |
Input: | |
yTr: n-dimensional vector of labels | |
Output: | |
impurity: weighted variance / squared loss impurity of this data set | |
""" |
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
def knnclassifier(xTr,yTr,xTe,k): | |
""" | |
function preds=knnclassifier(xTr,yTr,xTe,k); | |
k-nn classifier | |
Input: | |
xTr = nxd input matrix with n row-vectors of dimensionality d | |
xTe = mxd input matrix with m row-vectors of dimensionality d | |
k = number of nearest neighbors to be found |
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
def accuracy(truth,preds): | |
""" | |
function output=accuracy(truth,preds) | |
Analyzes the accuracy of a prediction against the ground truth | |
Input: | |
truth = n-dimensional vector of true class labels | |
preds = n-dimensional vector of predictions | |
Output: |
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
def findknn(xTr,xTe,k): | |
""" | |
function [indices,dists]=findknn(xTr,xTe,k); | |
Finds the k nearest neighbors of xTe in xTr. | |
Input: | |
xTr = nxd input matrix with n row-vectors of dimensionality d | |
xTe = mxd input matrix with m row-vectors of dimensionality d | |
k = number of nearest neighbors to be found |
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
def calculatedistance(X,Z=None): | |
# Input: | |
# X: nxd data matrix with n vectors (rows) of dimensionality d | |
# Z: mxd data matrix with m vectors (rows) of dimensionality d | |
# | |
# Output: | |
# Matrix D of size nxm | |
# D(i,j) is the Euclidean distance of X(i,:) and Z(j,:) | |
if Z is None: |