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 functools import partial | |
| def pipe(data, *funcs): | |
| for f in funcs: | |
| data = f(data) | |
| return data | |
| pipe( |
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 pandas as pd | |
| from sklearn.model_selection import KFold, GridSearchCV | |
| from sklearn.neighbors import KNeighborsClassifier | |
| from sklearn.preprocessing import StandardScaler | |
| # Get data | |
| df = pd.read_csv("data/diamonds.csv") | |
| df = df[df.cut.isin(["Ideal", "Good"])] | |
| X = df.select_dtypes(["int64", "float64"]) | |
| y = df.cut |
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 functools import reduce | |
| from pathlib import Path | |
| import pandas as pd | |
| def process_weather_data(filename): | |
| df = pd.read_csv(filename).dropna() | |
| first, *others = df.columns.to_list() | |
| return df.melt(id_vars=first, |
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 math import log10, log2, e, log | |
| x = 3.0 | |
| # Log base 10 | |
| one_k = 10**x # 1000.0 | |
| log10(one_k) # 3.0 | |
| # Log base 2 | |
| eight = 2**x # 8.0 |
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 | |
| r = list(range(-2, 3)) # [-2, -1, 0, 1, 2] | |
| # Log base 10 | |
| tens = [10**i for i in r] # [0.01, 0.1, 1, 10, 100] | |
| np.log10(tens).astype(int).tolist() # [-2, -1, 0, 1, 2] | |
| # Log base 2 | |
| twos = [2**i for i in r] # [0.25, 0.5, 1, 2, 4] |
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 get_singles(arr): | |
| while arr.size: | |
| first, arr = arr[0], arr[1:] | |
| if first in arr: | |
| arr = arr[arr != first] | |
| else: | |
| yield first |
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 get_singles(a_list): | |
| while a_list: | |
| x = a_list.pop(0) | |
| if x in a_list: | |
| a_list.remove(x) | |
| else: | |
| yield x | |
| list(get_singles([0, 1, 1, 2, 3])) |
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 this import s as zen | |
| from codecs import encode | |
| from functools import partial | |
| def get_alpha(string): | |
| return (char.lower() if char.isalpha() else " " for char in string) | |
| def join_then_split(iterable): |
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 fib(n): | |
| a, b = 0, 1 | |
| for _ in range(n): | |
| yield a | |
| a, b = b, a + b | |
| def fizzbuzz(iterable): | |
| return ( | |
| "fizz" * (i % 3 == 0) |