Skip to content

Instantly share code, notes, and snippets.

View aliafani's full-sized avatar

Ali Akbar Fani aliafani

View GitHub Profile
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
"""
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):
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 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):
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
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
"""
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
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:
@aliafani
aliafani / findKnn.py
Last active June 25, 2020 00:59
This function will calculate the k shortest distance between two matrices
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
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: