This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node: | |
def __init__(self, data): | |
self.data = data | |
self.next = None | |
def __next__(self): | |
return self.next | |
class LinkedList: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import io | |
sys.stdin = io.StringIO(""" | |
3 3 | |
0 1 1 1 | |
1 2 2 4 | |
2 0 1 2 | |
""".strip()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for bits in product([0, 1], repeat=4): | |
print("".join(str(b) for b in bits) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |