This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Lets explore the correlations in our data set | |
plt.figure(figsize=(20,20)) | |
sns.heatmap(Train.corr()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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"]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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"]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. | |
""" |
OlderNewer