Skip to content

Instantly share code, notes, and snippets.

View sksoumik's full-sized avatar

Sadman Kabir Soumik sksoumik

View GitHub Profile
@sksoumik
sksoumik / primary_imports_for_colab_notebooks.py
Created September 9, 2020 14:53
Basic required library import for colab notebooks | common imports
# mount gdrive
from google.colab import drive
drive.mount('/content/drive')
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import warnings
warnings.filterwarnings("ignore")
@sksoumik
sksoumik / device_print.py
Last active September 9, 2020 13:01
Check if GPU is availabel
import tensorflow as tf
import torch
device_name = tf.test.gpu_device_name()
if device_name != '/device:GPU:0':
raise SystemError('GPU device not found')
print('Found GPU at: {}'.format(device_name))
>>> torch.cuda.get_device_name(0)
@sksoumik
sksoumik / sentence_length.py
Created September 9, 2020 12:59
Avearage sentence length of a sentence
print('average sentence length: ', df.Text.str.split().str.len().mean())
print('stdev sentence length: ', df.Text.str.split().str.len().std())
@sksoumik
sksoumik / display_all_images_in_a_python_list.py
Last active September 5, 2020 12:26
Display all images using matplotlib from a python list
import cv2 as cv
import glob
import matplotlib.pyplot as plt
path = "static/subfolder/*/*.jpg"
my_image_list = []
for file in glob.glob(path):
file = cv.imread(file) # BGR
@sksoumik
sksoumik / gpu_growth.py
Created August 20, 2020 12:42
tensorflow gpu InteractiveSession, ResourceExhaustedError
gpu_options = tf.compat.v1.GPUOptions(allow_growth=True)
session = tf.compat.v1.InteractiveSession(
config=tf.compat.v1.ConfigProto(gpu_options=gpu_options)
)
@sksoumik
sksoumik / plot_target_column_distribution.py
Created August 17, 2020 06:39
Plot value distribution
data['target_column'].value_counts().plot.bar();
def sampling_k_elements(group, k=3):
if len(group) < k:
return group
return group.sample(k)
balanced = df.groupby('class').apply(sampling_k_elements).reset_index(drop=True)
def sampling_k_elements(group, k=3):
if len(group) < k:
return group
return group.sample(k)
balanced = df.groupby('class').apply(sampling_k_elements).reset_index(drop=True)
@sksoumik
sksoumik / print_output_save_in_file.py
Created August 12, 2020 22:52
Save the print output in a file python
import sys
# put your output data container
print(report)
original_stdout = sys.stdout
with open("classification_report.txt", "w") as f:
sys.stdout = f
# put your output data container again
@sksoumik
sksoumik / find_and_remove_null_values.py
Created August 5, 2020 14:57
See if there is any null values and remove in pandas
# check if there is any null values
df.isnull().sum()
# remove null values in aany rows
df = df.dropna(how='any',axis=0)
# check again if there is any null values
df.isnull().sum()