<jupyter_start><jupyter_text>Welcome to A Gentle Introduction to Keras  This course focuses on a specific sub-field of machine learning called **predictive modeling.**Within predicitve modeling is a speciality or another sub-field called **deep learning.**We will be crafting deep learning models with a library called Keras. >**Predictive modeling** is focused on developing models that make accurate predictions at the expense of explaining why predictions are made. You and I don't need to be able to write a binary classification model. We need to know how to use and interpret the results of the model.  **Where does machine learning fit into data science?**Data science is a much broader discipline. > Data Scientists take the raw data, analyse it, connect the dots and tell a story often via several visualizations. They usually have a broader range of skill-set and may not have too much depth into more than one or two. They are more on the creative side. Like an Artist. An Engineer, on the other hand, is someone who looks at the data as something they have to take in and churn out an output in some appropriate form in the most efficient way possible. The implementation details and other efficiency hacks are usually on the tip of their fingers. There can be a lot of overlap between the two but it is more like A Data Scientist is a Machine Learning Engineer but not the other way round. -- <NAME>, Data Scientist  Step 1. Import our modulesTwo important points here. Firstly, the **from** means we aren't importing the entire library, only a specific module. Secondly, notice we **are** imporing the entire numpy library. > If you get a message that states: WARNING (theano.configdefaults): g++ not detected, blah... blah. Run this in your Anaconda prompt. conda install mingw libpython<jupyter_code>from keras.models import Sequential
from keras.layers import Dense
import numpy<jupyter_output><empty_output><jupyter_text>Step 2.  Set our random seed Run an algorithm on a dataset and you've built a great model. Can you produce the same model again given the same data?You should be able to. It should be a requirement that is high on the list for your modeling project.> We achieve reproducibility in applied machine learning by using the exact same code, data and sequence of random numbers.Random numbers are created using a random number generator. It’s a simple program that generates a sequence of numbers that are random enough for most applications.This math function is deterministic. If it uses the same starting point called a seed number, it will give the same sequence of random numbers.Hold on... what's **deterministic** mean? > "a deterministic algorithm is an algorithm which, given a particular input, will always produce the same output, with the underlying machine always passing through the same sequence of states"Let's apply an English translator to this: > The **only purpose of seeding** is to make sure that you get the **exact same result** when you run this code many times on the exact same data.<jupyter_code>seed = 9
numpy.random.seed(seed)<jupyter_output><empty_output><jupyter_text>Step 3.  Import our data set Let's import the object called read_csv. We define a variable called filename and put our data set in it. The last line does the work. It using the function called **read_csv** to put the contents of our data set into a variable called dataframe.<jupyter_code>from pandas import read_csv
filename = 'BBCN.csv'
dataframe = read_csv(filename)<jupyter_output><empty_output><jupyter_text>Step 4.  Split the Output Variables The first thing we need to do is put our data in an array. > An array is a data structure that stores values of **same data type**. In Python, this is the main difference between arrays and lists. While python lists can contain values corresponding to different data types, arrays in python can only contain values corresponding to same data type.<jupyter_code>array = dataframe.values<jupyter_output><empty_output><jupyter_text>The code below is the trickest part of the exercise. Now, we are assinging X and y as output variables.> That looks pretty easy but keep in mind that an array starts at 0. If you take a look at the shape of our dataframe (shape means the number of columns and rows) you can see we have 12 rows. On the X array below we saying... include all items in the array from 0 to 11. On the y array below we are saying... just use the column in the array mapped to the **11th row**. The **BikeBuyer** column. > Before we split X and Y out we are going to put them in an array.<jupyter_code>X = array[:,0:11] 
Y = array[:,11]
dataframe.head()<jupyter_output><empty_output><jupyter_text>Step 4.  Build the Model We can piece it all together by adding each layer. > The first layer has 11 neurons and expects 11 input variables. The second hidden layer has 8 neurons.The third hidden layer has 8 neurons. The output layer has 1 neuron to predict the class. How many hidden layers are in our model?<jupyter_code>model = Sequential()
model.add(Dense(12, input_dim=11, init='uniform', activation='relu'))
model.add(Dense(8, init='uniform', activation='relu'))
model.add(Dense(1, init='uniform', activation='sigmoid'))<jupyter_output><empty_output><jupyter_text>Step 5.  Compile the Model A metric is a function that is used to judge the performance of your model. Metric functions are to be supplied in the  metrics parameter when a model is compiled.>  Lastly, we set the cost (or loss) function to categorical_crossentropy. The (binary) cross-entropy is just the technical term for the **cost function** in logistic regression, and the categorical cross-entropy is its generalization for multi-class predictions via softmax Binary learning models are models which just predict one of two outcomes: positive or negative. These models are very well suited to drive decisions, such as whether to administer a patient a certain drug or to include a lead in a targeted marketing campaign.> Accuracy is perhaps the most intuitive performance measure. **It is simply the ratio of correctly predicted observations.**Using accuracy is only good for symmetric data sets where the class distribution is 50/50 and the cost of false positives and false negatives are roughly the same. In our case our classes are balanced.  Whenever you train a model with your data, you are actually producing some new values (predicted) for a specific feature. However, that specific feature already has some values which are real values in the dataset. > We know the the closer the predicted values to their corresponding real values, the better the model.We are using cost function to measure **how close the predicted values are to their corresponding real values.**So, for our model we choose binary_crossentropy.<jupyter_code>model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>Step 5.  Fit the Model **Epoch:** A full pass over all of your training data.For example, let's say you have 1213 observations. So an epoch concludes when it has finished a training pass over all 1213 of your observations.> What you'd expect to see from running fit on your Keras model, is a decrease in loss over n number of epochs. batch_size denotes the subset size of your training sample (e.g. 100 out of 1000) which is going to be used in order to train the network during its learning process. Each batch trains network in a successive order, taking into account the updated weights coming from the appliance of the previous batch. >Example: if you have 1000 training examples, and your batch size is 500, then it will take 2 iterations to complete 1 epoch.<jupyter_code>model.fit(X, Y, nb_epoch=200, batch_size=30)<jupyter_output><empty_output><jupyter_text>Step 6.  Score the Model<jupyter_code>scores = model.evaluate(X, Y)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.05635220125786163, 'fraction_numerical': 0.00930817610062893, 'mean_word_length': 4.964741185296324, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '658131', 'license': 'MIT', 'n_tokens_mistral': 2040, 'n_tokens_neox': 1919, 'n_words': 1184, 'path': 'A_Gentle_Introduction_to_Keras.ipynb', 'repo_name': 'akhilvydyula/Deep-Learning', 'size': '7959'}