Skip to content

Instantly share code, notes, and snippets.

View simonespa's full-sized avatar

Simone Spaccarotella simonespa

View GitHub Profile
@simonespa
simonespa / distribute.py
Created June 11, 2024 08:14
TensorfFlow Distribute Strategy
# https://www.tensorflow.org/tutorials/distribute/keras
logical_device_names = [logical_device.name for logical_device in tf.config.list_logical_devices()]
if 'GPU' in ''.join(logical_device_names):
distribution_strategy = tf.distribute.MirroredStrategy()
elif 'TPU' in ''.join(logical_device_names):
tf.tpu.experimental.initialize_tpu_system()
tpu = tf.distribute.cluster_resolver.TPUClusterResolver(tpu='/device:TPU_SYSTEM:0')
distribution_strategy = tf.distribute.experimental.TPUStrategy(tpu)
@simonespa
simonespa / conda.sh
Last active April 16, 2025 16:58
Conda
## To setup a project
# Create an empty environment specifying the Python version
conda create --name ml python=3.12
# Activate the environment
conda activate ml
# Add a new channel
conda config --env --add channels conda-forge
# Install a list of packages
conda install ...
@simonespa
simonespa / neuralnet.py
Created May 17, 2024 09:23
Simple Feed-Forward Fully Connected Neural Network
import tensorflow as tf
from tensorflow import keras
nn = keras.Sequential(name='CovidClassification')
nn.add(keras.Input(shape=(X_train.shape[1],)))
nn.add(keras.layers.Dense(
units=4,
activation='leaky_relu',
kernel_regularizer=keras.regularizers.L2(),
bias_regularizer=keras.regularizers.L2(),
from category_encoders import HashingEncoder
N = unique[column]
encoder = HashingEncoder(
cols=[column],
n_components=math.ceil(math.log2(N)), # the number of bits required to encode N elements
hash_method='sha256' # https://docs.python.org/3/library/hashlib.html#constructors
)
df = encoder.fit_transform(df)
@simonespa
simonespa / one_hot_encoding.py
Last active October 6, 2025 13:15
Pandas and HashingEncoder
# Splits the values and expands them in multiple numbered columns
temp_df = df[column].str.split("|", expand=True).fillna('')
# One-Hot encodes all the values for each column
temp_df = pd.get_dummies(temp_df).astype('uint8')
# Removes the "N_" prefixe for each column to expose duplicates
temp_df = remove_prefixes(temp_df)
# Merges the duplicate columns
@simonespa
simonespa / ffmpeg.sh
Created March 2, 2024 21:07
Compress JPEG with ffmpeg
ffmpeg -hide_banner -i image.jpeg -compression_level 100 out/image.jpeg
@simonespa
simonespa / merge_columns.py
Created January 24, 2024 15:25
One-Hot Encoding
def merge_columns(df: pd.DataFrame) -> pd.DataFrame:
unique_columns = df.columns.unique()
total_n_columns = len(df.columns)
unique_n_columns = len(unique_columns)
print(f"There are {total_n_columns} columns, {unique_n_columns} are unique")
# if there are duplicates at all
if total_n_columns != unique_n_columns:
# get each unique colummn name
@simonespa
simonespa / dask_snippet.py
Created December 13, 2023 15:15
Dask Snippet
##
# This method is quite slow for a large number of files (GBs)
# Just wanted to save some parts of the entire snippet for reference
# of some useful stuff like picking a row from a Dask dataframe
# and appending it to a Pandas Dataframe.
for id in unique_ids:
# select all the rows across the partitions for the specified ID
subset = catalogue[catalogue.id == id]
# select only the one row with the latest date and time (max timestamp)
@simonespa
simonespa / media.html
Created November 26, 2023 15:58
Some old web stuff
<html>
<head>
<script type="text/javascript">
// uniformo la chiamata a getUserMedia rispetto ai vari prefissi sperimentali
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
// uniformo la chiamata a window.URL sempre rispetto ai prefissi sperimentali
window.URL = window.URL || window.webkitURL;
// invoco la richiesta di accedere sia al flusso video che quello audio, la funzione
@simonespa
simonespa / monkey.dl
Created November 26, 2023 15:51
Monkey & Banana in DataLog
object(box). object(monkey). object(banana).