Skip to content

Instantly share code, notes, and snippets.

View netsatsawat's full-sized avatar

Satsawat Natakarnkitkul (Net) netsatsawat

View GitHub Profile
@netsatsawat
netsatsawat / hmm_gaussianhmm_model.py
Created September 21, 2020 14:38
Python script to implement GaussianHMM from HMMLearn to model the hidden states
_df = np.column_stack([train_df[FT_COLS].values])
hmm_model = GaussianHMM(n_components=3, covariance_type="full",
n_iter=1000, random_state=SEED).fit(_df)
hidden_states = hmm_model.predict(_df)
print("Means and vars of each hidden state")
for i in range(hmm_model.n_components):
print(f'{i}th hidden state')
print('mean: ', (hmm_model.means_[i]))
print('var: ', np.diag(hmm_model.covars_[i]))
@netsatsawat
netsatsawat / hmm_gaussian_mixture_price_plot.py
Created September 21, 2020 13:03
Python script to map the hidden state to the GE price and plot
states = (pd.DataFrame(hidden_states, columns=['states'], index=train_df.index)
.join(train_df, how='inner')
.assign(mkt_cret=train_df.sret.cumsum())
.reset_index(drop=False)
.rename(columns={'index':'Date'}))
sns.set(font_scale=1.5)
sns.set_style('white', style_kwds)
order = np.arange(model.n_components)
fg = sns.FacetGrid(data=states, hue='states', hue_order=order,
@netsatsawat
netsatsawat / hmm_gaussian_mixture_model.py
Created September 21, 2020 12:44
Python script demonstrates the implementation of Gaussian Mixture model
train_df = data_df.loc[: '2019-01-01'].dropna()
test_df = data_df.loc['2019-01-01': ].dropna()
X_train = train_df[FT_COLS].values
X_test = test_df[FT_COLS].values
model = mix.GaussianMixture(n_components=N_COMPONENTS,
covariance_type="full",
n_init=100,
random_state=SEED).fit(X_train)
@netsatsawat
netsatsawat / hmm_data_prep.py
Created September 21, 2020 11:59
Python script to generate the stock time series data specifically for Hidden Markov Model example
import numpy as np
import pandas as pd
import pandas_datareader.data as web
import scipy.stats as scs
import matplotlib as mpl
from matplotlib import cm
import matplotlib.pyplot as plt
from matplotlib.dates import YearLocator, MonthLocator
import seaborn as sns
@netsatsawat
netsatsawat / cnn_with_data_augment.py
Created August 24, 2020 11:53
Snippet of CNN model with data augmentation implementation
train_gen = tf.keras.preprocessing.image.ImageDataGenerator(rotation_range=40, shear_range=0.2, zoom_range=0.2,
horizontal_flip=True, vertical_flip=True, rescale=1./255.,
validation_split=0.2)
train_generator = train_gen.flow_from_directory(TRAIN_DIR, target_size=IMG_SIZE, batch_size=32,
class_mode='categorical', subset='training')
valid_generator = train_gen.flow_from_directory(TRAIN_DIR, target_size=IMG_SIZE, batch_size=32,
class_mode='categorical', subset='validation')
cnn_model2 = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)),
@netsatsawat
netsatsawat / cnn_basic_example.py
Created August 20, 2020 11:36
Snippet code for basic CNN implementation using tensorflow
import tensorflow as tf
import tensorflow_docs as tfdocs
cnn_model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=train_imgs.shape[1: 4]),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
@netsatsawat
netsatsawat / lstm_example.py
Created August 17, 2020 14:46
Snippet example of LSTM implementation using tensorflow
lstm_model = tf.keras.Sequential([
layers.LSTM(32, return_sequences=True, input_shape=(n_steps, 1)),
layers.LSTM(32, return_sequences=True),
layers.Dropout(0.2),
layers.LSTM(32, return_sequences=True),
layers.LSTM(32),
layers.Dropout(0.2),
layers.Dense(1)
])
@netsatsawat
netsatsawat / gru_example.py
Created August 17, 2020 13:53
Snippet of RNN - GRU using Tensorflow
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
# require for installation: !pip install -q git+https://github.com/tensorflow/docs
import tensorflow_docs as tfdocs
import tensorflow_docs.plots
import tensorflow_docs.modeling
@netsatsawat
netsatsawat / feed_forward_neural_network.py
Last active August 6, 2020 15:47
Snippet code for feed forward neural network
import pandas as pd
import numpy as np
import random
from sklearn.datasets import make_regression
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
# require for installation: !pip install -q git+https://github.com/tensorflow/docs
@netsatsawat
netsatsawat / minibatch_sgd_sklearn.py
Created August 5, 2020 10:13
Snippet of mini-batch using SGDRegressor in sklearn
def _get_chunk(X, y, chunkrows):
X_chunk, y_chunk = X[chunkrows], y[chunkrows]
return X_chunk, y_chunk
def _iter_minibatch(X, y, chunk_size):
'''
Construct minibatch generator
'''
_start = 0