Skip to content

Instantly share code, notes, and snippets.

@amrakm
amrakm / linux_grep_search_keywords.sh
Created April 4, 2022 11:40
find keywords with files using #grep #linux
grep -Hrn "import re" . --include \*.ipynb
@amrakm
amrakm / psycopg2_batch_execute.py
Created April 4, 2022 09:58
psycopg2 batch execute
from psycopg2.extras import execute_batch
def commit(self,vals)->None:
conn = self.conn
cursor = conn.cursor()
sql = """
update "movies"
set imdb_vote_count = %(votes)s,
imdb_rating = %(rating)s,
"updatedAt" = now()
@amrakm
amrakm / fix_jupyter_lab_logs.py
Created April 1, 2022 10:06
fix jupyter notebook not showing
# Create logger
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
# Create STDERR handler
handler = logging.StreamHandler(sys.stderr)
# ch.setLevel(logging.DEBUG)
# Create formatter and add it to the handler
formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
@amrakm
amrakm / tensorflow_multiple_gpus.py
Created March 31, 2022 12:44
multiple #gpu #tensorflow #tensorflow_hub
import tensorflow as tf
strategy = tf.distribute.MirroredStrategy()
print('Number of devices: {}'.format(strategy.num_replicas_in_sync)) # it prints 2 .. correct
with strategy.scope():
embed = hub.load("https://tfhub.dev/google/universal-sentence-encoder-cmlm/multilingual-base/1")
@amrakm
amrakm / zero_shot_classification.py
Created February 18, 2022 17:33
#nlp #classification #zero_shot
import transformers
import torch
from collections import defaultdict
from transformers import pipeline
text_batch = """Natural language processing (NLP) is an area of computer science and artificial intelligence concerned with the interactions between computers and human (natural) languages, in particular how to program computers to process and analyze large amounts of natural language data. It is the branch of machine learning which is about analyzing any text and handling predictive analysis."""
kw_list = ['computer', 'machine learning', 'knowledge', 'language']
@amrakm
amrakm / psycopg2_execute_sql.py
Created February 14, 2022 12:45
#psycopg2 execute #sql from #python
import psycopg2
import logging
def execute_sql(sql, conn_str):
conn = psycopg2.connect(conn_str)
cursor = conn.cursor()
try:
@amrakm
amrakm / melt_pandas_lst_col.py
Created December 1, 2021 15:06
melt pandas column list into multiple rows
#source: https://stackoverflow.com/a/42012264
def melt_list_column(df, lst_col):
return pd.DataFrame({
col:np.repeat(df[col].values, df[lst_col].str.len())
for col in df.columns.difference([lst_col])
}).assign(**{lst_col:np.concatenate(df[lst_col].values)})[df.columns.tolist()]
@amrakm
amrakm / sort_3d_array.py
Created November 22, 2021 16:24
#numpy #matrix sorting 3d matrix
#source: stackoverflow answer, can't find the link anymore
import numpy as np
a = np.array([[[10,2],
[5,3],
[4,4]],
[[7,6],
[4,2],
@amrakm
amrakm / extract_nouns.py
Created November 1, 2021 16:13
extract nouns from text using #nltk #nlp
#source: https://www.maartengrootendorst.com/blog/concept/
import random
import nltk
nltk.download("wordnet")
from nltk.corpus import wordnet as wn
all_nouns = [word for synset in wn.all_synsets('n') for word in synset.lemma_names()
if "_" not in word]
selected_nouns = random.sample(all_nouns, 50_000)
@amrakm
amrakm / create_sparse_matrix.py
Last active October 31, 2021 19:48
create sparse matrix from df
from scipy.sparse import coo_matrix
def create_sparse_matrix(data, iid2cf_idx, uid2cf_idx, item_id_column, user_id_column):
max_item_idx = max(iid2cf_idx.values())
max_user_idx = max(uid2cf_idx.values())