Skip to content

Instantly share code, notes, and snippets.

@sumartoyo
sumartoyo / vars_stds.py
Created October 12, 2016 10:22
Scipy sparse matrix variance and standard deviation
import numpy as np
def vars(a, axis=None):
""" Variance of sparse matrix a
var = mean(a**2) - mean(a)**2
"""
a_squared = a.copy()
a_squared.data **= 2
return a_squared.mean(axis) - np.square(a.mean(axis))
import numpy as np
def logloss(y_true, prob_pred):
'''
https://www.kaggle.com/wiki/MultiClassLogLoss
'''
y_true = np.asarray(y_true, dtype=np.uint8)
prob_pred = np.array(prob_pred, dtype=np.float)
n_data = len(y_true)
@sumartoyo
sumartoyo / chooseTab.html
Last active June 22, 2019 09:58
Choosing one tab to run a task when multiple tabs are opened
<!DOCTYPE html>
<html>
<body>
<pre>open console</pre>
<script>
const ID_TAB = `${Math.random()}`;
const KEY_PREFIX = `handlerSomething-`;
@sumartoyo
sumartoyo / Using zustand Like Normal JavaScript Thing.md
Created January 27, 2021 19:23
Using zustand Like Normal JavaScript Thing

Global state management in React can be a foreign concept for new programmers. Over the years, we have met reducers, action types, atoms, etc. I have a friend that wants to learn web development, and I imagine it would be very difficult to teach him about these concepts.

zustand is a simple, straightforward, and un-opinionated library for global state management. Because of those traits, I am able to utilize it to manage global state like normal JavaScript thing. What I mean by "normal JavaScript thing" is there is no foreign concept involved in here. It's just a bunch of normal variables, normal functions, and occasionally normal React component. I hope my friend can understand global state management better and faster with this library.