Skip to content

Instantly share code, notes, and snippets.

View vikramsoni2's full-sized avatar

Vikram vikramsoni2

View GitHub Profile
@vikramsoni2
vikramsoni2 / tensorflow_plot_graphs.py
Created November 22, 2021 00:58
Tensorflow plot history using matplotlib
import matplotlib.pyplot as plt
def plot_graphs(history, string):
plt.plot(history.history[string])
plt.plot(history.history['val_'+string])
plt.xlabel("Epochs")
plt.ylabel(string)
plt.legend([string, 'val_'+string])
plt.show()
@vikramsoni2
vikramsoni2 / zip_directory.py
Created September 8, 2021 09:34
zip entire directory in python
from zipfile import ZipFile
import os
from os.path import basename
def zip_dir(dirname, zipfilename):
with ZipFile(zipfilename, 'w') as zipObj:
# Iterate over all the files in directory
for folderName, subfolders, filenames in os.walk(dirname):
for filename in filenames:
#create complete filepath of file in directory
@vikramsoni2
vikramsoni2 / parallel_coord.py
Last active March 2, 2022 15:22
parallel coordinates plot using matplotlib
import numpy as np
import pandas as pd
from matplotlib.path import Path
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import seaborn as sns
from typing import Union, List, Tuple
def parallel_coordinates(df: pd.DataFrame,
@vikramsoni2
vikramsoni2 / lightgbm_custom_feval.py
Created July 19, 2021 12:33
custom eval metric function for lightgbm cv. calculating top K accuracy
def top_10_accuracy(preds, train_data, is_higher_better=True):
y_preds = np.asarray(preds).reshape(-1, len(np.unique(y)))
y_true = train_data.get_label()
score = top_k_accuracy_score(y_true, y_preds, k=10)
return ('top_10_accuracy', score, True)
@vikramsoni2
vikramsoni2 / custom_format_logger.py
Created July 10, 2021 22:43
logging formats based on log levels
import logging
from bisect import bisect
from logging import getLogger, Formatter, LogRecord, StreamHandler
from typing import Dict
class LevelFormatter(Formatter):
def __init__(self, formats: Dict[int, str], **kwargs):
super().__init__()
@vikramsoni2
vikramsoni2 / remove_top_level_dir_unzip.py
Created July 10, 2021 02:08
extract zip file containing github repo witout the top level directory
from requests import get
from io import BytesIO
from zipfile import ZipFile
import os
from pathlib import Path
destination = "testproject"
file_url = 'https://github.com/vikramsoni2/aihubcli/archive/refs/heads/main.zip'
@vikramsoni2
vikramsoni2 / plot_keras_history.py
Created June 28, 2021 04:02
plotting train vs valid metrics in line plot
metrics_names = ['loss', 'accuracy', 'top10_accuracy']
plt.figure(figsize=(14,4))
sns.set_style('whitegrid')
for i in range(len(metrics_names)):
ax = plt.subplot(1, len(metrics_names), i+1)
ax.plot(history.history[metrics_names[i]], label="train")
ax.plot(history.history['val_'+ metrics_names[i]], label="valid")
ax.title.set_text(metrics_names[i])
@vikramsoni2
vikramsoni2 / contractions.py
Created June 28, 2021 00:04
contractions english
contractions = {
"ain't": "am not",
"aren't": "are not",
"can't": "cannot",
"can't've": "cannot have",
"'cause": "because",
"could've": "could have",
"couldn't": "could not",
"couldn't've": "could not have",
"didn't": "did not",
@vikramsoni2
vikramsoni2 / remove_highly_correlated_features.py
Created May 12, 2021 13:22
Remove Highly Correlated Features
def remove_highly_correlated_features(df = None, ths=0.99):
print("The data has : {} features ".format(len(list(df))))
correlations = df.corr()
mask = np.triu(np.ones_like(correlations, dtype=bool))
corr_matrix = correlations.abs()
# Create a True/False mask and apply it
mask = np.triu(np.ones_like(corr_matrix, dtype=bool))
tri_df = corr_matrix.mask(mask)
# List column names of highly correlated features (r >0.98 )
to_drop = [c for c in tri_df.columns if any(tri_df[c] > ths)]
@vikramsoni2
vikramsoni2 / confusion_matrix.py
Created March 25, 2021 03:05
Multiclass confusion matrix using Seaborn heatmap
y_true = le.inverse_transform(y_valid)
y_pred = le.inverse_transform(y_valid_pred_lr)
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
data = confusion_matrix(y_true, y_pred)