Skip to content

Instantly share code, notes, and snippets.

View john-adeojo's full-sized avatar

John Adeojo john-adeojo

View GitHub Profile
@john-adeojo
john-adeojo / gist:cdc4c8d2871061d24cd9166e405bc357
Created January 16, 2021 17:47
Plot histogram for saleprice
import seaborn as sns
import matplotlib
import matplotlib.pyplot as plt
x = Train.SalePrice
sns.set_style('whitegrid')
sns.distplot(x)
plt.show()
Train['SalePrice_log'] = np.log(Train.SalePrice)
# Lets explore the correlations in our data set
plt.figure(figsize=(20,20))
sns.heatmap(Train.corr())
# Model 1: Ranbdom Forest Rgressor
import sklearn
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestRegressor
# Split training data into features (x_train) and labels (Y_train)
x_train = Train_encoded.drop(columns=['Id','SalePrice','SalePrice_log'])
Y_train = Train_encoded.SalePrice_log
@john-adeojo
john-adeojo / gist:2da7e895553b6b8454f113a2d7a8169b
Created January 22, 2021 00:21
Build Sequential Neural Network Keras
# Build sequential neural network
model = keras.models.Sequential() # Keras sequential model is composed of a single stack of layers connected sequentially
model.add(keras.layers.Input(shape=(784))) # Input layer is a 1D array (no parameters, first layer so specify input shape, not including batch size just size of instances)
model.add(keras.layers.Dense(300, activation="relu")) # Dense hidden layer with 300 neurons and ReLU activation fucntion ( each dense layer manages it's own weight matrix and bias vector)
model.add(keras.layers.Dense(100, activation="relu")) # Second Dense hidden layer of 100 neurons
model.add(keras.layers.Dense(10, activation="softmax")) # Final layer is a dense output layer of 10 neurons with softmax activation function (there are 10 exxclusive classes)
# Displays all the model's layers
model.summary()
# Compile the model 1: sequential NN
model.compile(loss="sparse_categorical_crossentropy", # We have sparse labels: each instance there is a target class from 0 to 9 and classes are exclusive
optimizer="sgd", # model trained useing stochastic gradient descent
metrics=["accuracy"])
@john-adeojo
john-adeojo / NN Sequential
Created January 22, 2021 00:30
NN Sequential
# Compile the model 1: sequential NN
model.compile(loss="sparse_categorical_crossentropy", # We have sparse labels: each instance there is a target class from 0 to 9 and classes are exclusive
optimizer="sgd", # model trained useing stochastic gradient descent
metrics=["accuracy"])
@john-adeojo
john-adeojo / Fitting Sequential NN
Created January 22, 2021 00:32
Fitting Sequential NN
# Fit model1 : sequential NN
history = model.fit(X_train, y_train, epochs=30, # Set the number of epochs else Keras defaults to 1 epoch
validation_data=(X_valid, y_valid)) # Passing the validation sets enables Keras to measure the loss and accuracy metrics on the validation set
@john-adeojo
john-adeojo / Plot sequential NN
Created January 22, 2021 00:34
Plot sequential NN
# Plot the training history for the network
def plot_performance (history, save):
pd.DataFrame(history.history).plot(figsize=(8, 5))
plt.grid(True)
plt.gca().set_ylim(0, 1) # Sets the verticle axis range from 0 to 1
plt.show()
plt.savefig(save)
plot_performance (history, 'model1')
@john-adeojo
john-adeojo / Deep and Wide NN Build
Created January 22, 2021 00:42
Deep and Wide NN Build
# Build a neural network with deep and wide architecture
input_ = keras.layers.Input(shape=(784)) # Input layer
hidden1 = keras.layers.Dense(300, activation="relu")(input_) # Create first hidden layer and feed input layer to it
hidden2 = keras.layers.Dense(100, activation="relu")(hidden1) # Create 2nd hidden layer and feed output of first hidden layer
concat = keras.layers.Concatenate()([input_, hidden2]) # Concatenate output from 2nd hidden layer and input layer
output = keras.layers.Dense(10, activation="softmax")(concat) # Feed concatenation of first and second input layer to output layer creating wide and deep network
model2 = keras.Model(inputs=[input_], outputs=[output])
@john-adeojo
john-adeojo / Read CSV from GitHun
Created February 1, 2021 13:04
Reads CSV from GitHub
# THIS FUNCTION TAKES A CSV FILE FROM A GITHUB URL AND READS IT INTO A PANDAS DATA FRAME
import pandas as pd
def read_file(url):
"""
Takes GitHub url as an argument,
pulls CSV file located @ github URL.
"""