{"cells":[{"metadata":{"_uuid":"ba73af23692d08461a9ba25f757411cfe0755262"},"cell_type":"markdown","source":"# Awesome Data Science and Machine Learning Cheatsheets and Iris Species Classification\n\n    This kernel notebook has Iris Species Classification and the curated lists of Awesome Data Science and Machine Learning Cheatsheets to rule the world.\n\n> #### **Credits**: Thanks to **TensorFlow Team**, **Favio André Vázquez** and other contributers for such wonderful work!\n\n### Here are some of *my kernel notebooks* for **Machine Learning and Data Science** as follows, ***Upvote*** them if you *like* them\n\n> * [Data Science with R - Awesome Tutorials](https://www.kaggle.com/arunkumarramanan/data-science-with-r-awesome-tutorials)\n> * [Awesome ML Frameworks and MNIST Classification](https://www.kaggle.com/arunkumarramanan/awesome-machine-learning-ml-frameworks)\n> * [Awesome Data Science for Beginners with Titanic Exploration](https://kaggle.com/arunkumarramanan/awesome-data-science-for-beginners)\n> * [Tensorflow Tutorial and House Price Prediction](https://www.kaggle.com/arunkumarramanan/tensorflow-tutorial-and-examples)\n> * [Practical Machine Learning with PyTorch](https://www.kaggle.com/arunkumarramanan/practical-machine-learning-with-pytorch)\n> * [Awesome Deep Learning Basics and Resources](https://www.kaggle.com/arunkumarramanan/awesome-deep-learning-resources)\n> * [Awesome Computer Vision Resources (TBU)](https://www.kaggle.com/arunkumarramanan/awesome-computer-vision-resources-to-be-updated)\n> * [Data Scientist's Toolkits - Awesome Data Science Resources](https://www.kaggle.com/arunkumarramanan/data-scientist-s-toolkits-awesome-ds-resources)\n> * [Data Science with Python - Awesome Tutorials](https://www.kaggle.com/arunkumarramanan/data-science-with-python-awesome-tutorials)\n> * [Machine Learning and Deep Learning - Awesome Tutorials](https://www.kaggle.com/arunkumarramanan/awesome-deep-learning-ml-tutorials)\n> * [Machine Learning Engineer's Toolkit with Roadmap](https://www.kaggle.com/arunkumarramanan/machine-learning-engineer-s-toolkit-with-roadmap) \n> * [Awesome TensorFlow and PyTorch Resources](https://www.kaggle.com/arunkumarramanan/awesome-tensorflow-and-pytorch-resources)\n> * [Hands-on ML with scikit-learn and TensorFlow](https://www.kaggle.com/arunkumarramanan/hands-on-ml-with-scikit-learn-and-tensorflow)\n> * [Awesome Data Science IPython Notebooks](https://www.kaggle.com/arunkumarramanan/awesome-data-science-ipython-notebooks)\n> * [Data Science and Machine Learning Cheetcheets](https://www.kaggle.com/arunkumarramanan/data-science-and-machine-learning-cheatsheets)\n"},{"metadata":{"_uuid":"4816628eaf3bc3ce86cbf15069c929122508511b"},"cell_type":"markdown","source":"# Iris Species Classification with Custom Training: TensorFlow Walkthrough\n\nThis notebook uses machine learning to *categorize* Iris flowers by species. It uses TensorFlow's [eager execution](https://www.tensorflow.org/guide/eager) to:\n1. Build a model,\n2. Train this model on example data, and\n3. Use the model to make predictions about unknown data.\n\n## TensorFlow programming\n\nThis notebook uses these high-level TensorFlow concepts:\n\n* Enable an [eager execution](https://www.tensorflow.org/guide/eager) development environment,\n* Import data with the [Datasets API](https://www.tensorflow.org/guide/datasets),\n* Build models and layers with TensorFlow's [Keras API](https://keras.io/getting-started/sequential-model-guide/).\n\nThis tutorial is structured like many TensorFlow programs:\n\n1. Import and parse the data sets.\n2. Select the type of model.\n3. Train the model.\n4. Evaluate the model's effectiveness.\n5. Use the trained model to make predictions."},{"metadata":{"_uuid":"f68c9754ec3f482dc4563ac9964b0936333fdb94"},"cell_type":"markdown","source":"## Setup program\n\n### Configure imports and eager execution\n\nImport the required Python modules—including TensorFlow—and enable eager execution for this program. Eager execution makes TensorFlow evaluate operations immediately, returning concrete values instead of creating a [computational graph](https://www.tensorflow.org/guide/graphs) that is executed later. If you are used to a REPL or the `python` interactive console, this feels familiar. Eager execution is available in [Tensorlow >=1.8](https://www.tensorflow.org/install/).\n\nOnce eager execution is enabled, it *cannot* be disabled within the same program. See the [eager execution guide](https://www.tensorflow.org/guide/eager) for more details."},{"metadata":{"trusted":true,"_uuid":"92442f803ca9db1cdbd666c10e94236d87c9dd44"},"cell_type":"code","source":"from __future__ import absolute_import, division, print_function\n\nimport os\nimport matplotlib.pyplot as plt\n\nimport tensorflow as tf\n\ntf.enable_eager_execution()\n\nprint(\"TensorFlow version: {}\".format(tf.__version__))\nprint(\"Eager execution: {}\".format(tf.executing_eagerly()))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9a9db0564633c30b8c494e385601c157b82baf94"},"cell_type":"markdown","source":"## The Iris classification problem\n\nImagine you are a botanist seeking an automated way to categorize each Iris flower you find. Machine learning provides many algorithms to classify flowers statistically. For instance, a sophisticated machine learning program could classify flowers based on photographs. Our ambitions are more modest—we're going to classify Iris flowers based on the length and width measurements of their [sepals](https://en.wikipedia.org/wiki/Sepal) and [petals](https://en.wikipedia.org/wiki/Petal).\n\nThe Iris genus entails about 300 species, but our program will only classify the following three:\n\n* Iris setosa\n* Iris virginica\n* Iris versicolor\n\n<table>\n  <tr><td>\n    <img src=\"https://www.tensorflow.org/images/iris_three_species.jpg\"\n         alt=\"Petal geometry compared for three iris species: Iris setosa, Iris virginica, and Iris versicolor\">\n  </td></tr>\n  <tr><td align=\"center\">\n    <b>Figure 1.</b> <a href=\"https://commons.wikimedia.org/w/index.php?curid=170298\">Iris setosa</a> (by <a href=\"https://commons.wikimedia.org/wiki/User:Radomil\">Radomil</a>, CC BY-SA 3.0), <a href=\"https://commons.wikimedia.org/w/index.php?curid=248095\">Iris versicolor</a>, (by <a href=\"https://commons.wikimedia.org/wiki/User:Dlanglois\">Dlanglois</a>, CC BY-SA 3.0), and <a href=\"https://www.flickr.com/photos/33397993@N05/3352169862\">Iris virginica</a> (by <a href=\"https://www.flickr.com/photos/33397993@N05\">Frank Mayfield</a>, CC BY-SA 2.0).<br/>&nbsp;\n  </td></tr>\n</table>\n\nFortunately, someone has already created a [data set of 120 Iris flowers](https://en.wikipedia.org/wiki/Iris_flower_data_set) with the sepal and petal measurements. This is a classic dataset that is popular for beginner machine learning classification problems."},{"metadata":{"_uuid":"84e7e5a53cefc00739f007a1c64df281d395fb97"},"cell_type":"markdown","source":"## Import and parse the training dataset\n\nDownload the dataset file and convert it into a structure that can be used by this Python program.\n\n### Download the dataset\n\nDownload the training dataset file using the [tf.keras.utils.get_file](https://www.tensorflow.org/api_docs/python/tf/keras/utils/get_file) function. This returns the file path of the downloaded file."},{"metadata":{"trusted":true,"_uuid":"fb38c9367507a2d82a85a1283636e99047c2e43d"},"cell_type":"code","source":"train_dataset_url = \"https://storage.googleapis.com/download.tensorflow.org/data/iris_training.csv\"\n\ntrain_dataset_fp = tf.keras.utils.get_file(fname=os.path.basename(train_dataset_url),\n                                           origin=train_dataset_url)\n\nprint(\"Local copy of the dataset file: {}\".format(train_dataset_fp))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a8b46e97cda6a5d9ad5948761203b791242779c2"},"cell_type":"markdown","source":"### Inspect the data\n\nThis dataset, `iris_training.csv`, is a plain text file that stores tabular data formatted as comma-separated values (CSV). Use the `head -n5` command to take a peak at the first five entries:"},{"metadata":{"trusted":true,"_uuid":"8e23faece6da576771b538bb3ea0d4636978224d"},"cell_type":"code","source":"!head -n5 {train_dataset_fp}","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"291e360473f6a6121f25981c31bf87b9476b683c"},"cell_type":"markdown","source":"From this view of the dataset, notice the following:\n\n1. The first line is a header containing information about the dataset:\n  * There are 120 total examples. Each example has four features and one of three possible label names. \n2. Subsequent rows are data records, one *[example](https://developers.google.com/machine-learning/glossary/#example)* per line, where:\n  * The first four fields are *[features](https://developers.google.com/machine-learning/glossary/#feature)*: these are characteristics of an example. Here, the fields hold float numbers representing flower measurements.\n  * The last column is the *[label](https://developers.google.com/machine-learning/glossary/#label)*: this is the value we want to predict. For this dataset, it's an integer value of 0, 1, or 2 that corresponds to a flower name.\n\nLet's write that out in code:"},{"metadata":{"trusted":true,"_uuid":"027f8f7e68a7d707de0ce9b2a30025797c7c8f95"},"cell_type":"code","source":"# column order in CSV file\ncolumn_names = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species']\n\nfeature_names = column_names[:-1]\nlabel_name = column_names[-1]\n\nprint(\"Features: {}\".format(feature_names))\nprint(\"Label: {}\".format(label_name))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"23b07e4ea1d0339a435026f37f592b81d136a7b9"},"cell_type":"markdown","source":"Each label is associated with string name (for example, \"setosa\"), but machine learning typically relies on numeric values. The label numbers are mapped to a named representation, such as:\n\n* `0`: Iris setosa\n* `1`: Iris versicolor\n* `2`: Iris virginica\n\nFor more information about features and labels, see the [ML Terminology section of the Machine Learning Crash Course](https://developers.google.com/machine-learning/crash-course/framing/ml-terminology)."},{"metadata":{"trusted":true,"_uuid":"8e517b5814604bf5dc208b33bb777fffe74026c1"},"cell_type":"code","source":"class_names = ['Iris setosa', 'Iris versicolor', 'Iris virginica']","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"623c829f7a36f8e7a4d4a1310d0088254a93b384"},"cell_type":"markdown","source":"### Create a `tf.data.Dataset`\n\nTensorFlow's [Dataset API](https://www.tensorflow.org/guide/datasets) handles many common cases for loading data into a model. This is a high-level API for reading data and transforming it into a form used for training. See the [Datasets Quick Start guide](https://www.tensorflow.org/get_started/datasets_quickstart) for more information.\n\n\nSince the dataset is a CSV-formatted text file, use the [make_csv_dataset](https://www.tensorflow.org/api_docs/python/tf/contrib/data/make_csv_dataset) function to parse the data into a suitable format. Since this function generates data for training models, the default behavior is to shuffle the data (`shuffle=True, shuffle_buffer_size=10000`), and repeat the dataset forever (`num_epochs=None`). We also set the [batch_size](https://developers.google.com/machine-learning/glossary/#batch_size) parameter."},{"metadata":{"trusted":true,"_uuid":"b20a7531c58350d1caf447e42d10a8df0110c647"},"cell_type":"code","source":"batch_size = 32\n\ntrain_dataset = tf.contrib.data.make_csv_dataset(\n    train_dataset_fp,\n    batch_size, \n    column_names=column_names,\n    label_name=label_name,\n    num_epochs=1)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e8beaddf138033d57fb1ff684a9e4c03fd3abb0b"},"cell_type":"markdown","source":"The `make_csv_dataset` function returns a `tf.data.Dataset` of `(features, label)` pairs, where `features` is a dictionary: `{'feature_name': value}`\n\nWith eager execution enabled, these `Dataset` objects are iterable. Let's look at a batch of features:"},{"metadata":{"trusted":true,"_uuid":"b9d5bd231b60e494fd1b348d566e5aba64e12041"},"cell_type":"code","source":"features, labels = next(iter(train_dataset))\n\nfeatures","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"13611840a0a536cbe86e78491c8cca18d8222c5e"},"cell_type":"markdown","source":"Notice that like-features are grouped together, or *batched*. Each example row's fields are appended to the corresponding feature array. Change the `batch_size` to set the number of examples stored in these feature arrays.\n\nYou can start to see some clusters by plotting a few features from the batch:"},{"metadata":{"trusted":true,"_uuid":"4b750cafb7301852b0382326a0e6893ad9130d08"},"cell_type":"code","source":"plt.scatter(features['petal_length'].numpy(),\n            features['sepal_length'].numpy(),\n            c=labels.numpy(),\n            cmap='viridis')\n\nplt.xlabel(\"Petal length\")\nplt.ylabel(\"Sepal length\");","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2933f8c932ed2ef888ed641282c4e4c314876557"},"cell_type":"markdown","source":"To simplify the model building step, create a function to repackage the features dictionary into a single array with shape: `(batch_size, num_features)`.\n\nThis function uses the [tf.stack](https://www.tensorflow.org/api_docs/python/tf/stack) method which takes values from a list of tensors and creates a combined tensor at the specified dimension."},{"metadata":{"trusted":true,"_uuid":"7936f039e2f753d9fe3fbc67371e7ac74ecd3859"},"cell_type":"code","source":"def pack_features_vector(features, labels):\n  \"\"\"Pack the features into a single array.\"\"\"\n  features = tf.stack(list(features.values()), axis=1)\n  return features, labels","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ae190c53e2b641740959e112a32956d2bc4a6565"},"cell_type":"markdown","source":"Then use the [tf.data.Dataset.map](https://www.tensorflow.org/api_docs/python/tf/data/dataset/map) method to pack the `features` of each `(features,label)` pair into the training dataset:"},{"metadata":{"trusted":true,"_uuid":"fad1a977cd355eb61c30e924e67a3e4bc0b087d6"},"cell_type":"code","source":"train_dataset = train_dataset.map(pack_features_vector)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2f634449cb778e0a71524d4bc87f9ee7e637e093"},"cell_type":"markdown","source":"The features element of the `Dataset` are now arrays with shape `(batch_size, num_features)`. Let's look at the first few examples:"},{"metadata":{"trusted":true,"_uuid":"3e0471811f744a93afa794c0bac0801121e3b945"},"cell_type":"code","source":"features, labels = next(iter(train_dataset))\n\nprint(features[:5])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e0d4d8a96a46e5b72c598abbba2748f2c3dc24cf"},"cell_type":"markdown","source":"## Select the type of model\n\n### Why model?\n\nA *[model](https://developers.google.com/machine-learning/crash-course/glossary#model)* is a relationship between features and the label.  For the Iris classification problem, the model defines the relationship between the sepal and petal measurements and the predicted Iris species. Some simple models can be described with a few lines of algebra, but complex machine learning models have a large number of parameters that are difficult to summarize.\n\nCould you determine the relationship between the four features and the Iris species *without* using machine learning?  That is, could you use traditional programming techniques (for example, a lot of conditional statements) to create a model?  Perhaps—if you analyzed the dataset long enough to determine the relationships between petal and sepal measurements to a particular species. And this becomes difficult—maybe impossible—on more complicated datasets. A good machine learning approach *determines the model for you*. If you feed enough representative examples into the right machine learning model type, the program will figure out the relationships for you.\n\n### Select the model\n\nWe need to select the kind of model to train. There are many types of models and picking a good one takes experience. This tutorial uses a neural network to solve the Iris classification problem. *[Neural networks](https://developers.google.com/machine-learning/glossary/#neural_network)* can find complex relationships between features and the label. It is a highly-structured graph, organized into one or more *[hidden layers](https://developers.google.com/machine-learning/glossary/#hidden_layer)*. Each hidden layer consists of one or more *[neurons](https://developers.google.com/machine-learning/glossary/#neuron)*. There are several categories of neural networks and this program uses a dense, or *[fully-connected neural network](https://developers.google.com/machine-learning/glossary/#fully_connected_layer)*: the neurons in one layer receive input connections from *every* neuron in the previous layer. For example, Figure 2 illustrates a dense neural network consisting of an input layer, two hidden layers, and an output layer:\n\n<table>\n  <tr><td>\n    <img src=\"https://www.tensorflow.org/images/custom_estimators/full_network.png\"\n         alt=\"A diagram of the network architecture: Inputs, 2 hidden layers, and outputs\">\n  </td></tr>\n  <tr><td align=\"center\">\n    <b>Figure 2.</b> A neural network with features, hidden layers, and predictions.<br/>&nbsp;\n  </td></tr>\n</table>\n\nWhen the model from Figure 2 is trained and fed an unlabeled example, it yields three predictions: the likelihood that this flower is the given Iris species. This prediction is called *[inference](https://developers.google.com/machine-learning/crash-course/glossary#inference)*. For this example, the sum of the output predictions is 1.0. In Figure 2, this prediction breaks down as: `0.02` for *Iris setosa*, `0.95` for *Iris versicolor*, and `0.03` for *Iris virginica*. This means that the model predicts—with 95% probability—that an unlabeled example flower is an *Iris versicolor*."},{"metadata":{"_uuid":"b828e4df81fb0756676e22af2d3ee99e65bcafa8"},"cell_type":"markdown","source":"### Create a model using Keras\n\nThe TensorFlow [tf.keras](https://www.tensorflow.org/api_docs/python/tf/keras) API is the preferred way to create models and layers. This makes it easy to build models and experiment while Keras handles the complexity of connecting everything together.\n\nThe [tf.keras.Sequential](https://www.tensorflow.org/api_docs/python/tf/keras/Sequential) model is a linear stack of layers. Its constructor takes a list of layer instances, in this case, two [Dense](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dense) layers with 10 nodes each, and an output layer with 3 nodes representing our label predictions. The first layer's `input_shape` parameter corresponds to the number of features from the dataset, and is required."},{"metadata":{"trusted":true,"_uuid":"4076e580e96848c3944b54be5ae1a9270ea430fd"},"cell_type":"code","source":"model = tf.keras.Sequential([\n  tf.keras.layers.Dense(10, activation=tf.nn.relu, input_shape=(4,)),  # input shape required\n  tf.keras.layers.Dense(10, activation=tf.nn.relu),\n  tf.keras.layers.Dense(3)\n])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6c34731ed4154b2eb05706013d1719f07420dda1"},"cell_type":"markdown","source":"The *[activation function](https://developers.google.com/machine-learning/crash-course/glossary#activation_function)* determines the output shape of each node in the layer. These non-linearities are important—without them the model would be equivalent to a single layer. There are many [available activations](https://www.tensorflow.org/api_docs/python/tf/keras/activations), but [ReLU](https://developers.google.com/machine-learning/crash-course/glossary#ReLU) is common for hidden layers.\n\nThe ideal number of hidden layers and neurons depends on the problem and the dataset. Like many aspects of machine learning, picking the best shape of the neural network requires a mixture of knowledge and experimentation. As a rule of thumb, increasing the number of hidden layers and neurons typically creates a more powerful model, which requires more data to train effectively."},{"metadata":{"_uuid":"542671ea3be7df069ecb42b408614299983ec5c5"},"cell_type":"markdown","source":"### Using the model\n\nLet's have a quick look at what this model does to a batch of features:"},{"metadata":{"trusted":true,"_uuid":"ac51b1f993655bc7ce78d3a20dc4b3b0e7023432"},"cell_type":"code","source":"predictions = model(features)\npredictions[:5]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"99d2ec5968644969f55eab7e757de9ab512aefbb"},"cell_type":"markdown","source":"Here, each example returns a [logit](https://developers.google.com/machine-learning/crash-course/glossary#logits) for each class. \n\nTo convert these logits to a probability for each class, use the [softmax](https://developers.google.com/machine-learning/crash-course/glossary#softmax) function:"},{"metadata":{"trusted":true,"_uuid":"bae74e4a688633e8855e7ce1041d194e4bf2982b"},"cell_type":"code","source":"tf.nn.softmax(predictions[:5])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"31fb59729cc921ca9b6aaf5d05b6afcffe10c8cf"},"cell_type":"markdown","source":"Taking the `tf.argmax` across classes gives us the predicted class index. But, the model hasn't been trained yet, so these aren't good predictions."},{"metadata":{"trusted":true,"_uuid":"11dc493e631067200ad08c720dd3bcfc85a090c7"},"cell_type":"code","source":"print(\"Prediction: {}\".format(tf.argmax(predictions, axis=1)))\nprint(\"    Labels: {}\".format(labels))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c7b49aed26c1b4154fc11c1ff05144ec49090278"},"cell_type":"markdown","source":"## Train the model\n\n*[Training](https://developers.google.com/machine-learning/crash-course/glossary#training)* is the stage of machine learning when the model is gradually optimized, or the model *learns* the dataset. The goal is to learn enough about the structure of the training dataset to make predictions about unseen data. If you learn *too much* about the training dataset, then the predictions only work for the data it has seen and will not be generalizable. This problem is called *[overfitting](https://developers.google.com/machine-learning/crash-course/glossary#overfitting)*—it's like memorizing the answers instead of understanding how to solve a problem.\n\nThe Iris classification problem is an example of *[supervised machine learning](https://developers.google.com/machine-learning/glossary/#supervised_machine_learning)*: the model is trained from examples that contain labels. In *[unsupervised machine learning](https://developers.google.com/machine-learning/glossary/#unsupervised_machine_learning)*, the examples don't contain labels. Instead, the model typically finds patterns among the features."},{"metadata":{"_uuid":"3974e75b14e6a8a433ceab39d6b5c266a85aa5bd"},"cell_type":"markdown","source":"### Define the loss and gradient function\n\nBoth training and evaluation stages need to calculate the model's *[loss](https://developers.google.com/machine-learning/crash-course/glossary#loss)*. This measures how off a model's predictions are from the desired label, in other words, how bad the model is performing. We want to minimize, or optimize, this value.\n\nOur model will calculate its loss using the [tf.keras.losses.categorical_crossentropy](https://www.tensorflow.org/api_docs/python/tf/losses/sparse_softmax_cross_entropy) function which takes the model's class probability predictions and the desired label, and returns the average loss across the examples."},{"metadata":{"trusted":true,"_uuid":"a34c9e9a0f092a7df6f92329099f026124ee7aa3"},"cell_type":"code","source":"def loss(model, x, y):\n  y_ = model(x)\n  return tf.losses.sparse_softmax_cross_entropy(labels=y, logits=y_)\n\n\nl = loss(model, features, labels)\nprint(\"Loss test: {}\".format(l))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2d477eb2e2e3b34aad52822f10363dd235f1e860"},"cell_type":"markdown","source":"Use the [tf.GradientTape](https://www.tensorflow.org/api_docs/python/tf/GradientTape) context to calculate the *[gradients](https://developers.google.com/machine-learning/crash-course/glossary#gradient)* used to optimize our model. For more examples of this, see the [eager execution guide](https://www.tensorflow.org/guide/eager)."},{"metadata":{"trusted":true,"_uuid":"ac78c26d91be5f3c1dabce34c3ba4d6c116f0333"},"cell_type":"code","source":"def grad(model, inputs, targets):\n  with tf.GradientTape() as tape:\n    loss_value = loss(model, inputs, targets)\n  return loss_value, tape.gradient(loss_value, model.trainable_variables)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"03c0063d53d5b21b895a8c03f71ac3968b2625fd"},"cell_type":"markdown","source":"### Create an optimizer\n\nAn *[optimizer](https://developers.google.com/machine-learning/crash-course/glossary#optimizer)* applies the computed gradients to the model's variables to minimize the `loss` function. You can think of the loss function as a curved surface (see Figure 3) and we want to find its lowest point by walking around. The gradients point in the direction of steepest ascent—so we'll travel the opposite way and move down the hill. By iteratively calculating the loss and gradient for each batch, we'll adjust the model during training. Gradually, the model will find the best combination of weights and bias to minimize loss. And the lower the loss, the better the model's predictions.\n\n<table>\n  <tr><td>\n    <img src=\"https://cs231n.github.io/assets/nn3/opt1.gif\" width=\"70%\"\n         alt=\"Optimization algorithms visualized over time in 3D space.\">\n  </td></tr>\n  <tr><td align=\"center\">\n    <b>Figure 3.</b> Optimization algorithms visualized over time in 3D space.<br/>(Source: <a href=\"http://cs231n.github.io/neural-networks-3/\">Stanford class CS231n</a>, MIT License, Image credit: <a href=\"https://twitter.com/alecrad\">Alec Radford</a>)\n  </td></tr>\n</table>\n\nTensorFlow has many [optimization algorithms](https://www.tensorflow.org/api_guides/python/train) available for training. This model uses the [tf.train.GradientDescentOptimizer](https://www.tensorflow.org/api_docs/python/tf/train/GradientDescentOptimizer) that implements the *[stochastic gradient descent](https://developers.google.com/machine-learning/crash-course/glossary#gradient_descent)* (SGD) algorithm. The `learning_rate` sets the step size to take for each iteration down the hill. This is a *hyperparameter* that you'll commonly adjust to achieve better results."},{"metadata":{"_uuid":"d086db02caf5c55adc25974440cf192d1195a88e"},"cell_type":"markdown","source":"Let's setup the optimizer and the `global_step` counter:"},{"metadata":{"trusted":true,"_uuid":"1a693bc3084e1bca2c8389a3ecdd01fef57ed7ac"},"cell_type":"code","source":"optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)\n\nglobal_step = tf.Variable(0)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4405e642f3cd1a832b9261789890df722780386c"},"cell_type":"markdown","source":"We'll use this to calculate a single optimization step:"},{"metadata":{"trusted":true,"_uuid":"002a8330c0e6c8d4490dcb47b12c3f633c636a3f"},"cell_type":"code","source":"loss_value, grads = grad(model, features, labels)\n\nprint(\"Step: {}, Initial Loss: {}\".format(global_step.numpy(),\n                                          loss_value.numpy()))\n\noptimizer.apply_gradients(zip(grads, model.trainable_variables), global_step)\n\nprint(\"Step: {},         Loss: {}\".format(global_step.numpy(),\n                                          loss(model, features, labels).numpy()))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4627bbe6743b4423b614ac35892f01f2b1ef2539"},"cell_type":"markdown","source":"### Training loop\n\nWith all the pieces in place, the model is ready for training! A training loop feeds the dataset examples into the model to help it make better predictions. The following code block sets up these training steps:\n\n1. Iterate each *epoch*. An epoch is one pass through the dataset.\n2. Within an epoch, iterate over each example in the training `Dataset` grabbing its *features* (`x`) and *label* (`y`).\n3. Using the example's features, make a prediction and compare it with the label. Measure the inaccuracy of the prediction and use that to calculate the model's loss and gradients.\n4. Use an `optimizer` to update the model's variables.\n5. Keep track of some stats for visualization.\n6. Repeat for each epoch.\n\nThe `num_epochs` variable is the number of times to loop over the dataset collection. Counter-intuitively, training a model longer does not guarantee a better model. `num_epochs` is a *[hyperparameter](https://developers.google.com/machine-learning/glossary/#hyperparameter)* that you can tune. Choosing the right number usually requires both experience and experimentation."},{"metadata":{"trusted":true,"_uuid":"c53a75129ca4d28fd6f1da4fabcca2d1652c37cf"},"cell_type":"code","source":"## Note: Rerunning this cell uses the same model variables\n\nfrom tensorflow import contrib\ntfe = contrib.eager\n\n# keep results for plotting\ntrain_loss_results = []\ntrain_accuracy_results = []\n\nnum_epochs = 201\n\nfor epoch in range(num_epochs):\n  epoch_loss_avg = tfe.metrics.Mean()\n  epoch_accuracy = tfe.metrics.Accuracy()\n\n  # Training loop - using batches of 32\n  for x, y in train_dataset:\n    # Optimize the model\n    loss_value, grads = grad(model, x, y)\n    optimizer.apply_gradients(zip(grads, model.trainable_variables),\n                              global_step)\n\n    # Track progress\n    epoch_loss_avg(loss_value)  # add current batch loss\n    # compare predicted label to actual label\n    epoch_accuracy(tf.argmax(model(x), axis=1, output_type=tf.int32), y)\n\n  # end epoch\n  train_loss_results.append(epoch_loss_avg.result())\n  train_accuracy_results.append(epoch_accuracy.result())\n  \n  if epoch % 50 == 0:\n    print(\"Epoch {:03d}: Loss: {:.3f}, Accuracy: {:.3%}\".format(epoch,\n                                                                epoch_loss_avg.result(),\n                                                                epoch_accuracy.result()))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"27adc1dd5df8031bf0745ebe7d5269c027837d58"},"cell_type":"markdown","source":"### Visualize the loss function over time\n\nWhile it's helpful to print out the model's training progress, it's often *more* helpful to see this progress. [TensorBoard](https://www.tensorflow.org/guide/summaries_and_tensorboard) is a nice visualization tool that is packaged with TensorFlow, but we can create basic charts using the `matplotlib` module.\n\nInterpreting these charts takes some experience, but you really want to see the *loss* go down and the *accuracy* go up."},{"metadata":{"trusted":true,"_uuid":"15f3f4257d92f20e585f40a1ed6f304571e5be5a"},"cell_type":"code","source":"fig, axes = plt.subplots(2, sharex=True, figsize=(12, 8))\nfig.suptitle('Training Metrics')\n\naxes[0].set_ylabel(\"Loss\", fontsize=14)\naxes[0].plot(train_loss_results)\n\naxes[1].set_ylabel(\"Accuracy\", fontsize=14)\naxes[1].set_xlabel(\"Epoch\", fontsize=14)\naxes[1].plot(train_accuracy_results);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"764f3cab522d40ba6bf3a658ea2cd670b3b8c291"},"cell_type":"markdown","source":"## Evaluate the model's effectiveness\n\nNow that the model is trained, we can get some statistics on its performance.\n\n*Evaluating* means determining how effectively the model makes predictions. To determine the model's effectiveness at Iris classification, pass some sepal and petal measurements to the model and ask the model to predict what Iris species they represent. Then compare the model's prediction against the actual label.  For example, a model that picked the correct species on half the input examples has an *[accuracy](https://developers.google.com/machine-learning/glossary/#accuracy)* of `0.5`. Figure 4 shows a slightly more effective model, getting 4 out of 5 predictions correct at 80% accuracy:\n\n<table cellpadding=\"8\" border=\"0\">\n  <colgroup>\n    <col span=\"4\" >\n    <col span=\"1\" bgcolor=\"lightblue\">\n    <col span=\"1\" bgcolor=\"lightgreen\">\n  </colgroup>\n  <tr bgcolor=\"lightgray\">\n    <th colspan=\"4\">Example features</th>\n    <th colspan=\"1\">Label</th>\n    <th colspan=\"1\" >Model prediction</th>\n  </tr>\n  <tr>\n    <td>5.9</td><td>3.0</td><td>4.3</td><td>1.5</td><td align=\"center\">1</td><td align=\"center\">1</td>\n  </tr>\n  <tr>\n    <td>6.9</td><td>3.1</td><td>5.4</td><td>2.1</td><td align=\"center\">2</td><td align=\"center\">2</td>\n  </tr>\n  <tr>\n    <td>5.1</td><td>3.3</td><td>1.7</td><td>0.5</td><td align=\"center\">0</td><td align=\"center\">0</td>\n  </tr>\n  <tr>\n    <td>6.0</td> <td>3.4</td> <td>4.5</td> <td>1.6</td> <td align=\"center\">1</td><td align=\"center\" bgcolor=\"red\">2</td>\n  </tr>\n  <tr>\n    <td>5.5</td><td>2.5</td><td>4.0</td><td>1.3</td><td align=\"center\">1</td><td align=\"center\">1</td>\n  </tr>\n  <tr><td align=\"center\" colspan=\"6\">\n    <b>Figure 4.</b> An Iris classifier that is 80% accurate.<br/>&nbsp;\n  </td></tr>\n</table>"},{"metadata":{"_uuid":"cb5ba18cef6db8a00950e648f7370b087c682e8e"},"cell_type":"markdown","source":"### Setup the test dataset\n\nEvaluating the model is similar to training the model. The biggest difference is the examples come from a separate *[test set](https://developers.google.com/machine-learning/crash-course/glossary#test_set)* rather than the training set. To fairly assess a model's effectiveness, the examples used to evaluate a model must be different from the examples used to train the model.\n\nThe setup for the test `Dataset` is similar to the setup for training `Dataset`. Download the CSV text file and parse that values, then give it a little shuffle:"},{"metadata":{"trusted":true,"_uuid":"811f05f886e83db0c1b75a0bfa738fa6a446f5ee"},"cell_type":"code","source":"test_url = \"https://storage.googleapis.com/download.tensorflow.org/data/iris_test.csv\"\n\ntest_fp = tf.keras.utils.get_file(fname=os.path.basename(test_url),\n                                  origin=test_url)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"98436af69b2cb50f00e35d373f54a7af556546cc"},"cell_type":"code","source":"test_dataset = tf.contrib.data.make_csv_dataset(\n    test_fp,\n    batch_size, \n    column_names=column_names,\n    label_name='species',\n    num_epochs=1,\n    shuffle=False)\n\ntest_dataset = test_dataset.map(pack_features_vector)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9ce69e372ad272cec1c6f0ad576d81222bb7b901"},"cell_type":"markdown","source":"### Evaluate the model on the test dataset\n\nUnlike the training stage, the model only evaluates a single [epoch](https://developers.google.com/machine-learning/glossary/#epoch) of the test data. In the following code cell, we iterate over each example in the test set and compare the model's prediction against the actual label. This is used to measure the model's accuracy across the entire test set."},{"metadata":{"trusted":true,"_uuid":"57bc61f55cc9b202772c07a470833e8fff256a48"},"cell_type":"code","source":"test_accuracy = tfe.metrics.Accuracy()\n\nfor (x, y) in test_dataset:\n  logits = model(x)\n  prediction = tf.argmax(logits, axis=1, output_type=tf.int32)\n  test_accuracy(prediction, y)\n\nprint(\"Test set accuracy: {:.3%}\".format(test_accuracy.result()))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a97a7e05ee3e11f82c47875ba603eb259010ace6"},"cell_type":"markdown","source":"We can see on the last batch, for example, the model is usually correct:"},{"metadata":{"trusted":true,"_uuid":"f415d6f9866e3951d523ffee3453cb4a3d403fdb"},"cell_type":"code","source":"tf.stack([y,prediction],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"60a0bdeaf643c2bdb092f238dfa3f997d24f4680"},"cell_type":"markdown","source":"## Use the trained model to make predictions\n\nWe've trained a model and \"proven\" that it's good—but not perfect—at classifying Iris species. Now let's use the trained model to make some predictions on [unlabeled examples](https://developers.google.com/machine-learning/glossary/#unlabeled_example); that is, on examples that contain features but not a label.\n\nIn real-life, the unlabeled examples could come from lots of different sources including apps, CSV files, and data feeds. For now, we're going to manually provide three unlabeled examples to predict their labels. Recall, the label numbers are mapped to a named representation as:\n\n* `0`: Iris setosa\n* `1`: Iris versicolor\n* `2`: Iris virginica"},{"metadata":{"_uuid":"2066a44e2af50772a0afb0c5394e437d98ee37ae"},"cell_type":"markdown","source":"## License\n\n##### Copyright 2018 The TensorFlow Authors."},{"metadata":{"trusted":true,"_uuid":"7d1c815e3412a33a36c47ddfcc54355a5c35273b"},"cell_type":"code","source":"#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# https://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"markdown","source":"# Awesome Data Science and Machine Learning Cheatsheets\n\n## Table of Contents\n\n- [Business Science](#business-science)\n- [Python](#python)\n- [R](#r)\n- [Math and Calculus](#math-and-calculus)\n- [Big Data](#big-data)\n- [Machine Learning](#machine-learning)\n- [Deep Learning](#deep-learning)\n- [SQL](#sql)\n- [Data Visualization](#data-visualization)\n- [Data Science in General and Others](#data-science-in-general-and-others)\n\n----------------------------------------\n\n\n# [Business Science](https://www.business-science.io/) \n\n## [Business Science Problem Framework (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Business_Science/Business_Science_Problem_Framework.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Business_Science/img/Business_Science_Problem_Framework.png)\n\n## [Data Science with Python Workflow (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Business_Science/Data_Science_With_Python_Workflow.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Business_Science/img/Data_Science_With_Python_Workflow.png)\n\n## [Data Science with R Workflow (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Business_Science/Data_Science_With_R_Workflow.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Business_Science/img/Data_Science_With_R_Workflow.png)\n\n# Python\n\n## Datacamp\n\n- [Python Basics (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Datacamp/python_basics.pdf)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Datacamp/img/python_basics-1.png)\n\n- [Pandas Basics (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Datacamp/pandas_basics.pdf)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Datacamp/img/pandas_basics-1.png)\n\n- [Pandas (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Datacamp/pandas.pdf)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Datacamp/img/pandas-1.png)\n\n- [Importing Data (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Datacamp/import_data.pdf)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Datacamp/img/import_data-1.png)\n\n- [Jupyter (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Datacamp/jupyter.pdf)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Datacamp/img/jupyter-1.png)\n\n- [Numpy Basics (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Datacamp/numpy_basics.pdf)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Datacamp/img/numpy_basics-1.png)\n\n## [Python Crash Course](https://github.com/ehmatthes/pcc)\n\n- [Begginers Python Cheat Sheet (Long PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Python_Crash_Course/Begginers-Python-Cheat-Sheet.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Python_Crash_Course/Begginers-Python-Cheat-Sheet.png)\n\n## Dataquest\n\n- [Intermediate Python (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Dataquest/Intermediate-Python-Cheat-Sheet.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Dataquest/img/Intermediate-Python-Cheat-Sheet-1.png)\n\n## Others\n\n- [Python 3 Memento (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Others/mementopython3-english.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Others/img/mementopython3-english/mementopython3-english-1.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Others/img/mementopython3-english/mementopython3-english-2.png)\n\n\n# R\n\n## Datacamp\n\n- [Tidiverse (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/Datacamp/tidiverse_beginners.pdf)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/Datacamp/img/tidiverse_beginners-1.png)\n\n- [data.table (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/Datacamp/data_table.pdf)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/Datacamp/img/data_table-1.png)\n\n-[xts (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/Datacamp/xts.pdf)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/Datacamp/img/xts-1.png)\n\n## RStudio\n\n- [R Studio IDE (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/rstudio-ide.pdf)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/rstudio-ide.png)\n\n- [Base R (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/base-r.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/base-r.png)\n\n- [Data Import with readr (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/data-import.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/data-import.png)\n\n- [Data Transformation with Dplyr (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/data-transformation.pdf)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/data-transformation.png)\n\n- [Apply Functions with purrr (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/purrr.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/purrr.png)\n\n- [Data transformation with data.table (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/datatable.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/datatable.png)\n\n- [Dates and Times with lubridate (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/lubridate.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/lubridate.png)\n\n- [Randomizr (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/randomizr.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/randomizr.png)\n\n- [Regular Expressions (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/regex.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/regular-expressions.png)\n\n- [Work with Strings with stringr (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/strings.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/strings.png)\n\n- [Tidy Evaluation with rlang (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/tidyeval.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/tidyeval.png)\n\n- [Xplain (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/xplain.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/xplain.png)\n\n- [Sintax Comparison (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/syntax.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/syntax.png)\n\n- [Data and Variable Transformation with sjmisc (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/sjmisc.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/sjmisc.png)\n\n- [R Markdown (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/rmarkdown-2.0.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/rmarkdown-2.0.png)\n\n- [Package Development with devtools (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/package-development.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/package-development.png)\n\n\n# Math and Calculus\n\nFrom @afshinea:\n\n- [Refresher Algebra and Calculus (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Math_Calculus/refresher-algebra-calculus.pdf)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Math_Calculus/img/refresher-algebra-calculus/refresher-algebra-calculus-1.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Math_Calculus/img/refresher-algebra-calculus/refresher-algebra-calculus-2.png)\n\n- [Refresher Probabilities and Statistics (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Math_Calculus/refresher-probabilities-statistics.pdf)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Math_Calculus/img/refresher-probabilities-statistics/refresher-probabilities-statistics-1.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Math_Calculus/img/refresher-probabilities-statistics/refresher-probabilities-statistics-2.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Math_Calculus/img/refresher-probabilities-statistics/refresher-probabilities-statistics-3.png)\n\n# Big Data\n\n## Python\n\n- [Pyspark RDD (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Big_Data/pyspark_rdd.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Big_Data/img/pyspark_rdd/pyspark_rdd-1.png)\n\n- [Pyspark DF (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Big_Data/pyspark_df.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Big_Data/img/pyspark_df/pyspark_df-1.png)\n\n- [Dask (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Big_Data/daskcheatsheet.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Big_Data/img/daskcheatsheet/daskcheatsheet-1.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Big_Data/img/daskcheatsheet/daskcheatsheet-2.png)\n\n## R\n\n- [Sparklyr (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/sparklyr.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/sparklyr.png)\n\n# Machine Learning\n\n## Python\n\n- [Scitk-Learn (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Datacamp/scikit-learn.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Datacamp/img/scikit-learn-1.png)\n\n## R\n\n- [Machine Learning Modelling in R (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/Machine%20Learning%20Modelling%20in%20R.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/Machine%20Learning%20Modelling%20in%20R.png)\n\n- [Caret (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/caret.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/caret.png)\n\n- [Estimatr (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/estimatr.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/estimatr.png)\n\n_ [H2O (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/h2o.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/h2o.png)\n\n- [mlr (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/mlr.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/mlr.png)\n\n\n## Supervised Learning\n\nFrom @afshinea: \n\n- [VIP Supervised Learning (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Machine_Learning/Supervised%20Learning/cheatsheet-supervised-learning.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Machine_Learning/Supervised%20Learning/img/cheatsheet-supervised-learning/cheatsheet-supervised-learning-1.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Machine_Learning/Supervised%20Learning/img/cheatsheet-supervised-learning/cheatsheet-supervised-learning-2.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Machine_Learning/Supervised%20Learning/img/cheatsheet-supervised-learning/cheatsheet-supervised-learning-3.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Machine_Learning/Supervised%20Learning/img/cheatsheet-supervised-learning/cheatsheet-supervised-learning-4.png)\n\n## Unsupervised Learning\n\nFrom @afshinea:\n\n- [VIP Unsupervised Learning (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Machine_Learning/Unsupervised%20Learning/cheatsheet-unsupervised-learning.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Machine_Learning/Unsupervised%20Learning/img/cheatsheet-unsupervised-learning/cheatsheet-unsupervised-learning-1.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Machine_Learning/Unsupervised%20Learning/img/cheatsheet-unsupervised-learning/cheatsheet-unsupervised-learning-2.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Machine_Learning/Unsupervised%20Learning/img/cheatsheet-unsupervised-learning/cheatsheet-unsupervised-learning-3.png)\n\n## Hacks, tricks and tips\n\nFrom @afshinea:\n\n- [VIP Machine Learning Tips and Tricks (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Machine_Learning/Tricks/cheatsheet-machine-learning-tips-and-tricks.pdf)\n\n![cheatsheet-machine-learning-tips-and-tricks-1](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Machine_Learning/Tricks/img/cheatsheet-machine-learning-tips-and-tricks/cheatsheet-machine-learning-tips-and-tricks-1.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Machine_Learning/Tricks/img/cheatsheet-machine-learning-tips-and-tricks/cheatsheet-machine-learning-tips-and-tricks-2.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Machine_Learning/Tricks/img/cheatsheet-machine-learning-tips-and-tricks/cheatsheet-machine-learning-tips-and-tricks-3.png)\n\n## Chossing the right model\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Machine_Learning/Choosing_Model/img/microsoft-machine-learning-algorithm-cheat-sheet-v7-1.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Machine_Learning/Choosing_Model/img/ml_map_scikit_learn.png)\n\n\n# Deep Learning\n\n## Neural Nets\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Deep_Learning/Neural%20Network%20Cells.png)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Deep_Learning/Neural%20Network%20Graphs.png)\n\n## R\n\n- [Keras RStudio (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/keras.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/keras.png)\n\n\n## Python\n\n- [Keras (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Datacamp/keras.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Datacamp/img/keras-1.png)\n\nFrom @afshinea: \n\n- [Deep Learning Basics (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Deep_Learning/cheatsheet-deep-learning.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Deep_Learning/img/cheatsheet-deep-learning/cheatsheet-deep-learning-1.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Deep_Learning/img/cheatsheet-deep-learning/cheatsheet-deep-learning-2.png)\n\n- [Convolutional Neural Networkds (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Deep_Learning/cheatsheet-convolutional-neural-networks.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Deep_Learning/img/cheatsheet-convolutional-neural-networks/cheatsheet-convolutional-neural-networks-1.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Deep_Learning/img/cheatsheet-convolutional-neural-networks/cheatsheet-convolutional-neural-networks-2.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Deep_Learning/img/cheatsheet-convolutional-neural-networks/cheatsheet-convolutional-neural-networks-3.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Deep_Learning/img/cheatsheet-convolutional-neural-networks/cheatsheet-convolutional-neural-networks-4.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Deep_Learning/img/cheatsheet-convolutional-neural-networks/cheatsheet-convolutional-neural-networks-5.png)\n\n- [Recurrent Neural Networks (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Deep_Learning/cheatsheet-recurrent-neural-networks.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Deep_Learning/img/cheatsheet-recurrent-neural-networks/cheatsheet-recurrent-neural-networks-1.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Deep_Learning/img/cheatsheet-recurrent-neural-networks/cheatsheet-recurrent-neural-networks-2.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Deep_Learning/img/cheatsheet-recurrent-neural-networks/cheatsheet-recurrent-neural-networks-3.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Deep_Learning/img/cheatsheet-recurrent-neural-networks/cheatsheet-recurrent-neural-networks-4.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Deep_Learning/img/cheatsheet-recurrent-neural-networks/cheatsheet-recurrent-neural-networks-5.png)\n\n- [Tips and Tricks (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Deep_Learning/cheatsheet-deep-learning-tips-tricks.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Deep_Learning/img/cheatsheet-deep-learning-tips-tricks/cheatsheet-deep-learning-tips-tricks-1.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Deep_Learning/img/cheatsheet-deep-learning-tips-tricks/cheatsheet-deep-learning-tips-tricks-2.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Deep_Learning/img/cheatsheet-deep-learning-tips-tricks/cheatsheet-deep-learning-tips-tricks-3.png)\n\n# SQL\n\n- [SQL cheatsheet by sqltutorial (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/SQL/SQL-cheat-sheet.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/SQL/img/SQL-cheat-sheet/SQL-cheat-sheet-1.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/SQL/img/SQL-cheat-sheet/SQL-cheat-sheet-2.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/SQL/img/SQL-cheat-sheet/SQL-cheat-sheet-3.png)\n\n- [SQL cheatsheet by Rebel Labs](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/SQL/zt_sql_cheat_sheet.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/SQL/img/zt_sql_cheat_sheet/zt_sql_cheat_sheet-1.png)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/SQL/img/sql_reddit.jpg)\n\n# Data Visualization\n\n## Python\n\n- [Matplotlib (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Datacamp/matplotlib.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Datacamp/img/matplotlib-1.png)\n\n- [Seaborn (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Datacamp/seaborn.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Datacamp/img/seaborn-1.png)\n\n- [Bokeh (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Datacamp/bokeh.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Python/Datacamp/img/bokeh-1.png)\n\n- Comprehensive Guide to Data Visualization in Python\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Data_Visualization/img/data-visualisation-infographics-python.jpg)\n\n## R\n\n- [Ggplot2 (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/data-visualization-2.1.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/data-visualization-2.1.png)\n\n- [Leaflet (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/leaflet.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/leaflet.png)\n\n- [Cartography (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/cartography.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/cartography.png)\n\n- [Comprehensive Guide to Data Visualization in R](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Data_Visualization/Comprehensive%20Guide%20to%20Data%20Visualization_R.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/Data_Visualization/img/Comprehensive%20Guide%20to%20Data%20Visualization_R-1.png)\n\n- [Simple Features sf (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/sf.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/sf.png)\n\n- [survminer (PDF)](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/survminer.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/R/RStudio/img/survminer.png)\n\n# Data Science in General and Others\n\n- [Shiny (PDF)](https://github.com/rstudio/cheatsheets/blob/master/shiny.pdf)\n\n![](https://github.com/rstudio/cheatsheets/raw/master/pngs/shiny.png)\n\n- [Vim (PDF)](https://github.com/FavioVazquez/ds-cheatsheets/blob/master/General/Text_Editors/Vim/vim_cheat_sheet_for_programmers_print.pdf)\n\n![](https://github.com/FavioVazquez/ds-cheatsheets/raw/master/General/Text_Editors/Vim/vim_cheat_sheet_for_programmers_print.png)\n\n- [Emacs (PDF)](https://github.com/FavioVazquez/ds-cheatsheets/blob/master/General/Text_Editors/Emacs/How-to-learn-Emacs-v2-large.pdf)\n\n![](https://github.com/FavioVazquez/ds-cheatsheets/raw/master/General/Text_Editors/Emacs/How-to-learn-Emacs-v2-large.png)\n\n- [Git - Atlassian's Cheatsheet (PDF)](https://github.com/FavioVazquez/ds-cheatsheets/blob/master/General/Git/atlassian-git-cheatsheet.pdf)\n\n![](https://github.com/FavioVazquez/ds-cheatsheets/raw/master/General/Git/img/Atlassian_git_cheatsheet_pg1.png)\n\n![](https://github.com/FavioVazquez/ds-cheatsheets/raw/master/General/Git/img/Atlassian_git_cheatsheet_pg2.png)\n\n- [BASH (PDF)](https://github.com/FavioVazquez/ds-cheatsheets/blob/master/General/BASH/bash_cheat_sheet.pdf)\n\n![](https://github.com/FavioVazquez/ds-cheatsheets/raw/master/General/BASH/img/Linux-bash-cheatsheet-pg1.png)\n\n![](https://github.com/FavioVazquez/ds-cheatsheets/raw/master/General/BASH/img/Linux-bash-cheatsheet-pg2.png)\n\n![](https://github.com/FavioVazquez/ds-cheatsheets/raw/master/General/BASH/img/Linux-bash-cheatsheet-pg3.png)\n\n![](https://github.com/FavioVazquez/ds-cheatsheets/raw/master/General/BASH/img/Linux-bash-cheatsheet-pg4.png)\n\n![](https://github.com/FavioVazquez/ds-cheatsheets/raw/master/General/BASH/img/Linux-bash-cheatsheet-pg5.png)\n\n![](https://github.com/FavioVazquez/ds-cheatsheets/raw/master/General/BASH/img/Linux-bash-cheatsheet-pg6.png)\n\nBy @ml874\n\n- [Data Science Cheatsheet (PDF)](https://github.com/FavioVazquez/ds-cheatsheets/blob/master/General/data-science-cheatsheet.pdf)\n\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/General/img/data-science-cheatsheet/data-science-cheatsheet-01.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/General/img/data-science-cheatsheet/data-science-cheatsheet-02.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/General/img/data-science-cheatsheet/data-science-cheatsheet-03.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/General/img/data-science-cheatsheet/data-science-cheatsheet-04.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/General/img/data-science-cheatsheet/data-science-cheatsheet-05.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/General/img/data-science-cheatsheet/data-science-cheatsheet-06.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/General/img/data-science-cheatsheet/data-science-cheatsheet-07.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/General/img/data-science-cheatsheet/data-science-cheatsheet-08.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/General/img/data-science-cheatsheet/data-science-cheatsheet-09.png)\n![](https://github.com/ArunkumarRamanan/ds-cheatsheets/raw/master/General/img/data-science-cheatsheet/data-science-cheatsheet-10.png)"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":false},"cell_type":"markdown","source":"## Credits (Reference)\n\n> * [Custom training: walkthrough](https://www.tensorflow.org/tutorials/eager/custom_training_walkthrough)\n> * [Favio André Vázquez - DS Cheetsheets](https://github.com/FavioVazquez/ds-cheatsheets)\n> * [GitHub Data Science Topic](https://github.com/topics/data-science)\n> * [GitHub Topic - Deep Learning](https://github.com/topics/deep-learning)\n\n## License\n\n[![CC0](http://mirrors.creativecommons.org/presskit/buttons/88x31/svg/cc-zero.svg)](https://creativecommons.org/publicdomain/zero/1.0/)\n\n### Please ***UPVOTE*** my kernel if you like it or wanna fork it.\n\n##### Feedback: If you have any ideas or you want any other content to be added to this curated list, please feel free to make any comments to make it better.\n#### I am open to have your *feedback* for improving this ***kernel***\n###### Hope you enjoyed this kernel!\n\n### Thanks for visiting my *Kernel* and please *UPVOTE* to stay connected and follow up the *further updates!*"}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}