Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
360 changes: 360 additions & 0 deletions your-code/.ipynb_checkpoints/Learning-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,360 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Introduction to Scikit-Learn\n",
"\n",
"\n",
"**Lesson Goals**\n",
"\n",
"In this lesson we focus on explaining the Scikit-learn Machine Learning Toolkit:\n",
"\n",
" You will learn how to install Scikit-learn and its dependencies.\n",
" You will learn about the functionalities of Scikit-learn that will help you in the implementation of the Machine Learning workflow in your Machine Learning projects.\n",
"\n",
"**Introduction**\n",
"\n",
"Scikit-learn is the leading machine learning library in Python. It is an open source library with contributions from hundreds of developers as well as support from major corporations like Google. Scikit-learn utilizes numpy and scipy.\n",
"Installation\n",
"\n",
"In this section, we will guide you through the process of installing Scikit-learn as a python package. Scikit-learn depends on other packages, so first we have to check that those dependecies are installed.\n",
"Dependencies\n",
"\n",
"Scikit-learn depends on NumPy and SciPy, so before proceeding to install the Scikit-learn package, we will check the availability of the Numpy and Scipy packages. We can invoke the Python interpreter asking it to load Numpy, and it will reply with an error if Numpy is unavailable. We can start up a Jupyter notebook and enter the following code in a cell:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The Python interpreter will try load the Numpy package. If the Numpy package is not installed, the python interpreter will print a message similar to the following.\n",
"\n",
"ImportError: No module named 'numpy'\n",
"\n",
"In this case, we will install Numpy directly in Jupyter by adding an exclamation before the command.\n",
"\n",
"**pip install numpy**\n",
"\n",
"Now we will check the installation of the Scipy package in a similar way. Just type at the command prompt:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import scipy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If no error is reported, then it means that the Scipy package is installed and accessible to the Python interpreter.\n",
"\n",
"If the package is not installed, you can install the package similarly with the command:\n",
"\n",
"**pip install scipy**\n",
"\n",
"\n",
"# Install Scikit-learn\n",
"\n",
"So far you have checked that the required dependencies of the Scikit-learn package are in place, so now you are ready to proceed to install the Scikit-learn package.\n",
"\n",
"The quickest and easiest way is to install the scikit-learn package directly in Jupyter. As before, we add an exclamation before the command.\n",
"\n",
"**pip install sklearn**\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The log should indicate that the installation was successful and note what version was installed.\n",
"\n",
"\n",
"# Main Functionalities\n",
"\n",
"In previous lessons we have discussed the machine learning workflow. As an open source library, scikit-learn has benefited from many contributions that have turned the library a great resource for all stages of the machine learning workflow.\n",
"\n",
"\n",
"**Load Dataset**\n",
"\n",
"Scikit-learn comes bundled with several well known public datasets, to take you up to speed quickly, avoiding the hassle of finding and downloading datasets from the web. These bundled datasets can be loaded by name without even providing a path to the dataset file. This is an example:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'data': array([[ 0.03807591, 0.05068012, 0.06169621, ..., -0.00259226,\n",
" 0.01990842, -0.01764613],\n",
" [-0.00188202, -0.04464164, -0.05147406, ..., -0.03949338,\n",
" -0.06832974, -0.09220405],\n",
" [ 0.08529891, 0.05068012, 0.04445121, ..., -0.00259226,\n",
" 0.00286377, -0.02593034],\n",
" ...,\n",
" [ 0.04170844, 0.05068012, -0.01590626, ..., -0.01107952,\n",
" -0.04687948, 0.01549073],\n",
" [-0.04547248, -0.04464164, 0.03906215, ..., 0.02655962,\n",
" 0.04452837, -0.02593034],\n",
" [-0.04547248, -0.04464164, -0.0730303 , ..., -0.03949338,\n",
" -0.00421986, 0.00306441]]),\n",
" 'target': array([151., 75., 141., 206., 135., 97., 138., 63., 110., 310., 101.,\n",
" 69., 179., 185., 118., 171., 166., 144., 97., 168., 68., 49.,\n",
" 68., 245., 184., 202., 137., 85., 131., 283., 129., 59., 341.,\n",
" 87., 65., 102., 265., 276., 252., 90., 100., 55., 61., 92.,\n",
" 259., 53., 190., 142., 75., 142., 155., 225., 59., 104., 182.,\n",
" 128., 52., 37., 170., 170., 61., 144., 52., 128., 71., 163.,\n",
" 150., 97., 160., 178., 48., 270., 202., 111., 85., 42., 170.,\n",
" 200., 252., 113., 143., 51., 52., 210., 65., 141., 55., 134.,\n",
" 42., 111., 98., 164., 48., 96., 90., 162., 150., 279., 92.,\n",
" 83., 128., 102., 302., 198., 95., 53., 134., 144., 232., 81.,\n",
" 104., 59., 246., 297., 258., 229., 275., 281., 179., 200., 200.,\n",
" 173., 180., 84., 121., 161., 99., 109., 115., 268., 274., 158.,\n",
" 107., 83., 103., 272., 85., 280., 336., 281., 118., 317., 235.,\n",
" 60., 174., 259., 178., 128., 96., 126., 288., 88., 292., 71.,\n",
" 197., 186., 25., 84., 96., 195., 53., 217., 172., 131., 214.,\n",
" 59., 70., 220., 268., 152., 47., 74., 295., 101., 151., 127.,\n",
" 237., 225., 81., 151., 107., 64., 138., 185., 265., 101., 137.,\n",
" 143., 141., 79., 292., 178., 91., 116., 86., 122., 72., 129.,\n",
" 142., 90., 158., 39., 196., 222., 277., 99., 196., 202., 155.,\n",
" 77., 191., 70., 73., 49., 65., 263., 248., 296., 214., 185.,\n",
" 78., 93., 252., 150., 77., 208., 77., 108., 160., 53., 220.,\n",
" 154., 259., 90., 246., 124., 67., 72., 257., 262., 275., 177.,\n",
" 71., 47., 187., 125., 78., 51., 258., 215., 303., 243., 91.,\n",
" 150., 310., 153., 346., 63., 89., 50., 39., 103., 308., 116.,\n",
" 145., 74., 45., 115., 264., 87., 202., 127., 182., 241., 66.,\n",
" 94., 283., 64., 102., 200., 265., 94., 230., 181., 156., 233.,\n",
" 60., 219., 80., 68., 332., 248., 84., 200., 55., 85., 89.,\n",
" 31., 129., 83., 275., 65., 198., 236., 253., 124., 44., 172.,\n",
" 114., 142., 109., 180., 144., 163., 147., 97., 220., 190., 109.,\n",
" 191., 122., 230., 242., 248., 249., 192., 131., 237., 78., 135.,\n",
" 244., 199., 270., 164., 72., 96., 306., 91., 214., 95., 216.,\n",
" 263., 178., 113., 200., 139., 139., 88., 148., 88., 243., 71.,\n",
" 77., 109., 272., 60., 54., 221., 90., 311., 281., 182., 321.,\n",
" 58., 262., 206., 233., 242., 123., 167., 63., 197., 71., 168.,\n",
" 140., 217., 121., 235., 245., 40., 52., 104., 132., 88., 69.,\n",
" 219., 72., 201., 110., 51., 277., 63., 118., 69., 273., 258.,\n",
" 43., 198., 242., 232., 175., 93., 168., 275., 293., 281., 72.,\n",
" 140., 189., 181., 209., 136., 261., 113., 131., 174., 257., 55.,\n",
" 84., 42., 146., 212., 233., 91., 111., 152., 120., 67., 310.,\n",
" 94., 183., 66., 173., 72., 49., 64., 48., 178., 104., 132.,\n",
" 220., 57.]),\n",
" 'DESCR': '.. _diabetes_dataset:\\n\\nDiabetes dataset\\n----------------\\n\\nTen baseline variables, age, sex, body mass index, average blood\\npressure, and six blood serum measurements were obtained for each of n =\\n442 diabetes patients, as well as the response of interest, a\\nquantitative measure of disease progression one year after baseline.\\n\\n**Data Set Characteristics:**\\n\\n :Number of Instances: 442\\n\\n :Number of Attributes: First 10 columns are numeric predictive values\\n\\n :Target: Column 11 is a quantitative measure of disease progression one year after baseline\\n\\n :Attribute Information:\\n - Age\\n - Sex\\n - Body mass index\\n - Average blood pressure\\n - S1\\n - S2\\n - S3\\n - S4\\n - S5\\n - S6\\n\\nNote: Each of these 10 feature variables have been mean centered and scaled by the standard deviation times `n_samples` (i.e. the sum of squares of each column totals 1).\\n\\nSource URL:\\nhttp://www4.stat.ncsu.edu/~boos/var.select/diabetes.html\\n\\nFor more information see:\\nBradley Efron, Trevor Hastie, Iain Johnstone and Robert Tibshirani (2004) \"Least Angle Regression,\" Annals of Statistics (with discussion), 407-499.\\n(http://web.stanford.edu/~hastie/Papers/LARS/LeastAngle_2002.pdf)',\n",
" 'feature_names': ['age',\n",
" 'sex',\n",
" 'bmi',\n",
" 'bp',\n",
" 's1',\n",
" 's2',\n",
" 's3',\n",
" 's4',\n",
" 's5',\n",
" 's6'],\n",
" 'data_filename': '/home/iudh/.local/lib/python3.7/site-packages/sklearn/datasets/data/diabetes_data.csv.gz',\n",
" 'target_filename': '/home/iudh/.local/lib/python3.7/site-packages/sklearn/datasets/data/diabetes_target.csv.gz'}"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from sklearn import datasets\n",
"diabetesDataset = datasets.load_diabetes()\n",
"diabetesDataset"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This data structure contains 5 different components:\n",
"\n",
" The data, which is a numpy array with 442 rows and 10 columns\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0.03807591, 0.05068012, 0.06169621, ..., -0.00259226,\n",
" 0.01990842, -0.01764613],\n",
" [-0.00188202, -0.04464164, -0.05147406, ..., -0.03949338,\n",
" -0.06832974, -0.09220405],\n",
" [ 0.08529891, 0.05068012, 0.04445121, ..., -0.00259226,\n",
" 0.00286377, -0.02593034],\n",
" ...,\n",
" [ 0.04170844, 0.05068012, -0.01590626, ..., -0.01107952,\n",
" -0.04687948, 0.01549073],\n",
" [-0.04547248, -0.04464164, 0.03906215, ..., 0.02655962,\n",
" 0.04452837, -0.02593034],\n",
" [-0.04547248, -0.04464164, -0.0730303 , ..., -0.03949338,\n",
" -0.00421986, 0.00306441]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"diabetesDataset.data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" The target (this is the variable that we would like to predict) which is a one dimensional array with 442 rows."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([151., 75., 141., 206., 135., 97., 138., 63., 110., 310., 101.,\n",
" 69., 179., 185., 118., 171., 166., 144., 97., 168., 68., 49.,\n",
" 68., 245., 184., 202., 137., 85., 131., 283., 129., 59., 341.,\n",
" 87., 65., 102., 265., 276., 252., 90., 100., 55., 61., 92.,\n",
" 259., 53., 190., 142., 75., 142., 155., 225., 59., 104., 182.,\n",
" 128., 52., 37., 170., 170., 61., 144., 52., 128., 71., 163.,\n",
" 150., 97., 160., 178., 48., 270., 202., 111., 85., 42., 170.,\n",
" 200., 252., 113., 143., 51., 52., 210., 65., 141., 55., 134.,\n",
" 42., 111., 98., 164., 48., 96., 90., 162., 150., 279., 92.,\n",
" 83., 128., 102., 302., 198., 95., 53., 134., 144., 232., 81.,\n",
" 104., 59., 246., 297., 258., 229., 275., 281., 179., 200., 200.,\n",
" 173., 180., 84., 121., 161., 99., 109., 115., 268., 274., 158.,\n",
" 107., 83., 103., 272., 85., 280., 336., 281., 118., 317., 235.,\n",
" 60., 174., 259., 178., 128., 96., 126., 288., 88., 292., 71.,\n",
" 197., 186., 25., 84., 96., 195., 53., 217., 172., 131., 214.,\n",
" 59., 70., 220., 268., 152., 47., 74., 295., 101., 151., 127.,\n",
" 237., 225., 81., 151., 107., 64., 138., 185., 265., 101., 137.,\n",
" 143., 141., 79., 292., 178., 91., 116., 86., 122., 72., 129.,\n",
" 142., 90., 158., 39., 196., 222., 277., 99., 196., 202., 155.,\n",
" 77., 191., 70., 73., 49., 65., 263., 248., 296., 214., 185.,\n",
" 78., 93., 252., 150., 77., 208., 77., 108., 160., 53., 220.,\n",
" 154., 259., 90., 246., 124., 67., 72., 257., 262., 275., 177.,\n",
" 71., 47., 187., 125., 78., 51., 258., 215., 303., 243., 91.,\n",
" 150., 310., 153., 346., 63., 89., 50., 39., 103., 308., 116.,\n",
" 145., 74., 45., 115., 264., 87., 202., 127., 182., 241., 66.,\n",
" 94., 283., 64., 102., 200., 265., 94., 230., 181., 156., 233.,\n",
" 60., 219., 80., 68., 332., 248., 84., 200., 55., 85., 89.,\n",
" 31., 129., 83., 275., 65., 198., 236., 253., 124., 44., 172.,\n",
" 114., 142., 109., 180., 144., 163., 147., 97., 220., 190., 109.,\n",
" 191., 122., 230., 242., 248., 249., 192., 131., 237., 78., 135.,\n",
" 244., 199., 270., 164., 72., 96., 306., 91., 214., 95., 216.,\n",
" 263., 178., 113., 200., 139., 139., 88., 148., 88., 243., 71.,\n",
" 77., 109., 272., 60., 54., 221., 90., 311., 281., 182., 321.,\n",
" 58., 262., 206., 233., 242., 123., 167., 63., 197., 71., 168.,\n",
" 140., 217., 121., 235., 245., 40., 52., 104., 132., 88., 69.,\n",
" 219., 72., 201., 110., 51., 277., 63., 118., 69., 273., 258.,\n",
" 43., 198., 242., 232., 175., 93., 168., 275., 293., 281., 72.,\n",
" 140., 189., 181., 209., 136., 261., 113., 131., 174., 257., 55.,\n",
" 84., 42., 146., 212., 233., 91., 111., 152., 120., 67., 310.,\n",
" 94., 183., 66., 173., 72., 49., 64., 48., 178., 104., 132.,\n",
" 220., 57.])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"diabetesDataset.target"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
" The description of the dataset\n",
"\n",
" The data filename (this contains the file path)\n",
"\n",
" The target filename (also contains a file path)\n",
"\n",
"While the library contains preloaded datasets, we can use Pandas dataframes as well in scikit-learn. One major point is that when using a Pandas dataframe to perform a machine learning task with scikit-learn, we must separate the dataset into predictor variables and response variables. The predictor variables are used to estimate the response variable (or target variable).\n",
"\n",
"\n",
"# Preprocess Dataset\n",
"\n",
"In previous lessons, we have mentioned a number of data preprocessing functions including scaling and test train splitting. Scikit-learn has many data preprocessing functions that are geared towards numpy arrays.\n",
"\n",
"\n",
"# Feature Selection\n",
"\n",
"This is another stage of our Machine Learning workflow for which Scikit-Learn provides support. As mentioned previously on this course, there are multiple ways to select the best features for our model. Scikit-learn comes with a number of functions that help us perform this task.\n",
"\n",
"\n",
"# Train a Model\n",
"\n",
"Model-training is the core functionality of Scikit-Learn. It provides a variety of Machine Learning algorithms grouped according to the availability of the teaching signal (availability of supervision/criticism) criterion. In previous lessons, the interpretation of a training set as a set of solved problems was introduced. If a supervisor provided the solutions, then we can perform supervised Machine Learning. If no solutions are available then we are restricted to unsupervised Machine Learning.\n",
"\n",
"\n",
"# Model Selection and Evaluation\n",
"\n",
"Scikit-Learn test and tuning functionalities include the functionalities that were introduced in the previous lessons. These functionalities are available as functions from the sklearn.metrics package. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading