Skip to content

Instantly share code, notes, and snippets.

Avatar

cs95 Coldsp33d

  • Mountain View, CA
View GitHub Profile
@Coldsp33d
Coldsp33d / pprint.c
Last active Jul 2, 2016
Pretty print in NumPy format, any 1D or 2D C matrix (as long as it is stored in 1D form) by specifying dimensions.
View pprint.c
#include <stdio.h>
typedef float fpoint_t;
void print(fpoint_t* m, int row_size, int col_size)
{
int i, j;
printf(" array([");
View conway.py
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):
View Thoughts.md

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:

View Typing.md

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

View mnist_cnn.py
"""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.
View string_concat_benchmark.py
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))
View remove_unwanted_string_benchmark.py
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')))
View selective_handler_ohe.py
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 Jun 27, 2019
Interleave two or more lists
View list_interleave_benchmark.py
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))
View ohe_truncate_cols.py
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: