Skip to content

Instantly share code, notes, and snippets.

View rohan-paul's full-sized avatar
🎯
Focusing

Rohan Paul rohan-paul

🎯
Focusing
View GitHub Profile
model_keras_seq = Sequential()
model_keras_seq.add(Conv1D(64, input_shape=(3, 4096), kernel_size=3, activation='relu'))
model_keras_seq.add(BatchNormalization())
model_keras_seq.add(Flatten())
model_keras_seq.add(Dense(64, activation='relu'))
model_keras_seq.add(Dense(1, activation='sigmoid'))
model_keras_seq.compile(optimizer= Adam(lr=2e-4), loss='binary_crossentropy', metrics=['acc'])
model_keras_seq.summary()
""" First, we define the constructor to initialize the configuration of the generator.
Note that here, we assume the path to the data is in a dataframe column.
"""
class DataGenerator(Sequence):
# For this dataset the list_IDs are the value of the ids
# for each of the time-series file
# i.e. for Train data => values of column 'id' from training_labels.csv
# Defining a multi-plot function
# And I am going to call the 3 series as Series-1, Series-2 and Series-3
def multi_plot(series, plot_type, target):
if plot_type == 'box' or plot_type == 'kde':
plt.figure(figsize=(20, 2))
else:
plt.figure(figsize=(15,12))
for idx in range(3):
if plot_type == 'box':
# The gwpy's TimeSeries function expects array-like input data array as its first argument
# and sample_rate : float, Quantity, optional the rate of samples per second (Hertz)
def get_tseries_from_file(file_name):
t_data = np.load(file_name)
tseries1 = TimeSeries(t_data[0,:], sample_rate=2048)
tseries2 = TimeSeries(t_data[1,:], sample_rate=2048)
tseries3 = TimeSeries(t_data[2,:], sample_rate=2048)
return tseries1, tseries2, tseries3
''' Multi-data plots with gwpy.plot
import pandas as pd
import seaborn as sns
from scipy import signal
from gwpy.timeseries import TimeSeries
from gwpy.plot import Plot
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from PIL import Image
from glob import glob
from matplotlib import pyplot as plt
def get_cqt_spectrogram(
waves_from_each_file,
transform=CQT1992v2(sr=2048, fmin=20, fmax=1024, hop_length=64),
):
stacked_waves_from_each_file = np.hstack(waves_from_each_file)
stacked_waves_from_each_file = stacked_waves_from_each_file / np.max(
stacked_waves_from_each_file
)
stacked_waves_from_each_file = torch.from_numpy(stacked_waves_from_each_file).float()
cqt_image = transform(stacked_waves_from_each_file)
import pandas as pd
import seaborn as sns
import numpy as np
from glob import glob
from matplotlib import pyplot as plt
from colorama import Fore, Back, Style
plt.style.use('ggplot')
import warnings
warnings.filterwarnings('ignore')
import torch
def predict_y_given_x_bootstrap(x_query):
y_predicted_array_30_sample = []
for i in range(0, 30):
model_i = list_of_all_models_decision_tree[i]
# Extract x for ith data point with specific number of featues from list_selected_columns
x_data_point_i = [x_query[column] for column in list_selected_columns[i]]
x_data_point_i = np.array(x_data_point_i).reshape(1, -1)
y_predicted_i = model_i.predict(x_data_point_i)
y_predicted_array_30_sample.append(y_predicted_i)
# Function to build the entire bootstrapping steps that we did above and
# Reurning from the function the MSE and oob score
def bootstrapping_and_oob(x, y):
# Use generating_samples function to create 30 samples
# store these created samples in a list
list_input_data =[]
list_output_data =[]
list_selected_row= []
list_selected_columns=[]
# First noting that our earlier definded variable list_selected_row and list_selected_columns
# which has the list of selected rows and columns
# e.g. list_selected_row is a 2D array of the form [[], [], []...] each inner-array represnting selected row numbers
# for a specific sample. and len(list_selected_row) is 30 reprsenting the 30 samples we have selected for bootstrapping
# print("list_selected_row[10] ", list_selected_row[10])
# print(list_selected_columns)
y_predicted_oob_median_list = []
for i in range(0, 506):