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 fix_span(text: str, span: dict): | |
| # let us check that spans are correctly extracted | |
| fixed_span = span.copy() | |
| # span starts with a space or a punctuation | |
| while text[fixed_span["start"]] in [" ", ".", ",", ";", ":", "!", "?"]: | |
| fixed_span["start"] += 1 | |
| # span is cut in the begging: e.g. "ashington DC" |
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
| mkdir -p ~/.ssh | |
| ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts | |
| ssh-keygen -t rsa -C <email> | |
| cat ~/.ssh/id_rsa.pub |
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 statsmodels.api as sm | |
| X = np.array([[1, 85, 5], | |
| [1, 177, 6], | |
| [1, 100, 9], | |
| [1, 110, 8], | |
| [1, 90, 7.5], | |
| [1, 144, 5.5]]) |
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
| !sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1 | |
| !sudo update-alternatives --config python3 | |
| !sudo apt install python3-pip | |
| !python3 --version |
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 | |
| df = pd.DataFrame({'name': ['Store A', 'Store B', 'Store C', 'Store D'] | |
| 'district': ['I', "II", "I", "III"], | |
| "category": [X, X, Y, Z] | |
| } | |
| ) | |
| district_category_pivot_table = df.pivot_table('name', 'district', 'category', aggfunc='count') |
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 | |
| df = ... | |
| pd.options.display.float_format = '{:,.1f}%'.format | |
| print((df.isna().sum()/df.shape[0]).sort_values(ascending=False)*100) | |
| pd.options.display.float_format = '{:,.2f}'.format |
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 wordcloud import WordCloud | |
| def create_wordcloud_from_column(df, column_name, title, max_token_len=3): | |
| preprocessed_tokens = [] | |
| for tokens in df[column_name].str.split().tolist(): | |
| try: | |
| for token in tokens: | |
| if len(token) > max_token_len: | |
| preprocessed_tokens.append(token.lower().strip().strip('-').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
| import torch | |
| import torch.nn as nn | |
| import torch.optim as optim | |
| from torch.utils.data import DataLoader | |
| from torchvision.datasets import MNIST | |
| from torchvision.transforms import ToTensor | |
| from tqdm import tqdm | |
| # Set device (GPU or CPU) | |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
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 pathlib import Path | |
| DIR = Path.cwd() # work directory | |
| PATH_TEST_DIR = Path(DIR, 'data') | |
| PATH_TEST_FILE = Path( DIR, 'file.csv') |
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 PIL import Image | |
| import requests | |
| from io import BytesIO | |
| url = 'https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png' | |
| response = requests.get(url) | |
| img = Image.open(BytesIO(response.content)) |