Skip to content

Instantly share code, notes, and snippets.

View vaclavcadek's full-sized avatar

Václav Čadek vaclavcadek

  • Stealth
  • Prague, Czech Republic
View GitHub Profile
class Node:
def __init__(self, data):
self.data = data
self.next = None
def __next__(self):
return self.next
class LinkedList:
import numpy as np
import multiprocessing
def do_calculation(i, **kwargs):
np.random.seed()
rand=np.random.randint(10)
print(i, kwargs['foo'], kwargs['bar'], rand)
if __name__ == '__main__':
jobs = []
@vaclavcadek
vaclavcadek / periodic_task.py
Created May 23, 2017 14:16
Periodic write to file every second.
import sched, time
from datetime import datetime
s = sched.scheduler(time.time, time.sleep)
def read_signal(sc):
with open('/home/vasek/Downloads/test.csv', 'a') as log:
log.write('{timestamp},{temperature}\n'.format(timestamp=datetime.now(), temperature=23.0))
# do your stuff
s.enter(1, 1, read_signal, (sc,))
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
import time
def pltsin(ax, colors=['b']):
x = np.linspace(0,1,100)
if ax.lines:
for line in ax.lines:
@vaclavcadek
vaclavcadek / emulate_stdin.py
Created April 13, 2017 12:31
How to emulate input over stdin
import sys
import io
sys.stdin = io.StringIO("""
3 3
0 1 1 1
1 2 2 4
2 0 1 2
""".strip())
# List unique values in a DataFrame column
pd.unique(df.column_name.ravel())
# Convert Series datatype to numeric, getting rid of any non-numeric values
df['col'] = df['col'].astype(str).convert_objects(convert_numeric=True)
# Grab DataFrame rows where column has certain values
valuelist = ['value1', 'value2', 'value3']
df = df[df.column.isin(valuelist)]
@vaclavcadek
vaclavcadek / generate_bit_strings.py
Last active August 29, 2015 14:27
Generate all bit string permatation
for bits in product([0, 1], repeat=4):
print("".join(str(b) for b in bits)
@vaclavcadek
vaclavcadek / feature_selection_cv.py
Created August 7, 2015 08:03
Feature selection with cross-validation
import matplotlib.pyplot as plt
from sklearn.svm import SVC
from sklearn.cross_validation import StratifiedKFold
from sklearn.feature_selection import RFECV
from sklearn.datasets import make_classification
# Create the RFE object and compute a cross-validated score.
svc = SVC(kernel='linear')
# The "accuracy" scoring is proportional to the number of correct
# classifications
@vaclavcadek
vaclavcadek / vizualize_tree.py
Last active August 29, 2015 14:26
Vizualize sklearn decision tree using graphviz
from sklearn.externals.six import StringIO
from sklearn import tree
from graphviz import Source
from IPython.display import SVG
def vizualize_tree(model, figsize=(14,8)):
out = StringIO()
tree.export_graphviz(model, out_file=out)
graph = Source(out.getvalue())
graph.format = 'svg'
@vaclavcadek
vaclavcadek / df_correlation.py
Created August 4, 2015 13:11
Plot the correlation matrix (Spearman's) of values within the dataframe
import numpy as np
def plot_correlation(dataframe, title='', corr_type=''):
lang_names = dataframe.columns.tolist()
tick_indices = np.arange(0.5, len(lang_names) + 0.5)
plt.figure(figsize=(12,7))
plt.pcolor(dataframe.values, cmap='RdBu', vmin=-1, vmax=1)
colorbar = plt.colorbar()
colorbar.set_label(corr_type)
plt.title(title)