Skip to content

Instantly share code, notes, and snippets.

View Coldsp33d's full-sized avatar

cs95 Coldsp33d

  • Mountain View, CA
View GitHub Profile
@Coldsp33d
Coldsp33d / pprint.c
Last active July 2, 2016 15:50
Pretty print in NumPy format, any 1D or 2D C matrix (as long as it is stored in 1D form) by specifying dimensions.
#include <stdio.h>
typedef float fpoint_t;
void print(fpoint_t* m, int row_size, int col_size)
{
int i, j;
printf(" array([");
import numpy as np
import time
from enum import Enum
from typing import List, Iterator, Sequence, Tuple
NEIGHBOURS = [(i, j) for i in range(-1, 2) for j in range(-1, 2) if i or j]
class State(Enum):
"""Convolutional Neural Network with the MNIST dataset using Tensorflow."""
import numpy as np
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.INFO)
def cnn_model_fn(features, labels, mode):
"""Train a CNN model on the MNIST dataset.

I was wondering if it is a good approach to define a one-line method for the sake of making the whole program more readable?

If you're assigning a function to a shorter name, yes.

short_name = module.submodule.xtrelemly_long_name_here

Now every call to module.submodule.xtrelemly_long_name_here can be substituted with short_name, for readability.

However, defining one line functions can be done like this:

This is a typing test and I'm trying to see how fast I can type. I'm trying to practice my coding skills... well, my typing skills by seeing how fast I can translate my thoughts into words on a computer. The problem with my typing style right now is that I type with my forefingers without utilizing many of my other fingers. Sometimes the occassional index finger, othertimes the ring finger. The thumb seems a reliable finger for manning the space bar so I don't disturb him much.

There is a lack of coordination between the other fingers, but my forefingers seem to be busom buddies, having no trouble whatsoever falling over each other on the keyboard to do my bidding. Sometimes, my fingers try to get cute by getting into positions they've never been in before, this inadvertently ends up costing me dearly because I make mistakes and have to backtrack to fix them. Sometimes my hands will flourish after typing a sentence, for no real reason at all — these are wasteful actions and end up reducing my wpm rate.

And

@Coldsp33d
Coldsp33d / list_concat_benchmark.py
Last active December 5, 2020 22:18
Concatenate 2 (or more) lists - Benchmarking
from itertools import chain
import perfplot
import numpy
def add(L):
x, y = L
return x.copy() + y
def chain_(L):
x, y = L
@Coldsp33d
Coldsp33d / iter_vs_vec_benchmark.py
Last active November 8, 2024 13:59
Comparing iteration and vectorisation with a simple example
import perfplot
import pandas as pd
def vec(df):
return df['A'] + df['B']
def vec_numpy(df):
return df['A'].to_numpy() + df['B'].to_numpy()
def list_comp(df):
import perfplot
import pandas as pd
import numpy as np
def brenbarn(df):
return df.assign(baz=df.bar.map(str) + " is " + df.foo)
def danielvelkov(df):
return df.assign(baz=df.apply(
lambda x:'%s is %s' % (x['bar'],x['foo']),axis=1))
import perfplot
import pandas as pd
import re
p1 = re.compile(r'\D')
p2 = re.compile(r'\d+')
def eumiro(df):
return df.assign(
result=df['result'].map(lambda x: x.lstrip('+-').rstrip('aAbBcC')))
@Coldsp33d
Coldsp33d / selective_handler_ohe.py
Created June 18, 2019 02:54
Selective error handling
from sklearn.preprocessing import OneHotEncoder
class SelectiveHandlerOHE(OneHotEncoder):
def __init__(self, *args, raise_error_cols=[], **kwargs):
kwargs['handle_unknown'] = 'ignore' # change the default
self.raise_error_cols = raise_error_cols.copy()
super().__init__(*args, **kwargs)
def check_cols(self, X):
if self.raise_error_cols and any(