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):

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

"""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.
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(
@Coldsp33d
Coldsp33d / list_interleave_benchmark.py
Created June 27, 2019 15:42
Interleave two or more lists
from itertools import chain
import perfplot
def cs1(l):
def _cs1(l):
for i, x in enumerate(l, 1):
yield x
yield f'{x}_{i}'
return list(_cs1(l))
def load_data(datafile, encoder=None):
data = pd.read_csv(datafile, header=0, low_memory=False)
data_y = data[['job_performance']]
data_x = data.drop(['job_performance'], axis=1)
data_x.replace([np.inf, -np.inf], np.nan, inplace=True)
data_x.fillna(data_x.mean(), inplace=True)
if not encoder: