Skip to content

Instantly share code, notes, and snippets.

View Mehdi-Amine's full-sized avatar
💭
Having fun with Neural Networks

Mehdi Mehdi-Amine

💭
Having fun with Neural Networks
View GitHub Profile
@Mehdi-Amine
Mehdi-Amine / three_models.py
Created November 30, 2019 04:05
generating three models, training them on the make_moons dataset, then evaluating their accuracy
# Creating three models with three different algorithms
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
tree_clf = DecisionTreeClassifier(random_state=42)
log_clf = LogisticRegression(solver="lbfgs", random_state=42)
svm_clf = SVC(gamma="scale", random_state=42)
# Training, predicting, then evaluating the predictions
@Mehdi-Amine
Mehdi-Amine / ensemble_hard_voting.py
Last active November 30, 2019 04:36
Creating, training, predicting, and evaluating an ensemble
# Combining the three models into an ensemble
from sklearn.ensemble import VotingClassifier
# The ensemble is a voting classifier that aggregates our three models
voting_clf = VotingClassifier(estimators=[('svm', svm_clf), ('tree', tree_clf), ('log', log_clf)],
voting='hard')
voting_clf.fit(X_train, y_train) # training
y_pred_voting = voting_clf.predict(X_test) # predicting
accuracy_score(y_test, y_pred_voting) # evaluating
@Mehdi-Amine
Mehdi-Amine / soft_voting.py
Created November 30, 2019 23:19
An ensemble classifier that relies on soft voting.
# Out of the three models,
# only SVC requires some tweaking to output its confidence
# this is done by setting probability=True:
svm_clf_tweaked = SVC(gamma='scale', probability=True, random_state=42)
soft_voting_clf = VotingClassifier(estimators=[('svm', svm_clf_tweaked), ('tree', tree_clf), ('log', log_clf)],
voting='soft')
soft_voting_clf.fit(X_train, y_train) # training
y_pred_voting = soft_voting_clf.predict(X_test) # predicting
@Mehdi-Amine
Mehdi-Amine / data_split.py
Created November 30, 2019 23:22
Splitting data into training and testing sets
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
@Mehdi-Amine
Mehdi-Amine / bagging.py
Created December 1, 2019 00:06
A bagging classifier.
# Bagging creates several models that rely on the same algorithm.
# The training of each model uses a different subset of data sampled randomly from the training set.
# By default Bagging uses soft voting when its base estimator can provide its measure of confidence,
# Hence the SVC model is set to have probability=True
from sklearn.svm import SVC
from sklearn.ensemble import BaggingClassifier
bagging_clf = BaggingClassifier(SVC(gamma='scale', probability=True, random_state=42),
bootstrap=True, # set to False to use Pasting instead of Bagging
n_estimators=100, # number of SVC models to create
@Mehdi-Amine
Mehdi-Amine / sgdreg-sklearn.py
Created April 21, 2020 13:23
Sklearn sgdregressor
from sklearn.linear_model import SGDRegressor
sgd_reg = SGDRegressor(max_iter=100, tol=None, penalty=None, eta0=0.01, learning_rate='constant')
sgd_reg.fit(dx_train, dy_train)
print(f"Optimized Parameters: \nBias: {round(sgd_reg.intercept_[0], 2)}, Weights: {sgd_reg.coef_}")
'''
Out:
Optimized Parameters:
Bias: 0.0, Weights: [1. 0.5]
'''
from sklearn.preprocessing import MinMaxScaler
norm_scaler = MinMaxScaler()
dx_train_normalized_sklearn = norm_scaler.fit_transform(dx_train)
# Printing the first 5 rows
print(f"Training data normalized using Scikit-Learn: \n{dx_train_normalized_sklearn[:5]}")
'''
Out:
Training data normalized using Scikit-Learn:
from sklearn.preprocessing import StandardScaler
std_scaler = StandardScaler()
dx_train_standardized_sklearn = std_scaler.fit_transform(dx_train)
# Printing the first 5 rows
print(f"Training data standardized using Scikit-Learn: \n{dx_train_standardized_sklearn[:5]}")
'''
Out:
@Mehdi-Amine
Mehdi-Amine / plot-sgds.py
Created April 21, 2020 16:35
investigating the effect of feature scaling on SGD
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter3d(x = params[:, 0], y = params[:, 1], z = params[:, 2],
mode='lines',
name='No Scaling',
line=dict(color='green', width=2)))
fig.add_trace(go.Scatter3d(x = params_std[:, 0], y = params_std[:, 1], z = params_std[:, 2],
mode='lines',
name='Standardization',
@Mehdi-Amine
Mehdi-Amine / random-params.py
Last active April 22, 2020 12:27
Randomly generating the parameters of a perceptron
import numpy as np
bias = np.random.randn(1, 1)
weights = np.random.randn(input_length, 1) # for some integer input_length