<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 <a href='http
Step1:  Dataset
We will use the famous Iris Data set.
More info on the data set
Step2:  Split the Data into Training and Test
Its time to split the data into a train/test set. Keep in mind, sometimes people like to split 3 ways, train/test/validation. We'll keep things simple for now. Remember to check out the video explanation as to why we split and what all the parameters mean!
Step3:  Standardizing the Data
Usually when using Neural Networks, you will get better performance when you standardize the data. Standardization just means normalizing the values to all fit between a certain range, like 0-1, or -1 to 1.
The scikit learn library also provides a nice function for this.
http
Step4:  Ok, now we have the data scaled!
Step5:  Building the Network with Keras
Let's build a simple neural network!
Step6:  Fit (Train) the Model
Step7:  Predicting New Unseen Data
Let's see how we did by predicting on new data. Remember, our model has never seen the test data that we scaled previously! This process is the exact same process you would use on totally brand new data. For example , a brand new bank note that you just analyzed .
Step8:  Evaluating Model Performance
So how well did we do? How do we actually measure "well". Is 95% accuracy good enough? It all depends on the situation. Also we need to take into account things like recall and precision. Make sure to watch the video discussion on classification evaluation before running this code!
Step9:  Saving and Loading Models
Now that we have a model trained, let's see how we can save and load it.
<ASSISTANT_TASK:>
Python Code:
import numpy as np
Explanation: <a href='http://www.pieriandata.com'> <img src='../Pierian_Data_Logo.png' /></a>
Keras Basics
Welcome to the section on deep learning! We'll be using Keras with a TensorFlow backend to perform our deep learning operations.
This means we should get familiar with some Keras fundamentals and basics!
Imports
End of explanation
from sklearn.datasets import load_iris
iris = load_iris()
type(iris)
print(iris.DESCR)
X = iris.data
X
y = iris.target
y
from keras.utils import to_categorical
y = to_categorical(y)
y.shape
y
Explanation: Dataset
We will use the famous Iris Data set.
More info on the data set:
https://en.wikipedia.org/wiki/Iris_flower_data_set
Reading in the Data Set
We've already downloaded the dataset, its in this folder. So let's open it up.
End of explanation
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
X_train
X_test
y_train
y_test
Explanation: Split the Data into Training and Test
Its time to split the data into a train/test set. Keep in mind, sometimes people like to split 3 ways, train/test/validation. We'll keep things simple for now. Remember to check out the video explanation as to why we split and what all the parameters mean!
End of explanation
from sklearn.preprocessing import MinMaxScaler
scaler_object = MinMaxScaler()
scaler_object.fit(X_train)
scaled_X_train = scaler_object.transform(X_train)
scaled_X_test = scaler_object.transform(X_test)
Explanation: Standardizing the Data
Usually when using Neural Networks, you will get better performance when you standardize the data. Standardization just means normalizing the values to all fit between a certain range, like 0-1, or -1 to 1.
The scikit learn library also provides a nice function for this.
http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MinMaxScaler.html
End of explanation
X_train.max()
scaled_X_train.max()
X_train
scaled_X_train
Explanation: Ok, now we have the data scaled!
End of explanation
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(8, input_dim=4, activation='relu'))
model.add(Dense(8, input_dim=4, activation='relu'))
model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
Explanation: Building the Network with Keras
Let's build a simple neural network!
End of explanation
# Play around with number of epochs as well!
model.fit(scaled_X_train,y_train,epochs=150, verbose=2)
Explanation: Fit (Train) the Model
End of explanation
scaled_X_test
# Spits out probabilities by default.
# model.predict(scaled_X_test)
model.predict_classes(scaled_X_test)
Explanation: Predicting New Unseen Data
Let's see how we did by predicting on new data. Remember, our model has never seen the test data that we scaled previously! This process is the exact same process you would use on totally brand new data. For example , a brand new bank note that you just analyzed .
End of explanation
model.metrics_names
model.evaluate(x=scaled_X_test,y=y_test)
from sklearn.metrics import confusion_matrix,classification_report
predictions = model.predict_classes(scaled_X_test)
predictions
y_test.argmax(axis=1)
confusion_matrix(y_test.argmax(axis=1),predictions)
print(classification_report(y_test.argmax(axis=1),predictions))
Explanation: Evaluating Model Performance
So how well did we do? How do we actually measure "well". Is 95% accuracy good enough? It all depends on the situation. Also we need to take into account things like recall and precision. Make sure to watch the video discussion on classification evaluation before running this code!
End of explanation
model.save('myfirstmodel.h5')
from keras.models import load_model
newmodel = load_model('myfirstmodel.h5')
newmodel.predict_classes(X_test)
Explanation: Saving and Loading Models
Now that we have a model trained, let's see how we can save and load it.
End of explanation
<END_TASK>