Skip to content

Instantly share code, notes, and snippets.

View ImadDabbura's full-sized avatar
🎯
Focusing

Imad Dabbura ImadDabbura

🎯
Focusing
View GitHub Profile
def clip_gradients(gradients, max_value):
"""
Implements gradient clipping element-wise on gradients to be
between the interval [-max_value, max_value].
"""
for grad in gradients.keys():
np.clip(gradients[grad], -max_value, max_value, out=gradients[grad])
return gradients
def rnn_forward(x, y, h_prev, parameters):
"""Implement one Forward pass on one name."""
# Retrieve parameters
Wxh, Whh, b = parameters["Wxh"], parameters["Whh"], parameters["b"]
Why, c = parameters["Why"], parameters["c"]
# Initialize inputs, hidden state, output, and probabilities dictionaries
xs, hs, os, probs = {}, {}, {}, {}
# Plot confusion matrix
second_stack_probs = second_stack.predict_proba(first_stack.transform(X_test))
second_stack_preds = second_stack.predict(first_stack.transform(X_test))
conf_mat = confusion_matrix(y_test, second_stack_preds)
plt.figure(figsize=(16, 8))
plt.matshow(conf_mat, cmap=plt.cm.Reds, alpha=0.2)
for i in range(2):
for j in range(2):
plt.text(x=j, y=i, s=conf_mat[i, j], ha="center", va="center")
# Impute the missing data using features means
imp = Imputer()
imp.fit(X_train)
X_train = imp.transform(X_train)
X_test = imp.transform(X_test)
# Standardize the data
std = RobustScaler()
std.fit(X_train)
X_train = std.transform(X_train)
# Plot partial dependence plots
gbrt = GradientBoostingClassifier(loss="deviance",
learning_rate=0.1,
n_estimators=100,
max_depth=3,
random_state=123)
gbrt.fit(X_res, y_res)
fig, axes = plot_partial_dependence(gbrt,
X_res,
np.argsort(gbrt.feature_importances_)[::-1][:8],
# Build random forest classifier (same config)
rf_clf = RandomForestClassifier(n_estimators=500,
max_features=0.25,
criterion="entropy",
class_weight="balanced")
# Build model with no sampling
pip_orig = make_pipeline(Imputer(strategy="mean"),
RobustScaler(),
rf_clf)
# fit RF to plot feature importances
rf_clf.fit(RobustScaler().fit_transform(
Imputer(strategy="median").fit_transform(X_train)), y_train)
# Plot features importance
importances = rf_clf.feature_importances_
indices = np.argsort(rf_clf.feature_importances_)[::-1]
plt.figure(figsize=(12, 6))
plt.bar(range(1, 25), importances[indices], align="center")
plt.xticks(range(1, 25),
# Create binary features to check if the example is has missing values for all features that have missing values
for feature in df.columns:
if np.any(np.isnan(df[feature])):
df["is_" + feature + "_missing"] = np.isnan(df[feature]) * 1
# Original Data
X = df.loc[:, df.columns != "not_fully_paid"].values
y = df.loc[:, df.columns == "not_fully_paid"].values.flatten()
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, shuffle=True, random_state=123, stratify=y)
# Get number of positve and negative examples
pos = df[df["not_fully_paid"] == 1].shape[0]
neg = df[df["not_fully_paid"] == 0].shape[0]
print(f"Positive examples = {pos}")
print(f"Negative examples = {neg}")
print(f"Proportion of positive to negative examples = {(pos / neg) * 100:.2f}%")
plt.figure(figsize=(8, 6))
sns.countplot(df["not_fully_paid"])
plt.xticks((0, 1), ["Paid fully", "Not paid fully"])
plt.xlabel("")
# Load the data
df = pd.read_csv("../data/loans.csv")
# Check both the datatypes and if there is missing values print(f"Data types:\n{11 * '-'}")
print(f"{df.dtypes}\n")
print(f"Sum of null values in each feature:\n{35 * '-'}")
print(f"{df.isnull().sum()}")
df.head()