Skip to content

Instantly share code, notes, and snippets.

function Z = projectData(X, U, K)
% Initalize
Z = zeros(size(X, 1), K);
% index to create Ureduce
Ureduce = U(:, 1:K)
for i=1:size(Z,1)
function centroids = kMeansInitCentroids(X, K)
% initialize placeholder
centroids = zeros(K, size(X, 2));
% use randperm to shuffle the row indices of X
shuffle_X = randperm(size(X,1));
% only use the first K number of shuffled indices
first_K = shuffle_X(1:K, :);
function centroids = computeCentroids(X, idx, K)
% Initialize to store new centroid values
centroids = zeros(K, n);
% iterate over number of centroids
for i = 1:K
% create a boolean vector that match the current index, then use this to index into X
has_this_centroid = X(idx == i, :);
function idx = findClosestCentroids(X, centroids)
% Set K
K = size(centroids, 1);
% Set empty indices vector
idx = zeros(size(X,1), 1);
% for every example in X...
for i = 1:size(X, 1)
/**
* Depth first search and Breadth first search traverals in JavaScript
* This example uses a binary tree with left and right nodes
* by theptrk <github.com/theptrk>
*/
function Node(value, opts = {}) {
this.value = value
this.left = opts.left ? opts.left : null
this.right = opts.right ? opts.right : null
}
import random
# The indices in state represent this tic tac toe board
#
# index 0 | index 1 | index 2
# ---------------------------
# index 3 | index 4 | index 5
# ---------------------------
# index 6 | index 7 | index 8
@theptrk
theptrk / sequelize-migration-addColumn.js
Created April 13, 2019 04:21
Run $ /node_modules/.bin/sequelize migration:generate --name 1m_user_to_dids
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addColumn(
'Dids',
'UserId',
{
type: Sequelize.INTEGER,
references: {
@theptrk
theptrk / django-custom-user.md
Last active March 23, 2019 01:52
django 2.1.7

Create a custom user model to use as the new default

Gotcha! Django comes with a default auth library that utilizes a default hidden User model. This default model cannot be easily migrated so DO NOT use this model. Even the Django docs recommend extending this into a custom user model.

Why would you want to extend the model? Imagine you later want to record a users birthdate, using the default model, there is no easy way to add this field to the model. By extending the mode you are future proofing your user model.

Gotcha! Extending the model won’t be enough. In creating your own User model, you must...

  • Step 1: Create new app with "manage.py startapp"
@theptrk
theptrk / django-starter.md
Last active March 16, 2019 03:17
start a django project

moved

@theptrk
theptrk / sublime-text-vim-remap-jj.md
Last active May 10, 2020 00:34
Sublime Text vim (vintage) remap `jj` to exit
  • Go to Sublime Text -> Preferences -> Key Bindings and add this to your Default (OSX).sublime-keymap
[
    {
        "keys": ["j", "j"],
        "command": "_enter_normal_mode",
        "args": {
            "mode": "mode_insert"
        },
 "context": [{"key": "vi_insert_mode_aware"}]