<jupyter_start><jupyter_text>Machine LearningWhat is Machine Learning? Put simply, Machine Learning is altering the performance of an algorithm on future inputs based on previously seen data. Machine Learning is a subset of Artificial Intelligence (a large enough subset they are sometimes mistakenly used interchangeably) and Maching Learning has significant overlap with Statistics. Flavors of Machine LearningThe goal of Machine Learning is to produce a function (an algorithm) based on data. The type (think datatype) of input data and type of function determine the flavor of machine learning - which techniques are applicable to the problem. For any given type of problem, there are often multiple techniques that could be applied. This course will teach you which techniques you should use and how to use them.There are three dominant flavors of Machine Learning:1. **Supervised Learning** takes data of the form (input, output) and returns a function that maps from inputs to outputs. For example, face detection is a supervised learning problem. The machine learning algorithm uses data of existing pictures with _labels_ indicating where faces are in the photo. The algorithm produces a function (a _trained model_) that takes input images and outputs coordinates for any detected faces.2. **Unsupervised Learning** takes data of the form (input) and returns a function that maps from inputs to outputs. That should surprise you. Where do the outputs come from? Most unsupervised learning problems specify an objective (more on that in part 1-2) for the outputs on the training inputs. A common goal is for the model to produce outputs that simpler than the inputs (lower dimensional, for example) but that retain most of the "structure" of the inputs.3. **Reinforcement Learning** is used to model decision-making processes. RL takes data in the form of (last state, action taken, reward received, new state) and produces functions mapping from states to actions (this is called a _policy_ function). In this setting, state represents the context of a decision ("What do you do when the drone is falling?"), actions represent the things the decision-making agent can do ("Increase the downward thrust from the rotors."), and the reward is a form of numerical feedback on how the agent is doing ("Crashing is -37.")There are many other forms of Machine Learning, however, and the categories aren't necessarily mutually exclusive. We won't be able to cover them all, and many of them are specific to particular problems. But for example:1. **Semi-Supervised Learning** takes a combination of supervised and unsupervised data.2. **Generative Learning** takes a collection of input data and attempts to learn a function mapping from random noise to the observed distribution of data.3. **Model-Based Reinforcement Learning** is technically still a form of reinforcement learning, but it attempts to learn a transition model (a function mapping previous state and action taken to a probability distribution over next states), which is a form of supervised learning. This transition model is then used to reduce the amount of data needed to evaluate potential policy functions. How Far Does this Course Go?Machine Learning is a deep and rapidly expanding area of research, but this course will cover most of the major techniques in practical task-agnostic use today.  More importantly, it will give you the tools to learn more and to incorporate any technique you come across into a larger framework of development and evaluation. Types of Functions - Supervised TasksThe first part of this course covers Supervised Learning. In this setting, we are given data consisting of inputs and outputs (sometimes called labels/labelled values). The type of problem we are solving depends on the type of the inputs and outputs - are they categorical or real valued?- **Categorical Input, Categorical Output** - this is a simple discrete function, but there isn't much opportunity for generalization. The best you can usually do is to store observations for later lookup.- **Categorical Input, Real Output** - This is the realm of classical statistics. Given observations of, say, animal name and weight, we can produce summary statistics. So if someone asks for the weight of a cat, the model may report the mean value of 4 Kg.- **Real Input, Categorical Output** - These are called _Classification Problems_, and include everything from identifying objects in images to identifying weather phenomena. Classification algorithms are frequently concerned with _decision boundaries_. That is, where in the input space an input stops producing one output and starts producing another.- **Real Input, Real Output** - These are called _Regression Problems_, and include most forms of prediction. Predicting or inferring future temperature, pressure, windspeed, number of seals, time of day, or cost are all regression problems. Many classification problems can be cast as regression problems by regressing to the probability that an input produces a particular output. Regression using scikit-learnThere are many pre-built regression algorithms made available through the standard [scikit-learn library](https://scikit-learn.org/stable/). All of them have a very similar interface. First, the model is created. A list of input data X, and corresponding list of output data, Y, are passed to a `fit` method. The model can then be queried on some new input data X' using the `predict` method. Plotting Results using matplotlib[matplotlib](https://matplotlib.org/) is a commonly used library for generating plots and figures in Python. It supports many explort formats, and is easy to use. First, points/elements are added to the plot using a set of plotting commands. Then, the plot can be rendered by calling `show()`. These rendered plots conveniently show up inline in Jupyter notebooks.<jupyter_code>%matplotlib inline
# The above line makes graphics render within the notebook.
import numpy as np
from matplotlib import pyplot as plt

input_data = np.arange(20)
output_data = input_data * 3 + .2 + np.random.normal(0, 7, len(input_data))

plt.scatter(input_data, output_data)
plt.show()
from sklearn import linear_model

# Training inputs and outputs have to be reshaped to 2D matrices.
train_input = np.array(input_data).reshape(-1, 1)
train_output = np.array(output_data).reshape(-1, 1)

regr = linear_model.LinearRegression() # Model is Created
regr.fit(train_input, train_output) # Model is Trained
predictions = regr.predict(train_input) # Model predictions are evaluated
plt.scatter(input_data, output_data)
plt.plot(train_input, predictions)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.048039509129003295, 'fraction_numerical': 0.0032924274169410356, 'mean_word_length': 5.06442831215971, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '472985', 'license': 'FSFAP', 'n_tokens_mistral': 1522, 'n_tokens_neox': 1467, 'n_words': 968, 'path': 'Section 1/Part 1/1-1 Introduction.ipynb', 'repo_name': 'rgov/ml-bootcamp-2019', 'size': '6682'}