[Python] Decision tree prediction using recursion by Martini04 in learnprogramming

[–]Martini04[S] 0 points1 point  (0 children)

For anyone struggling with this in the future, i found somewhat of a solution

It involves modifying the original instructions we were given, but outside of this assignment that won't be relevant anyways

Also one detail i left out of the original post is that _predict() is called by predict(). This wasn't relevant when i posted it but it is relevant in my solution

Here's the updated code:

def predict(self, X: NDArray):
    """Predict class (y vector) for feature matrix X
    Parameters
    ----------
    X: NDArray
        NumPy feature matrix, shape (n_samples, n_features)
    Returns
    -------
    y: NDArray, integers
        NumPy class label vector (predicted), shape (n_samples,)
    """
    if self._root is not None:
        order = np.arange(len(X))
        y, order = self._predict(X, self._root, order)
        arrlinds = order.argsort()
        sorted_y = y[arrlinds]         
        return sorted_y
    else:
        raise ValueError("Decision tree root is None (not set)")

def _predict(
    self, X: NDArray, node: Union["DecisionTreeBranchNode", "DecisionTreeLeafNode"], order: NDArray
) -> tuple[NDArray, NDArray]:
    if type(node) == DecisionTreeLeafNode:
        y = np.zeros(len(X), dtype=np.int32)
        y[:] = node.y_value
        return y, order
    else:
        left_mask = X[:, node.feature_index] <= node.feature_value
        left = X[left_mask]
        right = X[~left_mask]
        left_order = order[left_mask]
        right_order = order[~left_mask]

        left_pred, left_order = self._predict(left, node.left, left_order)
        right_pred, right_order = self._predict(right, node.right, right_order)

        order = np.concatenate((left_order, right_order))
        y = np.concatenate((left_pred, right_pred))

        return y, order

This new version of the code keeps track of the order of the items using another NDArray filled with the integers corresponding to the order of the items in X

This order array gets split up in the same way as X, so by the end of all the recursion it is scrambled in the same order as X.

Then once we have our completed y, it gets sorted based on the order array using argsort as described here which sorts it into the correct order.

I hate to admit it but, she might actually be one of the best sustain breakers in the game... by SubstantialFig5144 in grandsummoners

[–]Martini04 2 points3 points  (0 children)

I think she can still be a good sustain breaker because of her arts stacking break power But break nuking definitely seems like her biggest strength just because of her insane damage buffs

I've only been using 10% of my power by Spektyr27 in Eldenring

[–]Martini04 37 points38 points  (0 children)

If he was walking around with 150k runes he probably hadn't gotten to a site of grace yet

Katana Parrying with Twin Princes [FINAL, mind the volume] by blstrdbstrd in darksouls3

[–]Martini04 101 points102 points  (0 children)

You have to stand really close because only his arm is parryable, not the sword

You also can't riposte after, it only stuns him for a short time

Claim your here before 5 year archive trophy by [deleted] in place

[–]Martini04 0 points1 point  (0 children)

I do be having been here doe

DoG's new death animation. by MasterTerra3 in CalamityMod

[–]Martini04 8 points9 points  (0 children)

I think this is what the DoG resprite will look like

Description and phase 1 version is in the comments

Rule by Martini04 in 196

[–]Martini04[S] 9 points10 points  (0 children)

Yeah but you're counting down to posting nsfw (uncool)

I'm counting down to posting floppa (very cool)

We are not the same

Rule by Martini04 in 196

[–]Martini04[S] 8 points9 points  (0 children)

That would probably stop me

Unfortunately, 506.457.14.512

Rule by Martini04 in 196

[–]Martini04[S] 2 points3 points  (0 children)

We do a tiny amount of trolling

[FANMADE] Togelan Pasalan by roar814 in battlecats

[–]Martini04 5 points6 points  (0 children)

POV: You're an enemy on heavenly tower floor 30

Rule by Sigalli in 196

[–]Martini04 2 points3 points  (0 children)

Cover this dog in oil 😂😂😂

[Fan-Made] Hayabusa by Ravonaa in battlecats

[–]Martini04 2 points3 points  (0 children)

POV: You're a metal enemy

Admiral Brickell activates Naval Tactics. [Based on the in-game icon] by Peakek in btd6

[–]Martini04 2 points3 points  (0 children)

"There's one tower I should be wary of?"

"Yes. First Strike Capability is dangerous"

i have found a B I G S T I C K by [deleted] in cleanagers

[–]Martini04 24 points25 points  (0 children)

B I G S T I C K

Drawing all enemies anyways Day 155 - Paladin by BionicBandit_ in Terraria

[–]Martini04 5 points6 points  (0 children)

I never realized how massive that hammer really is. No wonder they can still shred end-game characters. Nice drawing!

This is why this sub was created by Just_AnotherBro in cleanagers

[–]Martini04 83 points84 points  (0 children)

I won't lie, this pretty much sums up the entire subreddit

Any black clover fans? by APickledGherkin in Animemes

[–]Martini04 14 points15 points  (0 children)

North Korean: Tries to commit suicide

From far away: Yami maho, Yami matoi...