Skip to content

Instantly share code, notes, and snippets.

View notha99y's full-sized avatar
:octocat:
loading...

Ren Jie notha99y

:octocat:
loading...
View GitHub Profile
def simple_LSTM():
np.random.seed(7)
model = Sequential(name = 'simple_LSTM')
model.add(LSTM(512, input_shape=(None, 6), recurrent_dropout=0.5))
model.add(Dense(len(activities), activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=1e-4), metrics=['accuracy'])
print(model.summary())
return model
# Importing the Callbacks
from keras.callbacks import EarlyStopping
from keras.callbacks import TensorBoard
from keras.callbacks import ModelCheckpoint
# Saving logs
LOG_DIR = os.path.join(os.getcwd(), 'logs')
tb = TensorBoard(LOG_DIR)
# Saving weights
weights_dir = 'weights/' + model.name + \
'-{epoch:02d}-{loss:.2f}.hdf5'
def categorical_summarized(dataframe, x=None, y=None, hue=None, palette='Set1', verbose=True):
'''
Helper function that gives a quick summary of a given column of categorical data
Arguments
=========
dataframe: pandas dataframe
x: str. horizontal axis to plot the labels of categorical data, y would be the count
y: str. vertical axis to plot the labels of categorical data, x would be the count
hue: str. if you want to compare it another variable (usually the target variable)
# Target Variable: Survival
c_palette = ['tab:blue', 'tab:orange']
categorical_summarized(train_df, y = 'Survived', palette=c_palette)
# Feature Variable: Gender
categorical_summarized(train_df, y = 'Sex', hue='Survived', palette=c_palette)
def quantitative_summarized(dataframe, x=None, y=None, hue=None, palette='Set1', ax=None, verbose=True, swarm=False):
'''
Helper function that gives a quick summary of quantattive data
Arguments
=========
dataframe: pandas dataframe
x: str. horizontal axis to plot the labels of categorical data (usually the target variable)
y: str. vertical axis to plot the quantitative data
hue: str. if you want to compare it another categorical variable (usually the target variable if x is another variable)
# univariate analysis
quantitative_summarized(dataframe= train_df, y = 'Age', palette=c_palette, verbose=False, swarm=True)
# bivariate analysis with target variable
quantitative_summarized(dataframe= train_df, y = 'Age', x = 'Survived', palette=c_palette, verbose=False, swarm=True)
# multivariate analysis with Embarked variable and Pclass variable
quantitative_summarized(dataframe= train_df, y = 'Age', x = 'Embarked', hue = 'Pclass', palette=c_palette3, verbose=False, swarm=False)
from sklearn.ensemble import RandomForestClassifier
rf_clf = RandomForestClassifier(n_estimators = 500, max_depth=12)
rf_clf.fit(X_train, y_train)
rf_y_pred = rf_clf.predict(X_val)
pd.Series(rf_clf.feature_importances_, index = X_train.columns).nlargest(12).plot(kind = 'barh',
figsize = (10, 10),
title = 'Feature importance from RandomForest').invert_yaxis();