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
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
%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 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
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
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
from concurrent import futures | |
import time | |
def long_running_task(result): | |
print('Task is running!') | |
time.sleep(1) | |
return result | |
start = time.perf_counter() |
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 matplotlib.animation import FuncAnimation | |
import matplotlib.pyplot as plt | |
import numpy as np | |
fig, ax = plt.subplots(figsize=(5, 8)) | |
def update(i): | |
im_normed = np.random.random((64, 64)) | |
ax.imshow(im_normed) |
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
def logger(func): | |
def wrapped(*args, **kwargs): | |
print('Calling {} with args = {} and kwargs = {}'.format(func, args, kwargs)) | |
return func(*args, **kwargs) | |
return wrapped | |
@logger | |
def factorial(n): | |
if n == 1: | |
return 1 |
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
permutation = list(np.random.permutation(m)) | |
shuffled_X = X[:, permutation] | |
shuffled_Y = Y[:, permutation].reshape((1,m)) |