%% Generated by Sphinx. \def\sphinxdocclass{report} \documentclass[letterpaper,10pt,english]{sphinxmanual} \ifdefined\pdfpxdimen \let\sphinxpxdimen\pdfpxdimen\else\newdimen\sphinxpxdimen \fi \sphinxpxdimen=.75bp\relax \PassOptionsToPackage{warn}{textcomp} \usepackage[utf8]{inputenc} \ifdefined\DeclareUnicodeCharacter % support both utf8 and utf8x syntaxes \ifdefined\DeclareUnicodeCharacterAsOptional \def\sphinxDUC#1{\DeclareUnicodeCharacter{"#1}} \else \let\sphinxDUC\DeclareUnicodeCharacter \fi \sphinxDUC{00A0}{\nobreakspace} \sphinxDUC{2500}{\sphinxunichar{2500}} \sphinxDUC{2502}{\sphinxunichar{2502}} \sphinxDUC{2514}{\sphinxunichar{2514}} \sphinxDUC{251C}{\sphinxunichar{251C}} \sphinxDUC{2572}{\textbackslash} \fi \usepackage{cmap} \usepackage[T1]{fontenc} \usepackage{amsmath,amssymb,amstext} \usepackage{babel} \usepackage{times} \expandafter\ifx\csname T@LGR\endcsname\relax \else % LGR was declared as font encoding \substitutefont{LGR}{\rmdefault}{cmr} \substitutefont{LGR}{\sfdefault}{cmss} \substitutefont{LGR}{\ttdefault}{cmtt} \fi \expandafter\ifx\csname T@X2\endcsname\relax \expandafter\ifx\csname T@T2A\endcsname\relax \else % T2A was declared as font encoding \substitutefont{T2A}{\rmdefault}{cmr} \substitutefont{T2A}{\sfdefault}{cmss} \substitutefont{T2A}{\ttdefault}{cmtt} \fi \else % X2 was declared as font encoding \substitutefont{X2}{\rmdefault}{cmr} \substitutefont{X2}{\sfdefault}{cmss} \substitutefont{X2}{\ttdefault}{cmtt} \fi \usepackage[Bjarne]{fncychap} \usepackage[,numfigreset=1,mathnumfig]{sphinx} \fvset{fontsize=\small} \usepackage{geometry} % Include hyperref last. \usepackage{hyperref} % Fix anchor placement for figures with captions. \usepackage{hypcap}% it must be loaded after hyperref. % Set up styles of URL: it should be placed after hyperref. \urlstyle{same} \usepackage{sphinxmessages} \title{Applied Machine Learning and Data Analysis} \date{Sep 20, 2020} \release{} \author{Morten Hjorth\sphinxhyphen{}Jensen} \newcommand{\sphinxlogo}{\vbox{}} \renewcommand{\releasename}{} \makeindex \begin{document} \pagestyle{empty} \sphinxmaketitle \pagestyle{plain} \sphinxtableofcontents \pagestyle{normal} \phantomsection\label{\detokenize{chapter1::doc}} \sphinxstylestrong{Morten Hjorth\sphinxhyphen{}Jensen}, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University Date: \sphinxstylestrong{Sep 19, 2020} Copyright 1999\sphinxhyphen{}2020, Morten Hjorth\sphinxhyphen{}Jensen. Released under CC Attribution\sphinxhyphen{}NonCommercial 4.0 license \chapter{Introduction} \label{\detokenize{chapter1:introduction}} During the last two decades there has been a swift and amazing development of Machine Learning techniques and algorithms that impact many areas in not only Science and Technology but also the Humanities, Social Sciences, Medicine, Law, indeed, almost all possible disciplines. The applications are incredibly many, from self\sphinxhyphen{}driving cars to solving high\sphinxhyphen{}dimensional differential equations or complicated quantum mechanical many\sphinxhyphen{}body problems. Machine Learning is perceived by many as one of the main disruptive techniques nowadays. Statistics, Data science and Machine Learning form important fields of research in modern science. They describe how to learn and make predictions from data, as well as allowing us to extract important correlations about physical process and the underlying laws of motion in large data sets. The latter, big data sets, appear frequently in essentially all disciplines, from the traditional Science, Technology, Mathematics and Engineering fields to Life Science, Law, education research, the Humanities and the Social Sciences. It has become more and more common to see research projects on big data in for example the Social Sciences where extracting patterns from complicated survey data is one of many research directions. Having a solid grasp of data analysis and machine learning is thus becoming central to scientific computing in many fields, and competences and skills within the fields of machine learning and scientific computing are nowadays strongly requested by many potential employers. The latter cannot be overstated, familiarity with machine learning has almost become a prerequisite for many of the most exciting employment opportunities, whether they are in bioinformatics, life science, physics or finance, in the private or the public sector. This author has had several students or met students who have been hired recently based on their skills and competences in scientific computing and data science, often with marginal knowledge of machine learning. Machine learning is a subfield of computer science, and is closely related to computational statistics. It evolved from the study of pattern recognition in artificial intelligence (AI) research, and has made contributions to AI tasks like computer vision, natural language processing and speech recognition. Many of the methods we will study are also strongly rooted in basic mathematics and physics research. Ideally, machine learning represents the science of giving computers the ability to learn without being explicitly programmed. The idea is that there exist generic algorithms which can be used to find patterns in a broad class of data sets without having to write code specifically for each problem. The algorithm will build its own logic based on the data. You should however always keep in mind that machines and algorithms are to a large extent developed by humans. The insights and knowledge we have about a specific system, play a central role when we develop a specific machine learning algorithm. Machine learning is an extremely rich field, in spite of its young age. The increases we have seen during the last three decades in computational capabilities have been followed by developments of methods and techniques for analyzing and handling large date sets, relying heavily on statistics, computer science and mathematics. The field is rather new and developing rapidly. Popular software packages written in Python for machine learning like \sphinxhref{http://scikit-learn.org/stable/}{Scikit\sphinxhyphen{}learn}, \sphinxhref{https://www.tensorflow.org/}{Tensorflow}, \sphinxhref{http://pytorch.org/}{PyTorch} and \sphinxhref{https://keras.io/}{Keras}, all freely available at their respective GitHub sites, encompass communities of developers in the thousands or more. And the number of code developers and contributors keeps increasing. Not all the algorithms and methods can be given a rigorous mathematical justification, opening up thereby large rooms for experimenting and trial and error and thereby exciting new developments. However, a solid command of linear algebra, multivariate theory, probability theory, statistical data analysis, understanding errors and Monte Carlo methods are central elements in a proper understanding of many of algorithms and methods we will discuss. \chapter{Learning outcomes} \label{\detokenize{chapter1:learning-outcomes}} These sets of lectures aim at giving you an overview of central aspects of statistical data analysis as well as some of the central algorithms used in machine learning. We will introduce a variety of central algorithms and methods essential for studies of data analysis and machine learning. Hands\sphinxhyphen{}on projects and experimenting with data and algorithms plays a central role in these lectures, and our hope is, through the various projects and exercises, to expose you to fundamental research problems in these fields, with the aim to reproduce state of the art scientific results. You will learn to develop and structure codes for studying these systems, get acquainted with computing facilities and learn to handle large scientific projects. A good scientific and ethical conduct is emphasized throughout the course. More specifically, you will \begin{enumerate} \sphinxsetlistlabels{\arabic}{enumi}{enumii}{}{.}% \item {} Learn about basic data analysis, Bayesian statistics, Monte Carlo methods, data optimization and machine learning; \item {} Be capable of extending the acquired knowledge to other systems and cases; \item {} Have an understanding of central algorithms used in data analysis and machine learning; \item {} Gain knowledge of central aspects of Monte Carlo methods, Markov chains, Gibbs samplers and their possible applications, from numerical integration to simulation of stock markets; \item {} Understand methods for regression and classification; \item {} Learn about neural network, genetic algorithms and Boltzmann machines; \item {} Work on numerical projects to illustrate the theory. The projects play a central role and you are expected to know modern programming languages like Python or C++, in addition to a basic knowledge of linear algebra (typically taught during the first one or two years of undergraduate studies). \end{enumerate} There are several topics we will cover here, spanning from statistical data analysis and its basic concepts such as expectation values, variance, covariance, correlation functions and errors, via well\sphinxhyphen{}known probability distribution functions like the uniform distribution, the binomial distribution, the Poisson distribution and simple and multivariate normal distributions to central elements of Bayesian statistics and modeling. We will also remind the reader about central elements from linear algebra and standard methods based on linear algebra used to optimize (minimize) functions (the family of gradient descent methods) and the Singular\sphinxhyphen{}value decomposition and least square methods for parameterizing data. We will also cover Monte Carlo methods, Markov chains, well\sphinxhyphen{}known algorithms for sampling stochastic events like the Metropolis\sphinxhyphen{}Hastings and Gibbs sampling methods. An important aspect of all our calculations is a proper estimation of errors. Here we will also discuss famous resampling techniques like the blocking, the bootstrapping and the jackknife methods and the infamous bias\sphinxhyphen{}variance tradeoff. The second part of the material covers several algorithms used in machine learning. \chapter{Machine Learning, a small (and probably biased) introduction} \label{\detokenize{chapter1:machine-learning-a-small-and-probably-biased-introduction}} Ideally, machine learning represents the science of giving computers the ability to learn without being explicitly programmed. The idea is that there exist generic algorithms which can be used to find patterns in a broad class of data sets without having to write code specifically for each problem. The algorithm will build its own logic based on the data. You should however always keep in mind that machines and algorithms are to a large extent developed by humans. The insights and knowledge we have about a specific system, play a central role when we develop a specific machine learning algorithm. \chapter{Machine Learning, an extremely rich field} \label{\detokenize{chapter1:machine-learning-an-extremely-rich-field}} Machine learning is an extremely rich field, in spite of its young age. The increases we have seen during the last decades in computational capabilities have been followed by developments of methods and techniques for analyzing and handling large date sets, relying heavily on statistics, computer science and mathematics. The field is rather new and developing rapidly. Popular software libraries written in Python for machine learning like \sphinxhref{http://scikit-learn.org/stable/}{Scikit\sphinxhyphen{}learn}, \sphinxhref{https://www.tensorflow.org/}{Tensorflow}, \sphinxhref{http://pytorch.org/}{PyTorch} and \sphinxhref{https://keras.io/}{Keras}, all freely available at their respective GitHub sites, encompass communities of developers in the thousands or more. And the number of code developers and contributors keeps increasing. \chapter{A multidisciplinary approach} \label{\detokenize{chapter1:a-multidisciplinary-approach}} Not all the algorithms and methods can be given a rigorous mathematical justification (for example decision trees and random forests), opening up thereby large rooms for experimenting and trial and error and thereby exciting new developments. However, a solid command of linear algebra, multivariate theory, probability theory, statistical data analysis, understanding errors and Monte Carlo methods are central elements in a proper understanding of many of the algorithms and methods we will discuss. \chapter{Types of Machine Learning} \label{\detokenize{chapter1:types-of-machine-learning}} The approaches to machine learning are many, but are often split into two main categories. In \sphinxstyleemphasis{supervised learning} we know the answer to a problem, and let the computer deduce the logic behind it. On the other hand, \sphinxstyleemphasis{unsupervised learning} is a method for finding patterns and relationship in data sets without any prior knowledge of the system. Some authours also operate with a third category, namely \sphinxstyleemphasis{reinforcement learning}. This is a paradigm of learning inspired by behavioral psychology, where learning is achieved by trial\sphinxhyphen{}and\sphinxhyphen{}error, solely from rewards and punishment. Another way to categorize machine learning tasks is to consider the desired output of a system. Some of the most common tasks are: \begin{itemize} \item {} Classification: Outputs are divided into two or more classes. The goal is to produce a model that assigns inputs into one of these classes. An example is to identify digits based on pictures of hand\sphinxhyphen{}written ones. Classification is often supervised learning. \item {} Regression: Finding a functional relationship between an input data set and a reference data set. The goal is to construct a function that maps input data to continuous output values. \item {} Clustering: Data are divided into groups with certain common traits, without knowing the different groups beforehand. It is thus a form of unsupervised learning. \end{itemize} \chapter{Essential elements of ML} \label{\detokenize{chapter1:essential-elements-of-ml}} The methods we cover have three main topics in common, irrespective of whether we deal with supervised or unsupervised learning. \begin{itemize} \item {} The first ingredient is normally our data set (which can be subdivided into training, validation and test data). Many find the most difficult part of using Machine Learning to be the set up of your data in a meaningful way. \item {} The second item is a model which is normally a function of some parameters. The model reflects our knowledge of the system (or lack thereof). As an example, if we know that our data show a behavior similar to what would be predicted by a polynomial, fitting our data to a polynomial of some degree would then determin our model. \item {} The last ingredient is a so\sphinxhyphen{}called \sphinxstylestrong{cost/loss} function (or error function) which allows us to present an estimate on how good our model is in reproducing the data it is supposed to train. \end{itemize} \chapter{An optimization/minimization problem} \label{\detokenize{chapter1:an-optimization-minimization-problem}} At the heart of basically all Machine Learning algorithms we will encounter so\sphinxhyphen{}called minimization or optimization algorithms. A large family of such methods are so\sphinxhyphen{}called \sphinxstylestrong{gradient methods}. \chapter{A Frequentist approach to data analysis} \label{\detokenize{chapter1:a-frequentist-approach-to-data-analysis}} When you hear phrases like \sphinxstylestrong{predictions and estimations} and \sphinxstylestrong{correlations and causations}, what do you think of? May be you think of the difference between classifying new data points and generating new data points. Or perhaps you consider that correlations represent some kind of symmetric statements like if \(A\) is correlated with \(B\), then \(B\) is correlated with \(A\). Causation on the other hand is directional, that is if \(A\) causes \(B\), \(B\) does not necessarily cause \(A\). These concepts are in some sense the difference between machine learning and statistics. In machine learning and prediction based tasks, we are often interested in developing algorithms that are capable of learning patterns from given data in an automated fashion, and then using these learned patterns to make predictions or assessments of newly given data. In many cases, our primary concern is the quality of the predictions or assessments, and we are less concerned about the underlying patterns that were learned in order to make these predictions. In machine learning we normally use \sphinxhref{https://en.wikipedia.org/wiki/Frequentist\_inference}{a so\sphinxhyphen{}called frequentist approach}, where the aim is to make predictions and find correlations. We focus less on for example extracting a probability distribution function (PDF). The PDF can be used in turn to make estimations and find causations such as given \(A\) what is the likelihood of finding \(B\). \chapter{What is a good model?} \label{\detokenize{chapter1:what-is-a-good-model}} In science and engineering we often end up in situations where we want to infer (or learn) a quantitative model \(M\) for a given set of sample points \(\boldsymbol{X} \in [x_1, x_2,\dots x_N]\). As we will see repeatedely in these lectures, we could try to fit these data points to a model given by a straight line, or if we wish to be more sophisticated to a more complex function. The reason for inferring such a model is that it serves many useful purposes. On the one hand, the model can reveal information encoded in the data or underlying mechanisms from which the data were generated. For instance, we could discover important corelations that relate interesting physics interpretations. In addition, it can simplify the representation of the given data set and help us in making predictions about future data samples. A first important consideration to keep in mind is that inferring the \sphinxstyleemphasis{correct} model for a given data set is an elusive, if not impossible, task. The fundamental difficulty is that if we are not specific about what we mean by a \sphinxstyleemphasis{correct} model, there could easily be many different models that fit the given data set \sphinxstyleemphasis{equally well}. \chapter{What is a good model? Can we define it?} \label{\detokenize{chapter1:what-is-a-good-model-can-we-define-it}} The central question is this: what leads us to say that a model is correct or optimal for a given data set? To make the model inference problem well posed, i.e., to guarantee that there is a unique optimal model for the given data, we need to impose additional assumptions or restrictions on the class of models considered. To this end, we should not be looking for just any model that can describe the data. Instead, we should look for a \sphinxstylestrong{model} \(M\) that is the best among a restricted class of models. In addition, to make the model inference problem computationally tractable, we need to specify how restricted the class of models needs to be. A common strategy is to start with the simplest possible class of models that is just necessary to describe the data or solve the problem at hand. More precisely, the model class should be rich enough to contain at least one model that can fit the data to a desired accuracy and yet be restricted enough that it is relatively simple to find the best model for the given data. Thus, the most popular strategy is to start from the simplest class of models and increase the complexity of the models only when the simpler models become inadequate. For instance, if we work with a regression problem to fit a set of sample points, one may first try the simplest class of models, namely linear models, followed obviously by more complex models. How to evaluate which model fits best the data is something we will come back to over and over again in these set of lectures. \chapter{Choice of Programming Language} \label{\detokenize{chapter1:choice-of-programming-language}} Python plays nowadays a central role in the development of machine learning techniques and tools for data analysis. In particular, seen the wealth of machine learning and data analysis libraries written in Python, easy to use libraries with immediate visualization(and not the least impressive galleries of existing examples), the popularity of the Jupyter notebook framework with the possibility to run \sphinxstylestrong{R} codes or compiled programs written in C++, and much more made our choice of programming language for this series of lectures easy. However, since the focus here is not only on using existing Python libraries such as \sphinxstylestrong{Scikit\sphinxhyphen{}Learn}, \sphinxstylestrong{Tensorflow} and \sphinxstylestrong{Pytorch}, but also on developing your own algorithms and codes, we will as far as possible present many of these algorithms either as a Python codes or C++ or Fortran (or other languages) codes. \chapter{Data handling, machine learning and ethical aspects} \label{\detokenize{chapter1:data-handling-machine-learning-and-ethical-aspects}} In most of the cases we will study, we will either generate the data to analyze ourselves (both for supervised learning and unsupervised learning) or we will recur again and again to data present in say \sphinxstylestrong{Scikit\sphinxhyphen{}Learn} or \sphinxstylestrong{Tensorflow}. Many of the examples we end up dealing with are from a privacy and data protection point of view, rather inoccuous and boring results of numerical calculations. However, this does not hinder us from developing a sound ethical attitude to the data we use, how we analyze the data and how we handle the data. The most immediate and simplest possible ethical aspects deal with our approach to the scientific process. Nowadays, with version control software like \sphinxhref{https://git-scm.com/}{Git} and various online repositories like \sphinxhref{https://github.com/}{Github}, \sphinxhref{https://about.gitlab.com/}{Gitlab} etc, we can easily make our codes and data sets we have used, freely and easily accessible to a wider community. This helps us almost automagically in making our science reproducible. The large open\sphinxhyphen{}source development communities involved in say \sphinxhref{http://scikit-learn.org/stable/}{Scikit\sphinxhyphen{}Learn}, \sphinxhref{https://www.tensorflow.org/}{Tensorflow}, \sphinxhref{http://pytorch.org/}{PyTorch} and \sphinxhref{https://keras.io/}{Keras}, are all excellent examples of this. The codes can be tested and improved upon continuosly, helping thereby our scientific community at large in developing data analysis and machine learning tools. It is much easier today to gain traction and acceptance for making your science reproducible. From a societal stand, this is an important element since many of the developers are employees of large public institutions like universities and research labs. Our fellow taxpayers do deserve to get something back for their bucks. However, this more mechanical aspect of the ethics of science (in particular the reproducibility of scientific results) is something which is obvious and everybody should do so as part of the dialectics of science. The fact that many scientists are not willing to share their codes or data is detrimental to the scientific discourse. Before we proceed, we should add a disclaimer. Even though we may dream of computers developing some kind of higher learning capabilities, at the end (even if the artificial intelligence community keeps touting our ears full of fancy futuristic avenues), it is we, yes you reading these lines, who end up constructing and instructing, via various algorithms, the machine learning approaches. Self\sphinxhyphen{}driving cars for example, rely on sofisticated programs which take into account all possible situations a car can encounter. In addition, extensive usage of training data from GPS information, maps etc, are typically fed into the software for self\sphinxhyphen{}driving cars. Adding to this various sensors and cameras that feed information to the programs, there are zillions of ethical issues which arise from this. For self\sphinxhyphen{}driving cars, where basically many of the standard machine learning algorithms discussed here enter into the codes, at a certain stage we have to make choices. Yes, we , the lads and lasses who wrote a program for a specific brand of a self\sphinxhyphen{}driving car. As an example, all carmakers have as their utmost priority the security of the driver and the accompanying passengers. A famous European carmaker, which is one of the leaders in the market of self\sphinxhyphen{}driving cars, had \sphinxstylestrong{if} statements of the following type: suppose there are two obstacles in front of you and you cannot avoid to collide with one of them. One of the obstacles is a monstertruck while the other one is a kindergarten class trying to cross the road. The self\sphinxhyphen{}driving car algo would then opt for the hitting the small folks instead of the monstertruck, since the likelihood of surving a collision with our future citizens, is much higher. This leads to serious ethical aspects. Why should we opt for such an option? Who decides and who is entitled to make such choices? Keep in mind that many of the algorithms you will encounter in this series of lectures or hear about later, are indeed based on simple programming instructions. And you are very likely to be one of the people who may end up writing such a code. Thus, developing a sound ethical attitude to what we do, an approach well beyond the simple mechanistic one of making our science available and reproducible, is much needed. The example of the self\sphinxhyphen{}driving cars is just one of infinitely many cases where we have to make choices. When you analyze data on economic inequalities, who guarantees that you are not weighting some data in a particular way, perhaps because you dearly want a specific conclusion which may support your political views? Or what about the recent claims that a famous IT company like Apple has a sexist bias on the their recently \sphinxhref{https://qz.com/1748321/the-role-of-goldman-sachs-algorithms-in-the-apple-credit-card-scandal/}{launched credit card}? We do not have the answers here, nor will we venture into a deeper discussions of these aspects, but we want you think over these topics in a more overarching way. A statistical data analysis with its dry numbers and graphs meant to guide the eye, does not necessarily reflect the truth, whatever that is. As a scientist, and after a university education, you are supposedly a better citizen, with an improved critical view and understanding of the scientific method, and perhaps some deeper understanding of the ethics of science at large. Use these insights. Be a critical citizen. You owe it to our society. \section{Data Analysis and Machine Learning: Getting started, our first data and Machine Learning encounters} \label{\detokenize{chapter2:data-analysis-and-machine-learning-getting-started-our-first-data-and-machine-learning-encounters}}\label{\detokenize{chapter2::doc}} \sphinxstylestrong{Morten Hjorth\sphinxhyphen{}Jensen}, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University Date: \sphinxstylestrong{Dec 25, 2019} Copyright 1999\sphinxhyphen{}2019, Morten Hjorth\sphinxhyphen{}Jensen. Released under CC Attribution\sphinxhyphen{}NonCommercial 4.0 license \subsection{Introduction} \label{\detokenize{chapter2:introduction}} Our emphasis throughout this series of lecturesis on understanding the mathematical aspects of different algorithms used in the fields of data analysis and machine learning. However, where possible we will emphasize the importance of using available software. We start thus with a hands\sphinxhyphen{}on and top\sphinxhyphen{}down approach to machine learning. The aim is thus to start with relevant data or data we have produced and use these to introduce statistical data analysis concepts and machine learning algorithms before we delve into the algorithms themselves. The examples we will use in the beginning, start with simple polynomials with random noise added. We will use the Python software package \sphinxhref{http://scikit-learn.org/stable/}{Scikit\sphinxhyphen{}Learn} and introduce various machine learning algorithms to make fits of the data and predictions. We move thereafter to more interesting cases such as data from say experiments (below we will look at experimental nuclear binding energies as an example). These are examples where we can easily set up the data and then use machine learning algorithms included in for example \sphinxstylestrong{Scikit\sphinxhyphen{}Learn}. These examples will serve us the purpose of getting started. Furthermore, they allow us to catch more than two birds with a stone. They will allow us to bring in some programming specific topics and tools as well as showing the power of various Python libraries for machine learning and statistical data analysis. Here, we will mainly focus on two specific Python packages for Machine Learning, Scikit\sphinxhyphen{}Learn and Tensorflow (see below for links etc). Moreover, the examples we introduce will serve as inputs to many of our discussions later, as well as allowing you to set up models and produce your own data and get started with programming. \subsection{What is Machine Learning?} \label{\detokenize{chapter2:what-is-machine-learning}} Statistics, data science and machine learning form important fields of research in modern science. They describe how to learn and make predictions from data, as well as allowing us to extract important correlations about physical process and the underlying laws of motion in large data sets. The latter, big data sets, appear frequently in essentially all disciplines, from the traditional Science, Technology, Mathematics and Engineering fields to Life Science, Law, education research, the Humanities and the Social Sciences. It has become more and more common to see research projects on big data in for example the Social Sciences where extracting patterns from complicated survey data is one of many research directions. Having a solid grasp of data analysis and machine learning is thus becoming central to scientific computing in many fields, and competences and skills within the fields of machine learning and scientific computing are nowadays strongly requested by many potential employers. The latter cannot be overstated, familiarity with machine learning has almost become a prerequisite for many of the most exciting employment opportunities, whether they are in bioinformatics, life science, physics or finance, in the private or the public sector. This author has had several students or met students who have been hired recently based on their skills and competences in scientific computing and data science, often with marginal knowledge of machine learning. Machine learning is a subfield of computer science, and is closely related to computational statistics. It evolved from the study of pattern recognition in artificial intelligence (AI) research, and has made contributions to AI tasks like computer vision, natural language processing and speech recognition. Many of the methods we will study are also strongly rooted in basic mathematics and physics research. Ideally, machine learning represents the science of giving computers the ability to learn without being explicitly programmed. The idea is that there exist generic algorithms which can be used to find patterns in a broad class of data sets without having to write code specifically for each problem. The algorithm will build its own logic based on the data. You should however always keep in mind that machines and algorithms are to a large extent developed by humans. The insights and knowledge we have about a specific system, play a central role when we develop a specific machine learning algorithm. Machine learning is an extremely rich field, in spite of its young age. The increases we have seen during the last three decades in computational capabilities have been followed by developments of methods and techniques for analyzing and handling large date sets, relying heavily on statistics, computer science and mathematics. The field is rather new and developing rapidly. Popular software packages written in Python for machine learning like \sphinxhref{http://scikit-learn.org/stable/}{Scikit\sphinxhyphen{}learn}, \sphinxhref{https://www.tensorflow.org/}{Tensorflow}, \sphinxhref{http://pytorch.org/}{PyTorch} and \sphinxhref{https://keras.io/}{Keras}, all freely available at their respective GitHub sites, encompass communities of developers in the thousands or more. And the number of code developers and contributors keeps increasing. Not all the algorithms and methods can be given a rigorous mathematical justification, opening up thereby large rooms for experimenting and trial and error and thereby exciting new developments. However, a solid command of linear algebra, multivariate theory, probability theory, statistical data analysis, understanding errors and Monte Carlo methods are central elements in a proper understanding of many of algorithms and methods we will discuss. \subsection{Types of Machine Learning} \label{\detokenize{chapter2:types-of-machine-learning}} The approaches to machine learning are many, but are often split into two main categories. In \sphinxstyleemphasis{supervised learning} we know the answer to a problem, and let the computer deduce the logic behind it. On the other hand, \sphinxstyleemphasis{unsupervised learning} is a method for finding patterns and relationship in data sets without any prior knowledge of the system. Some authours also operate with a third category, namely \sphinxstyleemphasis{reinforcement learning}. This is a paradigm of learning inspired by behavioral psychology, where learning is achieved by trial\sphinxhyphen{}and\sphinxhyphen{}error, solely from rewards and punishment. Another way to categorize machine learning tasks is to consider the desired output of a system. Some of the most common tasks are: \begin{itemize} \item {} Classification: Outputs are divided into two or more classes. The goal is to produce a model that assigns inputs into one of these classes. An example is to identify digits based on pictures of hand\sphinxhyphen{}written ones. Classification is typically supervised learning. \item {} Regression: Finding a functional relationship between an input data set and a reference data set. The goal is to construct a function that maps input data to continuous output values. \item {} Clustering: Data are divided into groups with certain common traits, without knowing the different groups beforehand. It is thus a form of unsupervised learning. \end{itemize} The methods we cover have three main topics in common, irrespective of whether we deal with supervised or unsupervised learning. The first ingredient is normally our data set (which can be subdivided into training and test data), the second item is a model which is normally a function of some parameters. The model reflects our knowledge of the system (or lack thereof). As an example, if we know that our data show a behavior similar to what would be predicted by a polynomial, fitting our data to a polynomial of some degree would then determin our model. The last ingredient is a so\sphinxhyphen{}called \sphinxstylestrong{cost} function which allows us to present an estimate on how good our model is in reproducing the data it is supposed to train.At the heart of basically all ML algorithms there are so\sphinxhyphen{}called minimization algorithms, often we end up with various variants of \sphinxstylestrong{gradient} methods. \subsection{Software and needed installations} \label{\detokenize{chapter2:software-and-needed-installations}} We will make extensive use of Python as programming language and its myriad of available libraries. You will find Jupyter notebooks invaluable in your work. You can run \sphinxstylestrong{R} codes in the Jupyter/IPython notebooks, with the immediate benefit of visualizing your data. You can also use compiled languages like C++, Rust, Julia, Fortran etc if you prefer. The focus in these lectures will be on Python. If you have Python installed (we strongly recommend Python3) and you feel pretty familiar with installing different packages, we recommend that you install the following Python packages via \sphinxstylestrong{pip} as \begin{enumerate} \sphinxsetlistlabels{\arabic}{enumi}{enumii}{}{.}% \item {} pip install numpy scipy matplotlib ipython scikit\sphinxhyphen{}learn mglearn sympy pandas pillow \end{enumerate} For Python3, replace \sphinxstylestrong{pip} with \sphinxstylestrong{pip3}. For OSX users we recommend, after having installed Xcode, to install \sphinxstylestrong{brew}. Brew allows for a seamless installation of additional software via for example \begin{enumerate} \sphinxsetlistlabels{\arabic}{enumi}{enumii}{}{.}% \item {} brew install python3 \end{enumerate} For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution, you can use \sphinxstylestrong{pip} as well and simply install Python as \begin{enumerate} \sphinxsetlistlabels{\arabic}{enumi}{enumii}{}{.}% \item {} sudo apt\sphinxhyphen{}get install python3 (or python for pyhton2.7) \end{enumerate} etc etc. \subsection{Python installers} \label{\detokenize{chapter2:python-installers}} If you don’t want to perform these operations separately and venture into the hassle of exploring how to set up dependencies and paths, we recommend two widely used distrubutions which set up all relevant dependencies for Python, namely \begin{itemize} \item {} \sphinxhref{https://docs.anaconda.com/}{Anaconda}, \end{itemize} which is an open source distribution of the Python and R programming languages for large\sphinxhyphen{}scale data processing, predictive analytics, and scientific computing, that aims to simplify package management and deployment. Package versions are managed by the package management system \sphinxstylestrong{conda}. \begin{itemize} \item {} \sphinxhref{https://www.enthought.com/product/canopy/}{Enthought canopy} \end{itemize} is a Python distribution for scientific and analytic computing distribution and analysis environment, available for free and under a commercial license. Furthermore, \sphinxhref{https://colab.research.google.com/notebooks/welcome.ipynb}{Google’s Colab} is a free Jupyter notebook environment that requires no setup and runs entirely in the cloud. Try it out! \subsection{Useful Python libraries} \label{\detokenize{chapter2:useful-python-libraries}} Here we list several useful Python libraries we strongly recommend (if you use anaconda many of these are already there) \begin{itemize} \item {} \sphinxhref{https://www.numpy.org/}{NumPy} is a highly popular library for large, multi\sphinxhyphen{}dimensional arrays and matrices, along with a large collection of high\sphinxhyphen{}level mathematical functions to operate on these arrays \item {} \sphinxhref{https://pandas.pydata.org/}{The pandas} library provides high\sphinxhyphen{}performance, easy\sphinxhyphen{}to\sphinxhyphen{}use data structures and data analysis tools \item {} \sphinxhref{http://xarray.pydata.org/en/stable/}{Xarray} is a Python package that makes working with labelled multi\sphinxhyphen{}dimensional arrays simple, efficient, and fun! \item {} \sphinxhref{https://www.scipy.org/}{Scipy} (pronounced “Sigh Pie”) is a Python\sphinxhyphen{}based ecosystem of open\sphinxhyphen{}source software for mathematics, science, and engineering. \item {} \sphinxhref{https://matplotlib.org/}{Matplotlib} is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. \item {} \sphinxhref{https://github.com/HIPS/autograd}{Autograd} can automatically differentiate native Python and Numpy code. It can handle a large subset of Python’s features, including loops, ifs, recursion and closures, and it can even take derivatives of derivatives of derivatives \item {} \sphinxhref{https://www.sympy.org/en/index.html}{SymPy} is a Python library for symbolic mathematics. \item {} \sphinxhref{https://scikit-learn.org/stable/}{scikit\sphinxhyphen{}learn} has simple and efficient tools for machine learning, data mining and data analysis \item {} \sphinxhref{https://www.tensorflow.org/}{TensorFlow} is a Python library for fast numerical computing created and released by Google \item {} \sphinxhref{https://keras.io/}{Keras} is a high\sphinxhyphen{}level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK, or Theano \item {} And many more such as \sphinxhref{https://pytorch.org/}{pytorch}, \sphinxhref{https://pypi.org/project/Theano/}{Theano} etc \end{itemize} \subsection{Installing R, C++, cython or Julia} \label{\detokenize{chapter2:installing-r-c-cython-or-julia}} You will also find it convenient to utilize \sphinxstylestrong{R}. We will mainly use Python during our lectures and in various projects and exercises. Those of you already familiar with \sphinxstylestrong{R} should feel free to continue using \sphinxstylestrong{R}, keeping however an eye on the parallel Python set ups. Similarly, if you are a Python afecionado, feel free to explore \sphinxstylestrong{R} as well. Jupyter/Ipython notebook allows you to run \sphinxstylestrong{R} codes interactively in your browser. The software library \sphinxstylestrong{R} is really tailored for statistical data analysis and allows for an easy usage of the tools and algorithms we will discuss in these lectures. To install \sphinxstylestrong{R} with Jupyter notebook \sphinxhref{https://mpacer.org/maths/r-kernel-for-ipython-notebook}{follow the link here} \subsection{Installing R, C++, cython, Numba etc} \label{\detokenize{chapter2:installing-r-c-cython-numba-etc}} For the C++ aficionados, Jupyter/IPython notebook allows you also to install C++ and run codes written in this language interactively in the browser. Since we will emphasize writing many of the algorithms yourself, you can thus opt for either Python or C++ (or Fortran or other compiled languages) as programming languages. To add more entropy, \sphinxstylestrong{cython} can also be used when running your notebooks. It means that Python with the jupyter notebook setup allows you to integrate widely popular softwares and tools for scientific computing. Similarly, the \sphinxhref{https://numba.pydata.org/}{Numba Python package} delivers increased performance capabilities with minimal rewrites of your codes. With its versatility, including symbolic operations, Python offers a unique computational environment. Your jupyter notebook can easily be converted into a nicely rendered \sphinxstylestrong{PDF} file or a Latex file for further processing. For example, convert to latex as \begin{sphinxVerbatim}[commandchars=\\\{\}] pycod jupyter nbconvert filename.ipynb \PYGZhy{}\PYGZhy{}to latex \end{sphinxVerbatim} And to add more versatility, the Python package \sphinxhref{http://www.sympy.org/en/index.html}{SymPy} is a Python library for symbolic mathematics. It aims to become a full\sphinxhyphen{}featured computer algebra system (CAS) and is entirely written in Python. Finally, if you wish to use the light mark\sphinxhyphen{}up language \sphinxhref{https://github.com/hplgit/doconce}{doconce} you can convert a standard ascii text file into various HTML formats, ipython notebooks, latex files, pdf files etc with minimal edits. These lectures were generated using \sphinxstylestrong{doconce}. \subsection{Numpy examples and Important Matrix and vector handling packages} \label{\detokenize{chapter2:numpy-examples-and-important-matrix-and-vector-handling-packages}} There are several central software libraries for linear algebra and eigenvalue problems. Several of the more popular ones have been wrapped into ofter software packages like those from the widely used text \sphinxstylestrong{Numerical Recipes}. The original source codes in many of the available packages are often taken from the widely used software package LAPACK, which follows two other popular packages developed in the 1970s, namely EISPACK and LINPACK. We describe them shortly here. \begin{itemize} \item {} LINPACK: package for linear equations and least square problems. \item {} LAPACK:package for solving symmetric, unsymmetric and generalized eigenvalue problems. From LAPACK’s website \sphinxurl{http://www.netlib.org} it is possible to download for free all source codes from this library. Both C/C++ and Fortran versions are available. \item {} BLAS (I, II and III): (Basic Linear Algebra Subprograms) are routines that provide standard building blocks for performing basic vector and matrix operations. Blas I is vector operations, II vector\sphinxhyphen{}matrix operations and III matrix\sphinxhyphen{}matrix operations. Highly parallelized and efficient codes, all available for download from \sphinxurl{http://www.netlib.org}. \end{itemize} \subsection{Basic Matrix Features} \label{\detokenize{chapter2:basic-matrix-features}} \sphinxstylestrong{Matrix properties reminder.} \begin{equation*} \begin{split} \mathbf{A} = \begin{bmatrix} a_{11} & a_{12} & a_{13} & a_{14} \\ a_{21} & a_{22} & a_{23} & a_{24} \\ a_{31} & a_{32} & a_{33} & a_{34} \\ a_{41} & a_{42} & a_{43} & a_{44} \end{bmatrix}\qquad \mathbf{I} = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \end{split} \end{equation*} The inverse of a matrix is defined by \begin{equation*} \begin{split} \mathbf{A}^{-1} \cdot \mathbf{A} = I \end{split} \end{equation*} \subsubsection{Some famous Matrices} \label{\detokenize{chapter2:some-famous-matrices}}\begin{itemize} \item {} Diagonal if \(a_{ij}=0\) for \(i\ne j\) \item {} Upper triangular if \(a_{ij}=0\) for \(i > j\) \item {} Lower triangular if \(a_{ij}=0\) for \(i < j\) \item {} Upper Hessenberg if \(a_{ij}=0\) for \(i > j+1\) \item {} Lower Hessenberg if \(a_{ij}=0\) for \(i < j+1\) \item {} Tridiagonal if \(a_{ij}=0\) for \(|i -j| > 1\) \item {} Lower banded with bandwidth \(p\): \(a_{ij}=0\) for \(i > j+p\) \item {} Upper banded with bandwidth \(p\): \(a_{ij}=0\) for \(i < j+p\) \item {} Banded, block upper triangular, block lower triangular…. \end{itemize} \subsubsection{More Basic Matrix Features} \label{\detokenize{chapter2:more-basic-matrix-features}} \sphinxstylestrong{Some Equivalent Statements.} For an \(N\times N\) matrix \(\mathbf{A}\) the following properties are all equivalent \begin{itemize} \item {} If the inverse of \(\mathbf{A}\) exists, \(\mathbf{A}\) is nonsingular. \item {} The equation \(\mathbf{Ax}=0\) implies \(\mathbf{x}=0\). \item {} The rows of \(\mathbf{A}\) form a basis of \(R^N\). \item {} The columns of \(\mathbf{A}\) form a basis of \(R^N\). \item {} \(\mathbf{A}\) is a product of elementary matrices. \item {} \(0\) is not eigenvalue of \(\mathbf{A}\). \end{itemize} \subsection{Numpy and arrays} \label{\detokenize{chapter2:numpy-and-arrays}} \sphinxhref{http://www.numpy.org/}{Numpy} provides an easy way to handle arrays in Python. The standard way to import this library is as \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \end{sphinxVerbatim} Here follows a simple example where we set up an array of ten elements, all determined by random numbers drawn according to the normal distribution, \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{n}{n} \PYG{o}{=} \PYG{l+m+mi}{10} \PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{normal}\PYG{p}{(}\PYG{n}{size}\PYG{o}{=}\PYG{n}{n}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{x}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] [ 0.66109153 0.97132954 \PYGZhy{}0.91470448 1.81588452 0.49665195 0.77698678 1.04576423 1.65832096 \PYGZhy{}0.25613699 \PYGZhy{}0.7554783 ] 0.971329541323762 \end{sphinxVerbatim} We defined a vector \(x\) with \(n=10\) elements with its values given by the Normal distribution \(N(0,1)\). Another alternative is to declare a vector as follows \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{array}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] [1 2 3] \end{sphinxVerbatim} Here we have defined a vector with three elements, with \(x_0=1\), \(x_1=2\) and \(x_2=3\). Note that both Python and C++ start numbering array elements from \(0\) and on. This means that a vector with \(n\) elements has a sequence of entities \(x_0, x_1, x_2, \dots, x_{n-1}\). We could also let (recommended) Numpy to compute the logarithms of a specific array as \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{log}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{array}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{7}\PYG{p}{,} \PYG{l+m+mi}{8}\PYG{p}{]}\PYG{p}{)}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] [1.38629436 1.94591015 2.07944154] \end{sphinxVerbatim} In the last example we used Numpy’s unary function \(np.log\). This function is highly tuned to compute array elements since the code is vectorized and does not require looping. We normaly recommend that you use the Numpy intrinsic functions instead of the corresponding \sphinxstylestrong{log} function from Python’s \sphinxstylestrong{math} module. The looping is done explicitely by the \sphinxstylestrong{np.log} function. The alternative, and slower way to compute the logarithms of a vector would be to write \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{k+kn}{from} \PYG{n+nn}{math} \PYG{k+kn}{import} \PYG{n}{log} \PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{array}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{7}\PYG{p}{,} \PYG{l+m+mi}{8}\PYG{p}{]}\PYG{p}{)} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{p}{:} \PYG{n}{x}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{n}{log}\PYG{p}{(}\PYG{n}{x}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] [1 1 2] \end{sphinxVerbatim} We note that our code is much longer already and we need to import the \sphinxstylestrong{log} function from the \sphinxstylestrong{math} module. The attentive reader will also notice that the output is \([1, 1, 2]\). Python interprets automagically our numbers as integers (like the \sphinxstylestrong{automatic} keyword in C++). To change this we could define our array elements to be double precision numbers as \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{log}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{array}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{7}\PYG{p}{,} \PYG{l+m+mi}{8}\PYG{p}{]}\PYG{p}{,} \PYG{n}{dtype} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{float64}\PYG{p}{)}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] [1.38629436 1.94591015 2.07944154] \end{sphinxVerbatim} or simply write them as double precision numbers (Python uses 64 bits as default for floating point type variables), that is \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{log}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{array}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mf}{4.0}\PYG{p}{,} \PYG{l+m+mf}{7.0}\PYG{p}{,} \PYG{l+m+mf}{8.0}\PYG{p}{]}\PYG{p}{)}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] [1.38629436 1.94591015 2.07944154] \end{sphinxVerbatim} To check the number of bytes (remember that one byte contains eight bits for double precision variables), you can use simple use the \sphinxstylestrong{itemsize} functionality (the array \(x\) is actually an object which inherits the functionalities defined in Numpy) as \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{log}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{array}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mf}{4.0}\PYG{p}{,} \PYG{l+m+mf}{7.0}\PYG{p}{,} \PYG{l+m+mf}{8.0}\PYG{p}{]}\PYG{p}{)}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{x}\PYG{o}{.}\PYG{n}{itemsize}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] 8 \end{sphinxVerbatim} \subsection{Matrices in Python} \label{\detokenize{chapter2:matrices-in-python}} Having defined vectors, we are now ready to try out matrices. We can define a \(3 \times 3 \) real matrix \(\hat{A}\) as (recall that we user lowercase letters for vectors and uppercase letters for matrices) \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{n}{A} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{log}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{array}\PYG{p}{(}\PYG{p}{[} \PYG{p}{[}\PYG{l+m+mf}{4.0}\PYG{p}{,} \PYG{l+m+mf}{7.0}\PYG{p}{,} \PYG{l+m+mf}{8.0}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mf}{3.0}\PYG{p}{,} \PYG{l+m+mf}{10.0}\PYG{p}{,} \PYG{l+m+mf}{11.0}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mf}{4.0}\PYG{p}{,} \PYG{l+m+mf}{5.0}\PYG{p}{,} \PYG{l+m+mf}{7.0}\PYG{p}{]} \PYG{p}{]}\PYG{p}{)}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{A}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] [[1.38629436 1.94591015 2.07944154] [1.09861229 2.30258509 2.39789527] [1.38629436 1.60943791 1.94591015]] \end{sphinxVerbatim} If we use the \sphinxstylestrong{shape} function we would get \((3, 3)\) as output, that is verifying that our matrix is a \(3\times 3\) matrix. We can slice the matrix and print for example the first column (Python organized matrix elements in a row\sphinxhyphen{}major order, see below) as \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{n}{A} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{log}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{array}\PYG{p}{(}\PYG{p}{[} \PYG{p}{[}\PYG{l+m+mf}{4.0}\PYG{p}{,} \PYG{l+m+mf}{7.0}\PYG{p}{,} \PYG{l+m+mf}{8.0}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mf}{3.0}\PYG{p}{,} \PYG{l+m+mf}{10.0}\PYG{p}{,} \PYG{l+m+mf}{11.0}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mf}{4.0}\PYG{p}{,} \PYG{l+m+mf}{5.0}\PYG{p}{,} \PYG{l+m+mf}{7.0}\PYG{p}{]} \PYG{p}{]}\PYG{p}{)}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} print the first column, row\PYGZhy{}major order and elements start with 0} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{A}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] [1.38629436 1.09861229 1.38629436] \end{sphinxVerbatim} We can continue this was by printing out other columns or rows. The example here prints out the second column \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{n}{A} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{log}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{array}\PYG{p}{(}\PYG{p}{[} \PYG{p}{[}\PYG{l+m+mf}{4.0}\PYG{p}{,} \PYG{l+m+mf}{7.0}\PYG{p}{,} \PYG{l+m+mf}{8.0}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mf}{3.0}\PYG{p}{,} \PYG{l+m+mf}{10.0}\PYG{p}{,} \PYG{l+m+mf}{11.0}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mf}{4.0}\PYG{p}{,} \PYG{l+m+mf}{5.0}\PYG{p}{,} \PYG{l+m+mf}{7.0}\PYG{p}{]} \PYG{p}{]}\PYG{p}{)}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} print the first column, row\PYGZhy{}major order and elements start with 0} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{A}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{p}{:}\PYG{p}{]}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] [1.09861229 2.30258509 2.39789527] \end{sphinxVerbatim} Numpy contains many other functionalities that allow us to slice, subdivide etc etc arrays. We strongly recommend that you look up the \sphinxhref{http://www.numpy.org/}{Numpy website for more details}. Useful functions when defining a matrix are the \sphinxstylestrong{np.zeros} function which declares a matrix of a given dimension and sets all elements to zero \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{n}{n} \PYG{o}{=} \PYG{l+m+mi}{10} \PYG{c+c1}{\PYGZsh{} define a matrix of dimension 10 x 10 and set all elements to zero} \PYG{n}{A} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(} \PYG{p}{(}\PYG{n}{n}\PYG{p}{,} \PYG{n}{n}\PYG{p}{)} \PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{A}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] [[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]] \end{sphinxVerbatim} or initializing all elements to \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{n}{n} \PYG{o}{=} \PYG{l+m+mi}{10} \PYG{c+c1}{\PYGZsh{} define a matrix of dimension 10 x 10 and set all elements to one} \PYG{n}{A} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{eye}\PYG{p}{(} \PYG{n}{n} \PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{A}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] [[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 1. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 1. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 1. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 1. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 1. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]] \end{sphinxVerbatim} or as unitarily distributed random numbers (see the material on random number generators in the statistics part) \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{n}{n} \PYG{o}{=} \PYG{l+m+mi}{10} \PYG{c+c1}{\PYGZsh{} define a matrix of dimension 10 x 10 and set all elements to random numbers with x \PYGZbs{}in [0, 1]} \PYG{n}{A} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{rand}\PYG{p}{(}\PYG{n}{n}\PYG{p}{,} \PYG{n}{n}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{A}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] [[0.88645063 0.21176899 0.23897722 0.06720011 0.38272048 0.88809424 0.0878077 0.73425375 0.72093518 0.19764225] [0.74839206 0.31545538 0.14076925 0.59046117 0.89437867 0.81232389 0.20782531 0.52852411 0.10079113 0.07130568] [0.64573187 0.71051155 0.65626927 0.92475512 0.58540859 0.52327428 0.28610035 0.69580848 0.94590985 0.98668663] [0.01101253 0.38937429 0.60430671 0.81789875 0.2280742 0.17786312 0.34053316 0.19374203 0.01520576 0.2466458 ] [0.60613717 0.19314313 0.959787 0.6376673 0.73798553 0.63486051 0.15742577 0.39450088 0.07204545 0.46013967] [0.23509059 0.79672774 0.81910307 0.53487394 0.50992632 0.112086 0.55751711 0.10662789 0.70911163 0.59072491] [0.0259775 0.26815865 0.5090044 0.41809535 0.32232515 0.72743704 0.32110496 0.04202295 0.18265714 0.70084503] [0.07736581 0.57300172 0.97099592 0.62357997 0.36579921 0.37062279 0.74683978 0.59433006 0.35540585 0.97384662] [0.25205634 0.7601454 0.40639052 0.24504215 0.21745329 0.95788719 0.7490677 0.29789413 0.81839129 0.93822723] [0.6302074 0.47072129 0.16215217 0.53540566 0.91760002 0.14146861 0.37860338 0.8099108 0.07897476 0.69254202]] \end{sphinxVerbatim} As we will see throughout these lectures, there are several extremely useful functionalities in Numpy. As an example, consider the discussion of the covariance matrix. Suppose we have defined three vectors \(\hat{x}, \hat{y}, \hat{z}\) with \(n\) elements each. The covariance matrix is defined as \begin{equation*} \begin{split} \hat{\Sigma} = \begin{bmatrix} \sigma_{xx} & \sigma_{xy} & \sigma_{xz} \\ \sigma_{yx} & \sigma_{yy} & \sigma_{yz} \\ \sigma_{zx} & \sigma_{zy} & \sigma_{zz} \end{bmatrix}, \end{split} \end{equation*} where for example \begin{equation*} \begin{split} \sigma_{xy} =\frac{1}{n} \sum_{i=0}^{n-1}(x_i- \overline{x})(y_i- \overline{y}). \end{split} \end{equation*} The Numpy function \sphinxstylestrong{np.cov} calculates the covariance elements using the factor \(1/(n-1)\) instead of \(1/n\) since it assumes we do not have the exact mean values. The following simple function uses the \sphinxstylestrong{np.vstack} function which takes each vector of dimension \(1\times n\) and produces a \(3\times n\) matrix \(\hat{W}\) \begin{equation*} \begin{split} \hat{W} = \begin{bmatrix} x_0 & y_0 & z_0 \\ x_1 & y_1 & z_1 \\ x_2 & y_2 & z_2 \\ \dots & \dots & \dots \\ x_{n-2} & y_{n-2} & z_{n-2} \\ x_{n-1} & y_{n-1} & z_{n-1} \end{bmatrix}, \end{split} \end{equation*} which in turn is converted into into the \(3\times 3\) covariance matrix \(\hat{\Sigma}\) via the Numpy function \sphinxstylestrong{np.cov()}. We note that we can also calculate the mean value of each set of samples \(\hat{x}\) etc using the Numpy function \sphinxstylestrong{np.mean(x)}. We can also extract the eigenvalues of the covariance matrix through the \sphinxstylestrong{np.linalg.eig()} function. \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{c+c1}{\PYGZsh{} Importing various packages} \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{n}{n} \PYG{o}{=} \PYG{l+m+mi}{100} \PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{normal}\PYG{p}{(}\PYG{n}{size}\PYG{o}{=}\PYG{n}{n}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)} \PYG{n}{y} \PYG{o}{=} \PYG{l+m+mi}{4}\PYG{o}{+}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x}\PYG{o}{+}\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{normal}\PYG{p}{(}\PYG{n}{size}\PYG{o}{=}\PYG{n}{n}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)} \PYG{n}{z} \PYG{o}{=} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{3}\PYG{o}{+}\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{normal}\PYG{p}{(}\PYG{n}{size}\PYG{o}{=}\PYG{n}{n}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{z}\PYG{p}{)}\PYG{p}{)} \PYG{n}{W} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{vstack}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z}\PYG{p}{)}\PYG{p}{)} \PYG{n}{Sigma} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{cov}\PYG{p}{(}\PYG{n}{W}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{Sigma}\PYG{p}{)} \PYG{n}{Eigvals}\PYG{p}{,} \PYG{n}{Eigvecs} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{linalg}\PYG{o}{.}\PYG{n}{eig}\PYG{p}{(}\PYG{n}{Sigma}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{Eigvals}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYGZhy{}0.13489611340279922 3.7349937337703607 \PYGZhy{}0.6150223388222948 [[ 1.15822308 3.44928776 3.99889831] [ 3.44928776 11.11151113 12.25819175] [ 3.99889831 12.25819175 21.09855273]] [30.28557034 0.07629455 3.00642205] \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{o}{\PYGZpc{}}\PYG{k}{matplotlib} inline \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt} \PYG{k+kn}{from} \PYG{n+nn}{scipy} \PYG{k+kn}{import} \PYG{n}{sparse} \PYG{n}{eye} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{eye}\PYG{p}{(}\PYG{l+m+mi}{4}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{eye}\PYG{p}{)} \PYG{n}{sparse\PYGZus{}mtx} \PYG{o}{=} \PYG{n}{sparse}\PYG{o}{.}\PYG{n}{csr\PYGZus{}matrix}\PYG{p}{(}\PYG{n}{eye}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{sparse\PYGZus{}mtx}\PYG{p}{)} \PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{linspace}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{10}\PYG{p}{,}\PYG{l+m+mi}{10}\PYG{p}{,}\PYG{l+m+mi}{100}\PYG{p}{)} \PYG{n}{y} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,}\PYG{n}{y}\PYG{p}{,}\PYG{n}{marker}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{x}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] [[1. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 1. 0.] [0. 0. 0. 1.]] (0, 0) 1.0 (1, 1) 1.0 (2, 2) 1.0 (3, 3) 1.0 \end{sphinxVerbatim} \noindent\sphinxincludegraphics{{chapter2_42_1}.png} \subsection{Meet the Pandas} \label{\detokenize{chapter2:meet-the-pandas}} Another useful Python package is \sphinxhref{https://pandas.pydata.org/}{pandas}, which is an open source library providing high\sphinxhyphen{}performance, easy\sphinxhyphen{}to\sphinxhyphen{}use data structures and data analysis tools for Python. \sphinxstylestrong{pandas} stands for panel data, a term borrowed from econometrics and is an efficient library for data analysis with an emphasis on tabular data. \sphinxstylestrong{pandas} has two major classes, the \sphinxstylestrong{DataFrame} class with two\sphinxhyphen{}dimensional data objects and tabular data organized in columns and the class \sphinxstylestrong{Series} with a focus on one\sphinxhyphen{}dimensional data objects. Both classes allow you to index data easily as we will see in the examples below. \sphinxstylestrong{pandas} allows you also to perform mathematical operations on the data, spanning from simple reshapings of vectors and matrices to statistical operations. The following simple example shows how we can, in an easy way make tables of our data. Here we define a data set which includes names, place of birth and date of birth, and displays the data in an easy to read way. We will see repeated use of \sphinxstylestrong{pandas}, in particular in connection with classification of data. \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{pandas} \PYG{k}{as} \PYG{n+nn}{pd} \PYG{k+kn}{from} \PYG{n+nn}{IPython}\PYG{n+nn}{.}\PYG{n+nn}{display} \PYG{k+kn}{import} \PYG{n}{display} \PYG{n}{data} \PYG{o}{=} \PYG{p}{\PYGZob{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{First Name}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{:} \PYG{p}{[}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Frodo}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Bilbo}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Aragorn II}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Samwise}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{]}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Last Name}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{:} \PYG{p}{[}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Baggins}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Baggins}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Elessar}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Gamgee}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{]}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Place of birth}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{:} \PYG{p}{[}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Shire}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Shire}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Eriador}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Shire}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{]}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Date of Birth T.A.}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{:} \PYG{p}{[}\PYG{l+m+mi}{2968}\PYG{p}{,} \PYG{l+m+mi}{2890}\PYG{p}{,} \PYG{l+m+mi}{2931}\PYG{p}{,} \PYG{l+m+mi}{2980}\PYG{p}{]} \PYG{p}{\PYGZcb{}} \PYG{n}{data\PYGZus{}pandas} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{DataFrame}\PYG{p}{(}\PYG{n}{data}\PYG{p}{)} \PYG{n}{display}\PYG{p}{(}\PYG{n}{data\PYGZus{}pandas}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] First Name Last Name Place of birth Date of Birth T.A. 0 Frodo Baggins Shire 2968 1 Bilbo Baggins Shire 2890 2 Aragorn II Elessar Eriador 2931 3 Samwise Gamgee Shire 2980 \end{sphinxVerbatim} In the above we have imported \sphinxstylestrong{pandas} with the shorthand \sphinxstylestrong{pd}, the latter has become the standard way we import \sphinxstylestrong{pandas}. We make then a list of various variables and reorganize the aboves lists into a \sphinxstylestrong{DataFrame} and then print out a neat table with specific column labels as \sphinxstyleemphasis{Name}, \sphinxstyleemphasis{place of birth} and \sphinxstyleemphasis{date of birth}. Displaying these results, we see that the indices are given by the default numbers from zero to three. \sphinxstylestrong{pandas} is extremely flexible and we can easily change the above indices by defining a new type of indexing as \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{n}{data\PYGZus{}pandas} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{DataFrame}\PYG{p}{(}\PYG{n}{data}\PYG{p}{,}\PYG{n}{index}\PYG{o}{=}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Frodo}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Bilbo}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Aragorn}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Sam}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{)} \PYG{n}{display}\PYG{p}{(}\PYG{n}{data\PYGZus{}pandas}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] First Name Last Name Place of birth Date of Birth T.A. Frodo Frodo Baggins Shire 2968 Bilbo Bilbo Baggins Shire 2890 Aragorn Aragorn II Elessar Eriador 2931 Sam Samwise Gamgee Shire 2980 \end{sphinxVerbatim} Thereafter we display the content of the row which begins with the index \sphinxstylestrong{Aragorn} \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{n}{display}\PYG{p}{(}\PYG{n}{data\PYGZus{}pandas}\PYG{o}{.}\PYG{n}{loc}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Aragorn}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] First Name Aragorn II Last Name Elessar Place of birth Eriador Date of Birth T.A. 2931 Name: Aragorn, dtype: object \end{sphinxVerbatim} We can easily append data to this, for example \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{n}{new\PYGZus{}hobbit} \PYG{o}{=} \PYG{p}{\PYGZob{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{First Name}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{:} \PYG{p}{[}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Peregrin}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{]}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Last Name}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{:} \PYG{p}{[}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Took}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{]}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Place of birth}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{:} \PYG{p}{[}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Shire}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{]}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Date of Birth T.A.}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{:} \PYG{p}{[}\PYG{l+m+mi}{2990}\PYG{p}{]} \PYG{p}{\PYGZcb{}} \PYG{n}{data\PYGZus{}pandas}\PYG{o}{=}\PYG{n}{data\PYGZus{}pandas}\PYG{o}{.}\PYG{n}{append}\PYG{p}{(}\PYG{n}{pd}\PYG{o}{.}\PYG{n}{DataFrame}\PYG{p}{(}\PYG{n}{new\PYGZus{}hobbit}\PYG{p}{,} \PYG{n}{index}\PYG{o}{=}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Pippin}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{)}\PYG{p}{)} \PYG{n}{display}\PYG{p}{(}\PYG{n}{data\PYGZus{}pandas}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] First Name Last Name Place of birth Date of Birth T.A. Frodo Frodo Baggins Shire 2968 Bilbo Bilbo Baggins Shire 2890 Aragorn Aragorn II Elessar Eriador 2931 Sam Samwise Gamgee Shire 2980 Pippin Peregrin Took Shire 2990 \end{sphinxVerbatim} Here are other examples where we use the \sphinxstylestrong{DataFrame} functionality to handle arrays, now with more interesting features for us, namely numbers. We set up a matrix of dimensionality \(10\times 5\) and compute the mean value and standard deviation of each column. Similarly, we can perform mathematial operations like squaring the matrix elements and many other operations. \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{k+kn}{import} \PYG{n+nn}{pandas} \PYG{k}{as} \PYG{n+nn}{pd} \PYG{k+kn}{from} \PYG{n+nn}{IPython}\PYG{n+nn}{.}\PYG{n+nn}{display} \PYG{k+kn}{import} \PYG{n}{display} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{seed}\PYG{p}{(}\PYG{l+m+mi}{100}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} setting up a 10 x 5 matrix} \PYG{n}{rows} \PYG{o}{=} \PYG{l+m+mi}{10} \PYG{n}{cols} \PYG{o}{=} \PYG{l+m+mi}{5} \PYG{n}{a} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{randn}\PYG{p}{(}\PYG{n}{rows}\PYG{p}{,}\PYG{n}{cols}\PYG{p}{)} \PYG{n}{df} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{DataFrame}\PYG{p}{(}\PYG{n}{a}\PYG{p}{)} \PYG{n}{display}\PYG{p}{(}\PYG{n}{df}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{p}{)}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{std}\PYG{p}{(}\PYG{p}{)}\PYG{p}{)} \PYG{n}{display}\PYG{p}{(}\PYG{n}{df}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{df}\PYG{o}{\PYGZhy{}}\PYG{n}{df}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{p}{)}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] 0 1 2 3 4 0 \PYGZhy{}1.749765 0.342680 1.153036 \PYGZhy{}0.252436 0.981321 1 0.514219 0.221180 \PYGZhy{}1.070043 \PYGZhy{}0.189496 0.255001 2 \PYGZhy{}0.458027 0.435163 \PYGZhy{}0.583595 0.816847 0.672721 3 \PYGZhy{}0.104411 \PYGZhy{}0.531280 1.029733 \PYGZhy{}0.438136 \PYGZhy{}1.118318 4 1.618982 1.541605 \PYGZhy{}0.251879 \PYGZhy{}0.842436 0.184519 5 0.937082 0.731000 1.361556 \PYGZhy{}0.326238 0.055676 6 0.222400 \PYGZhy{}1.443217 \PYGZhy{}0.756352 0.816454 0.750445 7 \PYGZhy{}0.455947 1.189622 \PYGZhy{}1.690617 \PYGZhy{}1.356399 \PYGZhy{}1.232435 8 \PYGZhy{}0.544439 \PYGZhy{}0.668172 0.007315 \PYGZhy{}0.612939 1.299748 9 \PYGZhy{}1.733096 \PYGZhy{}0.983310 0.357508 \PYGZhy{}1.613579 1.470714 \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] 0 \PYGZhy{}0.175300 1 0.083527 2 \PYGZhy{}0.044334 3 \PYGZhy{}0.399836 4 0.331939 dtype: float64 0 1.069584 1 0.965548 2 1.018232 3 0.793167 4 0.918992 dtype: float64 \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] 0 1 2 3 4 0 3.061679 0.117430 1.329492 0.063724 0.962990 1 0.264421 0.048920 1.144993 0.035909 0.065026 2 0.209789 0.189367 0.340583 0.667239 0.452553 3 0.010902 0.282259 1.060349 0.191963 1.250636 4 2.621102 2.376547 0.063443 0.709698 0.034047 5 0.878123 0.534362 1.853835 0.106431 0.003100 6 0.049462 2.082875 0.572069 0.666597 0.563167 7 0.207888 1.415201 2.858185 1.839818 1.518895 8 0.296414 0.446453 0.000054 0.375694 1.689345 9 3.003620 0.966899 0.127812 2.603636 2.162999 \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] 0 1 2 3 4 0 \PYGZhy{}1.574465 0.259153 1.197370 0.147400 0.649382 1 0.689519 0.137652 \PYGZhy{}1.025709 0.210340 \PYGZhy{}0.076938 2 \PYGZhy{}0.282727 0.351636 \PYGZhy{}0.539261 1.216683 0.340782 3 0.070889 \PYGZhy{}0.614808 1.074067 \PYGZhy{}0.038300 \PYGZhy{}1.450257 4 1.794282 1.458078 \PYGZhy{}0.207545 \PYGZhy{}0.442600 \PYGZhy{}0.147420 5 1.112383 0.647473 1.405890 0.073598 \PYGZhy{}0.276263 6 0.397700 \PYGZhy{}1.526744 \PYGZhy{}0.712018 1.216290 0.418506 7 \PYGZhy{}0.280647 1.106095 \PYGZhy{}1.646283 \PYGZhy{}0.956563 \PYGZhy{}1.564374 8 \PYGZhy{}0.369139 \PYGZhy{}0.751699 0.051649 \PYGZhy{}0.213103 0.967809 9 \PYGZhy{}1.557795 \PYGZhy{}1.066837 0.401842 \PYGZhy{}1.213743 1.138775 \end{sphinxVerbatim} Thereafter we can select specific columns only and plot final results \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{n}{df}\PYG{o}{.}\PYG{n}{columns} \PYG{o}{=} \PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{First}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Second}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Third}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Fourth}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Fifth}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{n}{df}\PYG{o}{.}\PYG{n}{index} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{arange}\PYG{p}{(}\PYG{l+m+mi}{10}\PYG{p}{)} \PYG{n}{display}\PYG{p}{(}\PYG{n}{df}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{df}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Second}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{p}{)} \PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{info}\PYG{p}{(}\PYG{p}{)}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{describe}\PYG{p}{(}\PYG{p}{)}\PYG{p}{)} \PYG{k+kn}{from} \PYG{n+nn}{pylab} \PYG{k+kn}{import} \PYG{n}{plt}\PYG{p}{,} \PYG{n}{mpl} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{style}\PYG{o}{.}\PYG{n}{use}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{seaborn}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{mpl}\PYG{o}{.}\PYG{n}{rcParams}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{font.family}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{o}{=} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{serif}\PYG{l+s+s1}{\PYGZsq{}} \PYG{n}{df}\PYG{o}{.}\PYG{n}{cumsum}\PYG{p}{(}\PYG{p}{)}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{lw}\PYG{o}{=}\PYG{l+m+mf}{2.0}\PYG{p}{,} \PYG{n}{figsize}\PYG{o}{=}\PYG{p}{(}\PYG{l+m+mi}{10}\PYG{p}{,}\PYG{l+m+mi}{6}\PYG{p}{)}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \PYG{n}{df}\PYG{o}{.}\PYG{n}{plot}\PYG{o}{.}\PYG{n}{bar}\PYG{p}{(}\PYG{n}{figsize}\PYG{o}{=}\PYG{p}{(}\PYG{l+m+mi}{10}\PYG{p}{,}\PYG{l+m+mi}{6}\PYG{p}{)}\PYG{p}{,} \PYG{n}{rot}\PYG{o}{=}\PYG{l+m+mi}{15}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] First Second Third Fourth Fifth 0 \PYGZhy{}1.749765 0.342680 1.153036 \PYGZhy{}0.252436 0.981321 1 0.514219 0.221180 \PYGZhy{}1.070043 \PYGZhy{}0.189496 0.255001 2 \PYGZhy{}0.458027 0.435163 \PYGZhy{}0.583595 0.816847 0.672721 3 \PYGZhy{}0.104411 \PYGZhy{}0.531280 1.029733 \PYGZhy{}0.438136 \PYGZhy{}1.118318 4 1.618982 1.541605 \PYGZhy{}0.251879 \PYGZhy{}0.842436 0.184519 5 0.937082 0.731000 1.361556 \PYGZhy{}0.326238 0.055676 6 0.222400 \PYGZhy{}1.443217 \PYGZhy{}0.756352 0.816454 0.750445 7 \PYGZhy{}0.455947 1.189622 \PYGZhy{}1.690617 \PYGZhy{}1.356399 \PYGZhy{}1.232435 8 \PYGZhy{}0.544439 \PYGZhy{}0.668172 0.007315 \PYGZhy{}0.612939 1.299748 9 \PYGZhy{}1.733096 \PYGZhy{}0.983310 0.357508 \PYGZhy{}1.613579 1.470714 \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] 0.08352721390288316 \PYGZlt{}class \PYGZsq{}pandas.core.frame.DataFrame\PYGZsq{}\PYGZgt{} Int64Index: 10 entries, 0 to 9 Data columns (total 5 columns): \PYGZsh{} Column Non\PYGZhy{}Null Count Dtype \PYGZhy{}\PYGZhy{}\PYGZhy{} \PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{} \PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{} \PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{} 0 First 10 non\PYGZhy{}null float64 1 Second 10 non\PYGZhy{}null float64 2 Third 10 non\PYGZhy{}null float64 3 Fourth 10 non\PYGZhy{}null float64 4 Fifth 10 non\PYGZhy{}null float64 dtypes: float64(5) memory usage: 480.0 bytes None First Second Third Fourth Fifth count 10.000000 10.000000 10.000000 10.000000 10.000000 mean \PYGZhy{}0.175300 0.083527 \PYGZhy{}0.044334 \PYGZhy{}0.399836 0.331939 std 1.069584 0.965548 1.018232 0.793167 0.918992 min \PYGZhy{}1.749765 \PYGZhy{}1.443217 \PYGZhy{}1.690617 \PYGZhy{}1.613579 \PYGZhy{}1.232435 25\PYGZpc{} \PYGZhy{}0.522836 \PYGZhy{}0.633949 \PYGZhy{}0.713163 \PYGZhy{}0.785061 0.087887 50\PYGZpc{} \PYGZhy{}0.280179 0.281930 \PYGZhy{}0.122282 \PYGZhy{}0.382187 0.463861 75\PYGZpc{} 0.441264 0.657041 0.861676 \PYGZhy{}0.205231 0.923602 max 1.618982 1.541605 1.361556 0.816847 1.470714 \end{sphinxVerbatim} \noindent\sphinxincludegraphics{{chapter2_54_2}.png} \noindent\sphinxincludegraphics{{chapter2_54_3}.png} We can produce a \(4\times 4\) matrix \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{n}{b} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{arange}\PYG{p}{(}\PYG{l+m+mi}{16}\PYG{p}{)}\PYG{o}{.}\PYG{n}{reshape}\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{4}\PYG{p}{,}\PYG{l+m+mi}{4}\PYG{p}{)}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{b}\PYG{p}{)} \PYG{n}{df1} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{DataFrame}\PYG{p}{(}\PYG{n}{b}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{df1}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] 0 1 2 3 0 0 1 2 3 1 4 5 6 7 2 8 9 10 11 3 12 13 14 15 \end{sphinxVerbatim} and many other operations. The \sphinxstylestrong{Series} class is another important class included in \sphinxstylestrong{pandas}. You can view it as a specialization of \sphinxstylestrong{DataFrame} but where we have just a single column of data. It shares many of the same features as \_DataFrame. As with \sphinxstylestrong{DataFrame}, most operations are vectorized, achieving thereby a high performance when dealing with computations of arrays, in particular labeled arrays. As we will see below it leads also to a very concice code close to the mathematical operations we may be interested in. For multidimensional arrays, we recommend strongly \sphinxhref{http://xarray.pydata.org/en/stable/}{xarray}. \sphinxstylestrong{xarray} has much of the same flexibility as \sphinxstylestrong{pandas}, but allows for the extension to higher dimensions than two. We will see examples later of the usage of both \sphinxstylestrong{pandas} and \sphinxstylestrong{xarray}. \subsection{Reading Data and fitting} \label{\detokenize{chapter2:reading-data-and-fitting}} In order to study various Machine Learning algorithms, we need to access data. Acccessing data is an essential step in all machine learning algorithms. In particular, setting up the so\sphinxhyphen{}called \sphinxstylestrong{design matrix} (to be defined below) is often the first element we need in order to perform our calculations. To set up the design matrix means reading (and later, when the calculations are done, writing) data in various formats, The formats span from reading files from disk, loading data from databases and interacting with online sources like web application programming interfaces (APIs). In handling various input formats, as discussed above, we will mainly stay with \sphinxstylestrong{pandas}, a Python package which allows us, in a seamless and painless way, to deal with a multitude of formats, from standard \sphinxstylestrong{csv} (comma separated values) files, via \sphinxstylestrong{excel}, \sphinxstylestrong{html} to \sphinxstylestrong{hdf5} formats. With \sphinxstylestrong{pandas} and the \sphinxstylestrong{DataFrame} and \sphinxstylestrong{Series} functionalities we are able to convert text data into the calculational formats we need for a specific algorithm. And our code is going to be pretty close the basic mathematical expressions. Our first data set is going to be a classic from nuclear physics, namely all available data on binding energies. Don’t be intimidated if you are not familiar with nuclear physics. It serves simply as an example here of a data set. We will show some of the strengths of packages like \sphinxstylestrong{Scikit\sphinxhyphen{}Learn} in fitting nuclear binding energies to specific functions using linear regression first. Then, as a teaser, we will show you how you can easily implement other algorithms like decision trees and random forests and neural networks. But before we really start with nuclear physics data, let’s just look at some simpler polynomial fitting cases, such as, (don’t be offended) fitting straight lines! \subsubsection{Simple linear regression model using \sphinxstylestrong{scikit\sphinxhyphen{}learn}} \label{\detokenize{chapter2:simple-linear-regression-model-using-scikit-learn}} We start with perhaps our simplest possible example, using \sphinxstylestrong{Scikit\sphinxhyphen{}Learn} to perform linear regression analysis on a data set produced by us. What follows is a simple Python code where we have defined a function \(y\) in terms of the variable \(x\). Both are defined as vectors with \(100\) entries. The numbers in the vector \(\hat{x}\) are given by random numbers generated with a uniform distribution with entries \(x_i \in [0,1]\) (more about probability distribution functions later). These values are then used to define a function \(y(x)\) (tabulated again as a vector) with a linear dependence on \(x\) plus a random noise added via the normal distribution. The Numpy functions are imported used the \sphinxstylestrong{import numpy as np} statement and the random number generator for the uniform distribution is called using the function \sphinxstylestrong{np.random.rand()}, where we specificy that we want \(100\) random variables. Using Numpy we define automatically an array with the specified number of elements, \(100\) in our case. With the Numpy function \sphinxstylestrong{randn()} we can compute random numbers with the normal distribution (mean value \(\mu\) equal to zero and variance \(\sigma^2\) set to one) and produce the values of \(y\) assuming a linear dependence as function of \(x\) \begin{equation*} \begin{split} y = 2x+N(0,1), \end{split} \end{equation*} where \(N(0,1)\) represents random numbers generated by the normal distribution. From \sphinxstylestrong{Scikit\sphinxhyphen{}Learn} we import then the \sphinxstylestrong{LinearRegression} functionality and make a prediction \(\tilde{y} = \alpha + \beta x\) using the function \sphinxstylestrong{fit(x,y)}. We call the set of data \((\hat{x},\hat{y})\) for our training data. The Python package \sphinxstylestrong{scikit\sphinxhyphen{}learn} has also a functionality which extracts the above fitting parameters \(\alpha\) and \(\beta\) (see below). Later we will distinguish between training data and test data. For plotting we use the Python package \sphinxhref{https://matplotlib.org/}{matplotlib} which produces publication quality figures. Feel free to explore the extensive \sphinxhref{https://matplotlib.org/gallery/index.html}{gallery} of examples. In this example we plot our original values of \(x\) and \(y\) as well as the prediction \sphinxstylestrong{ypredict} (\(\tilde{y}\)), which attempts at fitting our data with a straight line. The Python code follows here. \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{c+c1}{\PYGZsh{} Importing various packages} \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n}{LinearRegression} \PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{rand}\PYG{p}{(}\PYG{l+m+mi}{100}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{n}{y} \PYG{o}{=} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{o}{+}\PYG{l+m+mf}{0.01}\PYG{o}{*}\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{randn}\PYG{p}{(}\PYG{l+m+mi}{100}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{n}{linreg} \PYG{o}{=} \PYG{n}{LinearRegression}\PYG{p}{(}\PYG{p}{)} \PYG{n}{linreg}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,}\PYG{n}{y}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{}ynew = linreg.predict(x)} \PYG{c+c1}{\PYGZsh{}xnew = np.array([[0],[1]])} \PYG{n}{ypredict} \PYG{o}{=} \PYG{n}{linreg}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{ypredict}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{r\PYGZhy{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y} \PYG{p}{,}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{ro}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{axis}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mf}{1.0}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mf}{5.0}\PYG{p}{]}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{xlabel}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{\PYGZdl{}x\PYGZdl{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{ylabel}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{\PYGZdl{}y\PYGZdl{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{title}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Simple Linear Regression}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \end{sphinxVerbatim} \noindent\sphinxincludegraphics{{chapter2_60_0}.png} This example serves several aims. It allows us to demonstrate several aspects of data analysis and later machine learning algorithms. The immediate visualization shows that our linear fit is not impressive. It goes through the data points, but there are many outliers which are not reproduced by our linear regression. We could now play around with this small program and change for example the factor in front of \(x\) and the normal distribution. Try to change the function \(y\) to \begin{equation*} \begin{split} y = 10x+0.01 \times N(0,1), \end{split} \end{equation*} where \(x\) is defined as before. Does the fit look better? Indeed, by reducing the role of the noise given by the normal distribution we see immediately that our linear prediction seemingly reproduces better the training set. However, this testing ‘by the eye’ is obviouly not satisfactory in the long run. Here we have only defined the training data and our model, and have not discussed a more rigorous approach to the \sphinxstylestrong{cost} function. We need more rigorous criteria in defining whether we have succeeded or not in modeling our training data. You will be surprised to see that many scientists seldomly venture beyond this ‘by the eye’ approach. A standard approach for the \sphinxstyleemphasis{cost} function is the so\sphinxhyphen{}called \(\chi^2\) function (a variant of the mean\sphinxhyphen{}squared error (MSE)) \begin{equation*} \begin{split} \chi^2 = \frac{1}{n} \sum_{i=0}^{n-1}\frac{(y_i-\tilde{y}_i)^2}{\sigma_i^2}, \end{split} \end{equation*} where \(\sigma_i^2\) is the variance (to be defined later) of the entry \(y_i\). We may not know the explicit value of \(\sigma_i^2\), it serves however the aim of scaling the equations and make the cost function dimensionless. Minimizing the cost function is a central aspect of our discussions to come. Finding its minima as function of the model parameters (\(\alpha\) and \(\beta\) in our case) will be a recurring theme in these series of lectures. Essentially all machine learning algorithms we will discuss center around the minimization of the chosen cost function. This depends in turn on our specific model for describing the data, a typical situation in supervised learning. Automatizing the search for the minima of the cost function is a central ingredient in all algorithms. Typical methods which are employed are various variants of \sphinxstylestrong{gradient} methods. These will be discussed in more detail later. Again, you’ll be surprised to hear that many practitioners minimize the above function ‘’by the eye’, popularly dubbed as ‘chi by the eye’. That is, change a parameter and see (visually and numerically) that the \(\chi^2\) function becomes smaller. There are many ways to define the cost function. A simpler approach is to look at the relative difference between the training data and the predicted data, that is we define the relative error (why would we prefer the MSE instead of the relative error?) as \begin{equation*} \begin{split} \epsilon_{\mathrm{relative}}= \frac{\vert \hat{y} -\hat{\tilde{y}}\vert}{\vert \hat{y}\vert}. \end{split} \end{equation*} The squared cost function results in an arithmetic mean\sphinxhyphen{}unbiased estimator, and the absolute\sphinxhyphen{}value cost function results in a median\sphinxhyphen{}unbiased estimator (in the one\sphinxhyphen{}dimensional case, and a geometric median\sphinxhyphen{}unbiased estimator for the multi\sphinxhyphen{}dimensional case). The squared cost function has the disadvantage that it has the tendency to be dominated by outliers. We can modify easily the above Python code and plot the relative error instead \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n}{LinearRegression} \PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{rand}\PYG{p}{(}\PYG{l+m+mi}{100}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{n}{y} \PYG{o}{=} \PYG{l+m+mi}{5}\PYG{o}{*}\PYG{n}{x}\PYG{o}{+}\PYG{l+m+mf}{0.01}\PYG{o}{*}\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{randn}\PYG{p}{(}\PYG{l+m+mi}{100}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{n}{linreg} \PYG{o}{=} \PYG{n}{LinearRegression}\PYG{p}{(}\PYG{p}{)} \PYG{n}{linreg}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,}\PYG{n}{y}\PYG{p}{)} \PYG{n}{ypredict} \PYG{o}{=} \PYG{n}{linreg}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{abs}\PYG{p}{(}\PYG{n}{ypredict}\PYG{o}{\PYGZhy{}}\PYG{n}{y}\PYG{p}{)}\PYG{o}{/}\PYG{n+nb}{abs}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{ro}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{axis}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mf}{1.0}\PYG{p}{,}\PYG{l+m+mf}{0.0}\PYG{p}{,} \PYG{l+m+mf}{0.5}\PYG{p}{]}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{xlabel}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{\PYGZdl{}x\PYGZdl{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{ylabel}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{\PYGZdl{}}\PYG{l+s+s1}{\PYGZbs{}}\PYG{l+s+s1}{epsilon\PYGZus{}}\PYG{l+s+s1}{\PYGZob{}}\PYG{l+s+s1}{\PYGZbs{}}\PYG{l+s+s1}{mathrm}\PYG{l+s+si}{\PYGZob{}relative\PYGZcb{}}\PYG{l+s+s1}{\PYGZcb{}\PYGZdl{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{title}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Relative error}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \end{sphinxVerbatim} \noindent\sphinxincludegraphics{{chapter2_68_0}.png} Depending on the parameter in front of the normal distribution, we may have a small or larger relative error. Try to play around with different training data sets and study (graphically) the value of the relative error. As mentioned above, \sphinxstylestrong{Scikit\sphinxhyphen{}Learn} has an impressive functionality. We can for example extract the values of \(\alpha\) and \(\beta\) and their error estimates, or the variance and standard deviation and many other properties from the statistical data analysis. Here we show an example of the functionality of \sphinxstylestrong{Scikit\sphinxhyphen{}Learn}. \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n}{LinearRegression} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{metrics} \PYG{k+kn}{import} \PYG{n}{mean\PYGZus{}squared\PYGZus{}error}\PYG{p}{,} \PYG{n}{r2\PYGZus{}score}\PYG{p}{,} \PYG{n}{mean\PYGZus{}squared\PYGZus{}log\PYGZus{}error}\PYG{p}{,} \PYG{n}{mean\PYGZus{}absolute\PYGZus{}error} \PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{rand}\PYG{p}{(}\PYG{l+m+mi}{100}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{n}{y} \PYG{o}{=} \PYG{l+m+mf}{2.0}\PYG{o}{+} \PYG{l+m+mi}{5}\PYG{o}{*}\PYG{n}{x}\PYG{o}{+}\PYG{l+m+mf}{0.5}\PYG{o}{*}\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{randn}\PYG{p}{(}\PYG{l+m+mi}{100}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{n}{linreg} \PYG{o}{=} \PYG{n}{LinearRegression}\PYG{p}{(}\PYG{p}{)} \PYG{n}{linreg}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,}\PYG{n}{y}\PYG{p}{)} \PYG{n}{ypredict} \PYG{o}{=} \PYG{n}{linreg}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{The intercept alpha: }\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{linreg}\PYG{o}{.}\PYG{n}{intercept\PYGZus{}}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Coefficient beta : }\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{linreg}\PYG{o}{.}\PYG{n}{coef\PYGZus{}}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} The mean squared error } \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Mean squared error: }\PYG{l+s+si}{\PYGZpc{}.2f}\PYG{l+s+s2}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{mean\PYGZus{}squared\PYGZus{}error}\PYG{p}{(}\PYG{n}{y}\PYG{p}{,} \PYG{n}{ypredict}\PYG{p}{)}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Explained variance score: 1 is perfect prediction } \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Variance score: }\PYG{l+s+si}{\PYGZpc{}.2f}\PYG{l+s+s1}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{r2\PYGZus{}score}\PYG{p}{(}\PYG{n}{y}\PYG{p}{,} \PYG{n}{ypredict}\PYG{p}{)}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Mean squared log error } \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Mean squared log error: }\PYG{l+s+si}{\PYGZpc{}.2f}\PYG{l+s+s1}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{mean\PYGZus{}squared\PYGZus{}log\PYGZus{}error}\PYG{p}{(}\PYG{n}{y}\PYG{p}{,} \PYG{n}{ypredict}\PYG{p}{)} \PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Mean absolute error } \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Mean absolute error: }\PYG{l+s+si}{\PYGZpc{}.2f}\PYG{l+s+s1}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{mean\PYGZus{}absolute\PYGZus{}error}\PYG{p}{(}\PYG{n}{y}\PYG{p}{,} \PYG{n}{ypredict}\PYG{p}{)}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{ypredict}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{r\PYGZhy{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y} \PYG{p}{,}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{ro}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{axis}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mf}{0.0}\PYG{p}{,}\PYG{l+m+mf}{1.0}\PYG{p}{,}\PYG{l+m+mf}{1.5}\PYG{p}{,} \PYG{l+m+mf}{7.0}\PYG{p}{]}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{xlabel}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{\PYGZdl{}x\PYGZdl{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{ylabel}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{\PYGZdl{}y\PYGZdl{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{title}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Linear Regression fit }\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] The intercept alpha: [2.18780801] Coefficient beta : [[4.72228205]] Mean squared error: 0.37 Variance score: 0.83 Mean squared log error: 0.01 Mean absolute error: 0.47 \end{sphinxVerbatim} \noindent\sphinxincludegraphics{{chapter2_70_1}.png} The function \sphinxstylestrong{coef} gives us the parameter \(\beta\) of our fit while \sphinxstylestrong{intercept} yields \(\alpha\). Depending on the constant in front of the normal distribution, we get values near or far from \(alpha =2\) and \(\beta =5\). Try to play around with different parameters in front of the normal distribution. The function \sphinxstylestrong{meansquarederror} gives us the mean square error, a risk metric corresponding to the expected value of the squared (quadratic) error or loss defined as \begin{equation*} \begin{split} MSE(\hat{y},\hat{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2, \end{split} \end{equation*} The smaller the value, the better the fit. Ideally we would like to have an MSE equal zero. The attentive reader has probably recognized this function as being similar to the \(\chi^2\) function defined above. The \sphinxstylestrong{r2score} function computes \(R^2\), the coefficient of determination. It provides a measure of how well future samples are likely to be predicted by the model. Best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of \(\hat{y}\), disregarding the input features, would get a \(R^2\) score of \(0.0\). If \(\tilde{\hat{y}}_i\) is the predicted value of the \(i-th\) sample and \(y_i\) is the corresponding true value, then the score \(R^2\) is defined as \begin{equation*} \begin{split} R^2(\hat{y}, \tilde{\hat{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2}, \end{split} \end{equation*} where we have defined the mean value of \(\hat{y}\) as \begin{equation*} \begin{split} \bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i. \end{split} \end{equation*} Another quantity taht we will meet again in our discussions of regression analysis is the mean absolute error (MAE), a risk metric corresponding to the expected value of the absolute error loss or what we call the \(l1\)\sphinxhyphen{}norm loss. In our discussion above we presented the relative error. The MAE is defined as follows \begin{equation*} \begin{split} \text{MAE}(\hat{y}, \hat{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n-1} \left| y_i - \tilde{y}_i \right|. \end{split} \end{equation*} We present the squared logarithmic (quadratic) error \begin{equation*} \begin{split} \text{MSLE}(\hat{y}, \hat{\tilde{y}}) = \frac{1}{n} \sum_{i=0}^{n - 1} (\log_e (1 + y_i) - \log_e (1 + \tilde{y}_i) )^2, \end{split} \end{equation*} where \(\log_e (x)\) stands for the natural logarithm of \(x\). This error estimate is best to use when targets having exponential growth, such as population counts, average sales of a commodity over a span of years etc. Finally, another cost function is the Huber cost function used in robust regression. The rationale behind this possible cost function is its reduced sensitivity to outliers in the data set. In our discussions on dimensionality reduction and normalization of data we will meet other ways of dealing with outliers. The Huber cost function is defined as \begin{equation*} \begin{split} H_{\delta}(a)={\begin{cases}{\frac {1}{2}}{a^{2}}&{\text{for }}|a|\leq \delta ,\\\delta (|a|-{\frac {1}{2}}\delta ),&{\text{otherwise.}}\end{cases}}}. \end{split} \end{equation*} Here \(a=\boldsymbol{y} - \boldsymbol{\tilde{y}}\). We will discuss in more detail these and other functions in the various lectures. We conclude this part with another example. Instead of a linear \(x\)\sphinxhyphen{}dependence we study now a cubic polynomial and use the polynomial regression analysis tools of scikit\sphinxhyphen{}learn. \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt} \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{k+kn}{import} \PYG{n+nn}{random} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n}{Ridge} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{preprocessing} \PYG{k+kn}{import} \PYG{n}{PolynomialFeatures} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{pipeline} \PYG{k+kn}{import} \PYG{n}{make\PYGZus{}pipeline} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n}{LinearRegression} \PYG{n}{x}\PYG{o}{=}\PYG{n}{np}\PYG{o}{.}\PYG{n}{linspace}\PYG{p}{(}\PYG{l+m+mf}{0.02}\PYG{p}{,}\PYG{l+m+mf}{0.98}\PYG{p}{,}\PYG{l+m+mi}{200}\PYG{p}{)} \PYG{n}{noise} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{asarray}\PYG{p}{(}\PYG{n}{random}\PYG{o}{.}\PYG{n}{sample}\PYG{p}{(}\PYG{p}{(}\PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{200}\PYG{p}{)}\PYG{p}{)}\PYG{p}{,}\PYG{l+m+mi}{200}\PYG{p}{)}\PYG{p}{)} \PYG{n}{y}\PYG{o}{=}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{noise} \PYG{n}{yn}\PYG{o}{=}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{l+m+mi}{100} \PYG{n}{poly3} \PYG{o}{=} \PYG{n}{PolynomialFeatures}\PYG{p}{(}\PYG{n}{degree}\PYG{o}{=}\PYG{l+m+mi}{3}\PYG{p}{)} \PYG{n}{X} \PYG{o}{=} \PYG{n}{poly3}\PYG{o}{.}\PYG{n}{fit\PYGZus{}transform}\PYG{p}{(}\PYG{n}{x}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{)} \PYG{n}{clf3} \PYG{o}{=} \PYG{n}{LinearRegression}\PYG{p}{(}\PYG{p}{)} \PYG{n}{clf3}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,}\PYG{n}{y}\PYG{p}{)} \PYG{n}{Xplot}\PYG{o}{=}\PYG{n}{poly3}\PYG{o}{.}\PYG{n}{fit\PYGZus{}transform}\PYG{p}{(}\PYG{n}{x}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{)} \PYG{n}{poly3\PYGZus{}plot}\PYG{o}{=}\PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{clf3}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{Xplot}\PYG{p}{)}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Cubic Fit}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,}\PYG{n}{yn}\PYG{p}{,} \PYG{n}{color}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{red}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{True Cubic}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{scatter}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Data}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{color}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{orange}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{s}\PYG{o}{=}\PYG{l+m+mi}{15}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{legend}\PYG{p}{(}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \PYG{k}{def} \PYG{n+nf}{error}\PYG{p}{(}\PYG{n}{a}\PYG{p}{)}\PYG{p}{:} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n}{y}\PYG{p}{:} \PYG{n}{err}\PYG{o}{=}\PYG{p}{(}\PYG{n}{y}\PYG{o}{\PYGZhy{}}\PYG{n}{yn}\PYG{p}{)}\PYG{o}{/}\PYG{n}{yn} \PYG{k}{return} \PYG{n+nb}{abs}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{sum}\PYG{p}{(}\PYG{n}{err}\PYG{p}{)}\PYG{p}{)}\PYG{o}{/}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{err}\PYG{p}{)} \PYG{n+nb}{print} \PYG{p}{(}\PYG{n}{error}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)} \end{sphinxVerbatim} \noindent\sphinxincludegraphics{{chapter2_84_0}.png} \begin{sphinxVerbatim}[commandchars=\\\{\}] 0.005000000000000007 \end{sphinxVerbatim} \subsubsection{To our real data: nuclear binding energies. Brief reminder on masses and binding energies} \label{\detokenize{chapter2:to-our-real-data-nuclear-binding-energies-brief-reminder-on-masses-and-binding-energies}} Let us now dive into nuclear physics and remind ourselves briefly about some basic features about binding energies. A basic quantity which can be measured for the ground states of nuclei is the atomic mass \(M(N, Z)\) of the neutral atom with atomic mass number \(A\) and charge \(Z\). The number of neutrons is \(N\). There are indeed several sophisticated experiments worldwide which allow us to measure this quantity to high precision (parts per million even). Atomic masses are usually tabulated in terms of the mass excess defined by \begin{equation*} \begin{split} \Delta M(N, Z) = M(N, Z) - uA, \end{split} \end{equation*} where \(u\) is the Atomic Mass Unit \begin{equation*} \begin{split} u = M(^{12}\mathrm{C})/12 = 931.4940954(57) \hspace{0.1cm} \mathrm{MeV}/c^2. \end{split} \end{equation*} The nucleon masses are \begin{equation*} \begin{split} m_p = 1.00727646693(9)u, \end{split} \end{equation*} and \begin{equation*} \begin{split} m_n = 939.56536(8)\hspace{0.1cm} \mathrm{MeV}/c^2 = 1.0086649156(6)u. \end{split} \end{equation*} In the \sphinxhref{http://nuclearmasses.org/resources\_folder/Wang\_2017\_Chinese\_Phys\_C\_41\_030003.pdf}{2016 mass evaluation of by W.J.Huang, G.Audi, M.Wang, F.G.Kondev, S.Naimi and X.Xu} there are data on masses and decays of 3437 nuclei. The nuclear binding energy is defined as the energy required to break up a given nucleus into its constituent parts of \(N\) neutrons and \(Z\) protons. In terms of the atomic masses \(M(N, Z)\) the binding energy is defined by \begin{equation*} \begin{split} BE(N, Z) = ZM_H c^2 + Nm_n c^2 - M(N, Z)c^2 , \end{split} \end{equation*} where \(M_H\) is the mass of the hydrogen atom and \(m_n\) is the mass of the neutron. In terms of the mass excess the binding energy is given by \begin{equation*} \begin{split} BE(N, Z) = Z\Delta_H c^2 + N\Delta_n c^2 -\Delta(N, Z)c^2 , \end{split} \end{equation*} where \(\Delta_H c^2 = 7.2890\) MeV and \(\Delta_n c^2 = 8.0713\) MeV. A popular and physically intuitive model which can be used to parametrize the experimental binding energies as function of \(A\), is the so\sphinxhyphen{}called \sphinxstylestrong{liquid drop model}. The ansatz is based on the following expression \begin{equation*} \begin{split} BE(N,Z) = a_1A-a_2A^{2/3}-a_3\frac{Z^2}{A^{1/3}}-a_4\frac{(N-Z)^2}{A}, \end{split} \end{equation*} where \(A\) stands for the number of nucleons and the \(a_i\)s are parameters which are determined by a fit to the experimental data. To arrive at the above expression we have assumed that we can make the following assumptions: \begin{itemize} \item {} There is a volume term \(a_1A\) proportional with the number of nucleons (the energy is also an extensive quantity). When an assembly of nucleons of the same size is packed together into the smallest volume, each interior nucleon has a certain number of other nucleons in contact with it. This contribution is proportional to the volume. \item {} There is a surface energy term \(a_2A^{2/3}\). The assumption here is that a nucleon at the surface of a nucleus interacts with fewer other nucleons than one in the interior of the nucleus and hence its binding energy is less. This surface energy term takes that into account and is therefore negative and is proportional to the surface area. \item {} There is a Coulomb energy term \(a_3\frac{Z^2}{A^{1/3}}\). The electric repulsion between each pair of protons in a nucleus yields less binding. \item {} There is an asymmetry term \(a_4\frac{(N-Z)^2}{A}\). This term is associated with the Pauli exclusion principle and reflects the fact that the proton\sphinxhyphen{}neutron interaction is more attractive on the average than the neutron\sphinxhyphen{}neutron and proton\sphinxhyphen{}proton interactions. \end{itemize} We could also add a so\sphinxhyphen{}called pairing term, which is a correction term that arises from the tendency of proton pairs and neutron pairs to occur. An even number of particles is more stable than an odd number. \subsubsection{Organizing our data} \label{\detokenize{chapter2:organizing-our-data}} Let us start with reading and organizing our data. We start with the compilation of masses and binding energies from 2016. After having downloaded this file to our own computer, we are now ready to read the file and start structuring our data. We start with preparing folders for storing our calculations and the data file over masses and binding energies. We import also various modules that we will find useful in order to present various Machine Learning methods. Here we focus mainly on the functionality of \sphinxstylestrong{scikit\sphinxhyphen{}learn}. \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{c+c1}{\PYGZsh{} Common imports} \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{k+kn}{import} \PYG{n+nn}{pandas} \PYG{k}{as} \PYG{n+nn}{pd} \PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt} \PYG{k+kn}{import} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k}{as} \PYG{n+nn}{skl} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{train\PYGZus{}test\PYGZus{}split} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{metrics} \PYG{k+kn}{import} \PYG{n}{mean\PYGZus{}squared\PYGZus{}error}\PYG{p}{,} \PYG{n}{r2\PYGZus{}score}\PYG{p}{,} \PYG{n}{mean\PYGZus{}absolute\PYGZus{}error} \PYG{k+kn}{import} \PYG{n+nn}{os} \PYG{c+c1}{\PYGZsh{} Where to save the figures and data files} \PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Results}\PYG{l+s+s2}{\PYGZdq{}} \PYG{n}{FIGURE\PYGZus{}ID} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Results/FigureFiles}\PYG{l+s+s2}{\PYGZdq{}} \PYG{n}{DATA\PYGZus{}ID} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{DataFiles/}\PYG{l+s+s2}{\PYGZdq{}} \PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR}\PYG{p}{)}\PYG{p}{:} \PYG{n}{os}\PYG{o}{.}\PYG{n}{mkdir}\PYG{p}{(}\PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR}\PYG{p}{)} \PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{)}\PYG{p}{:} \PYG{n}{os}\PYG{o}{.}\PYG{n}{makedirs}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{)} \PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{)}\PYG{p}{:} \PYG{n}{os}\PYG{o}{.}\PYG{n}{makedirs}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{)} \PYG{k}{def} \PYG{n+nf}{image\PYGZus{}path}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)}\PYG{p}{:} \PYG{k}{return} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{join}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{,} \PYG{n}{fig\PYGZus{}id}\PYG{p}{)} \PYG{k}{def} \PYG{n+nf}{data\PYGZus{}path}\PYG{p}{(}\PYG{n}{dat\PYGZus{}id}\PYG{p}{)}\PYG{p}{:} \PYG{k}{return} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{join}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{,} \PYG{n}{dat\PYGZus{}id}\PYG{p}{)} \PYG{k}{def} \PYG{n+nf}{save\PYGZus{}fig}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)}\PYG{p}{:} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{savefig}\PYG{p}{(}\PYG{n}{image\PYGZus{}path}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)} \PYG{o}{+} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{.png}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n+nb}{format}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{png}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{infile} \PYG{o}{=} \PYG{n+nb}{open}\PYG{p}{(}\PYG{n}{data\PYGZus{}path}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{MassEval2016.dat}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}\PYG{p}{,}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \end{sphinxVerbatim} Before we proceed, we define also a function for making our plots. You can obviously avoid this and simply set up various \sphinxstylestrong{matplotlib} commands every time you need them. You may however find it convenient to collect all such commands in one function and simply call this function. \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{from} \PYG{n+nn}{pylab} \PYG{k+kn}{import} \PYG{n}{plt}\PYG{p}{,} \PYG{n}{mpl} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{style}\PYG{o}{.}\PYG{n}{use}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{seaborn}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{mpl}\PYG{o}{.}\PYG{n}{rcParams}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{font.family}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{o}{=} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{serif}\PYG{l+s+s1}{\PYGZsq{}} \PYG{k}{def} \PYG{n+nf}{MakePlot}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,}\PYG{n}{y}\PYG{p}{,} \PYG{n}{styles}\PYG{p}{,} \PYG{n}{labels}\PYG{p}{,} \PYG{n}{axlabels}\PYG{p}{)}\PYG{p}{:} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{figure}\PYG{p}{(}\PYG{n}{figsize}\PYG{o}{=}\PYG{p}{(}\PYG{l+m+mi}{10}\PYG{p}{,}\PYG{l+m+mi}{6}\PYG{p}{)}\PYG{p}{)} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{p}{:} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{x}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{,} \PYG{n}{y}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{,} \PYG{n}{styles}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{,} \PYG{n}{label} \PYG{o}{=} \PYG{n}{labels}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{xlabel}\PYG{p}{(}\PYG{n}{axlabels}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{ylabel}\PYG{p}{(}\PYG{n}{axlabels}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{legend}\PYG{p}{(}\PYG{n}{loc}\PYG{o}{=}\PYG{l+m+mi}{0}\PYG{p}{)} \end{sphinxVerbatim} Our next step is to read the data on experimental binding energies and reorganize them as functions of the mass number \(A\), the number of protons \(Z\) and neutrons \(N\) using \sphinxstylestrong{pandas}. Before we do this it is always useful (unless you have a binary file or other types of compressed data) to actually open the file and simply take a look at it! In particular, the program that outputs the final nuclear masses is written in Fortran with a specific format. It means that we need to figure out the format and which columns contain the data we are interested in. Pandas comes with a function that reads formatted output. After having admired the file, we are now ready to start massaging it with \sphinxstylestrong{pandas}. The file begins with some basic format information. \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{l+s+sd}{\PYGZdq{}\PYGZdq{}\PYGZdq{} } \PYG{l+s+sd}{This is taken from the data file of the mass 2016 evaluation. } \PYG{l+s+sd}{All files are 3436 lines long with 124 character per line. } \PYG{l+s+sd}{ Headers are 39 lines long. } \PYG{l+s+sd}{ col 1 : Fortran character control: 1 = page feed 0 = line feed } \PYG{l+s+sd}{ format : a1,i3,i5,i5,i5,1x,a3,a4,1x,f13.5,f11.5,f11.3,f9.3,1x,a2,f11.3,f9.3,1x,i3,1x,f12.5,f11.5 } \PYG{l+s+sd}{ These formats are reflected in the pandas widths variable below, see the statement } \PYG{l+s+sd}{ widths=(1,3,5,5,5,1,3,4,1,13,11,11,9,1,2,11,9,1,3,1,12,11,1), } \PYG{l+s+sd}{ Pandas has also a variable header, with length 39 in this case. } \PYG{l+s+sd}{\PYGZdq{}\PYGZdq{}\PYGZdq{}} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYGZsq{} \PYGZbs{}nThis is taken from the data file of the mass 2016 evaluation. \PYGZbs{}nAll files are 3436 lines long with 124 character per line. \PYGZbs{}n Headers are 39 lines long. \PYGZbs{}n col 1 : Fortran character control: 1 = page feed 0 = line feed \PYGZbs{}n format : a1,i3,i5,i5,i5,1x,a3,a4,1x,f13.5,f11.5,f11.3,f9.3,1x,a2,f11.3,f9.3,1x,i3,1x,f12.5,f11.5 \PYGZbs{}n These formats are reflected in the pandas widths variable below, see the statement \PYGZbs{}n widths=(1,3,5,5,5,1,3,4,1,13,11,11,9,1,2,11,9,1,3,1,12,11,1), \PYGZbs{}n Pandas has also a variable header, with length 39 in this case. \PYGZbs{}n\PYGZsq{} \end{sphinxVerbatim} The data we are interested in are in columns 2, 3, 4 and 11, giving us the number of neutrons, protons, mass numbers and binding energies, respectively. We add also for the sake of completeness the element name. The data are in fixed\sphinxhyphen{}width formatted lines and we will covert them into the \sphinxstylestrong{pandas} DataFrame structure. \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{c+c1}{\PYGZsh{} Read the experimental data with Pandas} \PYG{n}{Masses} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{read\PYGZus{}fwf}\PYG{p}{(}\PYG{n}{infile}\PYG{p}{,} \PYG{n}{usecols}\PYG{o}{=}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{4}\PYG{p}{,}\PYG{l+m+mi}{6}\PYG{p}{,}\PYG{l+m+mi}{11}\PYG{p}{)}\PYG{p}{,} \PYG{n}{names}\PYG{o}{=}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{N}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Z}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{A}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Element}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Ebinding}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{widths}\PYG{o}{=}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{5}\PYG{p}{,}\PYG{l+m+mi}{5}\PYG{p}{,}\PYG{l+m+mi}{5}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{4}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{13}\PYG{p}{,}\PYG{l+m+mi}{11}\PYG{p}{,}\PYG{l+m+mi}{11}\PYG{p}{,}\PYG{l+m+mi}{9}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{11}\PYG{p}{,}\PYG{l+m+mi}{9}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{12}\PYG{p}{,}\PYG{l+m+mi}{11}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{,} \PYG{n}{header}\PYG{o}{=}\PYG{l+m+mi}{39}\PYG{p}{,} \PYG{n}{index\PYGZus{}col}\PYG{o}{=}\PYG{k+kc}{False}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Extrapolated values are indicated by \PYGZsq{}\PYGZsh{}\PYGZsq{} in place of the decimal place, so} \PYG{c+c1}{\PYGZsh{} the Ebinding column won\PYGZsq{}t be numeric. Coerce to float and drop these entries.} \PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Ebinding}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{to\PYGZus{}numeric}\PYG{p}{(}\PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Ebinding}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{,} \PYG{n}{errors}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{coerce}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{Masses} \PYG{o}{=} \PYG{n}{Masses}\PYG{o}{.}\PYG{n}{dropna}\PYG{p}{(}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Convert from keV to MeV.} \PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Ebinding}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{o}{/}\PYG{o}{=} \PYG{l+m+mi}{1000} \PYG{c+c1}{\PYGZsh{} Group the DataFrame by nucleon number, A.} \PYG{n}{Masses} \PYG{o}{=} \PYG{n}{Masses}\PYG{o}{.}\PYG{n}{groupby}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{A}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Find the rows of the grouped DataFrame with the maximum binding energy.} \PYG{n}{Masses} \PYG{o}{=} \PYG{n}{Masses}\PYG{o}{.}\PYG{n}{apply}\PYG{p}{(}\PYG{k}{lambda} \PYG{n}{t}\PYG{p}{:} \PYG{n}{t}\PYG{p}{[}\PYG{n}{t}\PYG{o}{.}\PYG{n}{Ebinding}\PYG{o}{==}\PYG{n}{t}\PYG{o}{.}\PYG{n}{Ebinding}\PYG{o}{.}\PYG{n}{max}\PYG{p}{(}\PYG{p}{)}\PYG{p}{]}\PYG{p}{)} \end{sphinxVerbatim} We have now read in the data, grouped them according to the variables we are interested in. We see how easy it is to reorganize the data using \sphinxstylestrong{pandas}. If we were to do these operations in C/C++ or Fortran, we would have had to write various functions/subroutines which perform the above reorganizations for us. Having reorganized the data, we can now start to make some simple fits using both the functionalities in \sphinxstylestrong{numpy} and \sphinxstylestrong{Scikit\sphinxhyphen{}Learn} afterwards. Now we define five variables which contain the number of nucleons \(A\), the number of protons \(Z\) and the number of neutrons \(N\), the element name and finally the energies themselves. \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{n}{A} \PYG{o}{=} \PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{A}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{n}{Z} \PYG{o}{=} \PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Z}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{n}{N} \PYG{o}{=} \PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{N}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{n}{Element} \PYG{o}{=} \PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Element}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{n}{Energies} \PYG{o}{=} \PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Ebinding}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{Masses}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] N Z A Element Ebinding A 1 0 0 1 1 H 0.000000 2 1 1 1 2 H 1.112283 3 2 2 1 3 H 2.827265 4 6 2 2 4 He 7.073915 5 9 3 2 5 He 5.512132 ... ... ... ... ... ... 264 3304 156 108 264 Hs 7.298375 265 3310 157 108 265 Hs 7.296247 266 3317 158 108 266 Hs 7.298273 269 3338 159 110 269 Ds 7.250154 270 3344 160 110 270 Ds 7.253775 [267 rows x 5 columns] \end{sphinxVerbatim} The next step, and we will define this mathematically later, is to set up the so\sphinxhyphen{}called \sphinxstylestrong{design matrix}. We will throughout call this matrix \(\boldsymbol{X}\). It has dimensionality \(p\times n\), where \(n\) is the number of data points and \(p\) are the so\sphinxhyphen{}called predictors. In our case here they are given by the number of polynomials in \(A\) we wish to include in the fit. \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{c+c1}{\PYGZsh{} Now we set up the design matrix X} \PYG{n}{X} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{A}\PYG{p}{)}\PYG{p}{,}\PYG{l+m+mi}{5}\PYG{p}{)}\PYG{p}{)} \PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{]} \PYG{o}{=} \PYG{l+m+mi}{1} \PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{]} \PYG{o}{=} \PYG{n}{A} \PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{]} \PYG{o}{=} \PYG{n}{A}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mf}{2.0}\PYG{o}{/}\PYG{l+m+mf}{3.0}\PYG{p}{)} \PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{]} \PYG{o}{=} \PYG{n}{A}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mf}{1.0}\PYG{o}{/}\PYG{l+m+mf}{3.0}\PYG{p}{)} \PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{l+m+mi}{4}\PYG{p}{]} \PYG{o}{=} \PYG{n}{A}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mf}{1.0}\PYG{p}{)} \end{sphinxVerbatim} With \sphinxstylestrong{scikitlearn} we are now ready to use linear regression and fit our data. \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{n}{clf} \PYG{o}{=} \PYG{n}{skl}\PYG{o}{.}\PYG{n}{LinearRegression}\PYG{p}{(}\PYG{p}{)}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{Energies}\PYG{p}{)} \PYG{n}{fity} \PYG{o}{=} \PYG{n}{clf}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{X}\PYG{p}{)} \end{sphinxVerbatim} Pretty simple!Now we can print measures of how our fit is doing, the coefficients from the fits and plot the final fit together with our data. \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{c+c1}{\PYGZsh{} The mean squared error } \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Mean squared error: }\PYG{l+s+si}{\PYGZpc{}.2f}\PYG{l+s+s2}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{mean\PYGZus{}squared\PYGZus{}error}\PYG{p}{(}\PYG{n}{Energies}\PYG{p}{,} \PYG{n}{fity}\PYG{p}{)}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Explained variance score: 1 is perfect prediction } \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Variance score: }\PYG{l+s+si}{\PYGZpc{}.2f}\PYG{l+s+s1}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{r2\PYGZus{}score}\PYG{p}{(}\PYG{n}{Energies}\PYG{p}{,} \PYG{n}{fity}\PYG{p}{)}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Mean absolute error } \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Mean absolute error: }\PYG{l+s+si}{\PYGZpc{}.2f}\PYG{l+s+s1}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{mean\PYGZus{}absolute\PYGZus{}error}\PYG{p}{(}\PYG{n}{Energies}\PYG{p}{,} \PYG{n}{fity}\PYG{p}{)}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{clf}\PYG{o}{.}\PYG{n}{coef\PYGZus{}}\PYG{p}{,} \PYG{n}{clf}\PYG{o}{.}\PYG{n}{intercept\PYGZus{}}\PYG{p}{)} \PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Eapprox}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{o}{=} \PYG{n}{fity} \PYG{c+c1}{\PYGZsh{} Generate a plot comparing the experimental with the fitted values values.} \PYG{n}{fig}\PYG{p}{,} \PYG{n}{ax} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{subplots}\PYG{p}{(}\PYG{p}{)} \PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}xlabel}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{\PYGZdl{}A = N + Z\PYGZdl{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}ylabel}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{\PYGZdl{}E\PYGZus{}}\PYG{l+s+s1}{\PYGZbs{}}\PYG{l+s+s1}{mathrm}\PYG{l+s+si}{\PYGZob{}bind\PYGZcb{}}\PYG{l+s+s1}{\PYGZbs{}}\PYG{l+s+s1}{,/}\PYG{l+s+s1}{\PYGZbs{}}\PYG{l+s+s1}{mathrm}\PYG{l+s+si}{\PYGZob{}MeV\PYGZcb{}}\PYG{l+s+s1}{\PYGZdl{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{ax}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{A}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{,} \PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Ebinding}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{,} \PYG{n}{alpha}\PYG{o}{=}\PYG{l+m+mf}{0.7}\PYG{p}{,} \PYG{n}{lw}\PYG{o}{=}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Ame2016}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{ax}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{A}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{,} \PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Eapprox}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{,} \PYG{n}{alpha}\PYG{o}{=}\PYG{l+m+mf}{0.7}\PYG{p}{,} \PYG{n}{lw}\PYG{o}{=}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{c}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{m}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Fit}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{ax}\PYG{o}{.}\PYG{n}{legend}\PYG{p}{(}\PYG{p}{)} \PYG{n}{save\PYGZus{}fig}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Masses2016}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] Mean squared error: 0.04 Variance score: 0.95 Mean absolute error: 0.05 [ 0.00000000e+00 7.06492086e\PYGZhy{}03 \PYGZhy{}1.73091052e\PYGZhy{}01 \PYGZhy{}1.66020213e+01 1.17385778e+00] 15.212327334149508 \end{sphinxVerbatim} \noindent\sphinxincludegraphics{{chapter2_114_1}.png} \subsubsection{Seeing the wood for the trees} \label{\detokenize{chapter2:seeing-the-wood-for-the-trees}} As a teaser, let us now see how we can do this with decision trees using \sphinxstylestrong{scikit\sphinxhyphen{}learn}. Later we will switch to so\sphinxhyphen{}called \sphinxstylestrong{random forests}! \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{c+c1}{\PYGZsh{}Decision Tree Regression} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{tree} \PYG{k+kn}{import} \PYG{n}{DecisionTreeRegressor} \PYG{n}{regr\PYGZus{}1}\PYG{o}{=}\PYG{n}{DecisionTreeRegressor}\PYG{p}{(}\PYG{n}{max\PYGZus{}depth}\PYG{o}{=}\PYG{l+m+mi}{5}\PYG{p}{)} \PYG{n}{regr\PYGZus{}2}\PYG{o}{=}\PYG{n}{DecisionTreeRegressor}\PYG{p}{(}\PYG{n}{max\PYGZus{}depth}\PYG{o}{=}\PYG{l+m+mi}{7}\PYG{p}{)} \PYG{n}{regr\PYGZus{}3}\PYG{o}{=}\PYG{n}{DecisionTreeRegressor}\PYG{p}{(}\PYG{n}{max\PYGZus{}depth}\PYG{o}{=}\PYG{l+m+mi}{9}\PYG{p}{)} \PYG{n}{regr\PYGZus{}1}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{Energies}\PYG{p}{)} \PYG{n}{regr\PYGZus{}2}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{Energies}\PYG{p}{)} \PYG{n}{regr\PYGZus{}3}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{Energies}\PYG{p}{)} \PYG{n}{y\PYGZus{}1} \PYG{o}{=} \PYG{n}{regr\PYGZus{}1}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{X}\PYG{p}{)} \PYG{n}{y\PYGZus{}2} \PYG{o}{=} \PYG{n}{regr\PYGZus{}2}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{X}\PYG{p}{)} \PYG{n}{y\PYGZus{}3}\PYG{o}{=}\PYG{n}{regr\PYGZus{}3}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{X}\PYG{p}{)} \PYG{n}{Masses}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Eapprox}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{o}{=} \PYG{n}{y\PYGZus{}3} \PYG{c+c1}{\PYGZsh{} Plot the results} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{figure}\PYG{p}{(}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{A}\PYG{p}{,} \PYG{n}{Energies}\PYG{p}{,} \PYG{n}{color}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{blue}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Data}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{linewidth}\PYG{o}{=}\PYG{l+m+mi}{2}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{A}\PYG{p}{,} \PYG{n}{y\PYGZus{}1}\PYG{p}{,} \PYG{n}{color}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{red}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{max\PYGZus{}depth=5}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{linewidth}\PYG{o}{=}\PYG{l+m+mi}{2}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{A}\PYG{p}{,} \PYG{n}{y\PYGZus{}2}\PYG{p}{,} \PYG{n}{color}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{green}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{max\PYGZus{}depth=7}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{linewidth}\PYG{o}{=}\PYG{l+m+mi}{2}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{A}\PYG{p}{,} \PYG{n}{y\PYGZus{}3}\PYG{p}{,} \PYG{n}{color}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{m}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{max\PYGZus{}depth=9}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{linewidth}\PYG{o}{=}\PYG{l+m+mi}{2}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{xlabel}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{\PYGZdl{}A\PYGZdl{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{ylabel}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{\PYGZdl{}E\PYGZdl{}[MeV]}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{title}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Decision Tree Regression}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{legend}\PYG{p}{(}\PYG{p}{)} \PYG{n}{save\PYGZus{}fig}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Masses2016Trees}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{Masses}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(} \PYG{p}{(}\PYG{n}{Energies}\PYG{o}{\PYGZhy{}}\PYG{n}{y\PYGZus{}1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)} \end{sphinxVerbatim} \noindent\sphinxincludegraphics{{chapter2_116_0}.png} \begin{sphinxVerbatim}[commandchars=\\\{\}] N Z A Element Ebinding Eapprox A 1 0 0 1 1 H 0.000000 0.000000 2 1 1 1 2 H 1.112283 1.112283 3 2 2 1 3 H 2.827265 2.827265 4 6 2 2 4 He 7.073915 7.073915 5 9 3 2 5 He 5.512132 5.512132 ... ... ... ... ... ... ... 264 3304 156 108 264 Hs 7.298375 7.298375 265 3310 157 108 265 Hs 7.296247 7.297260 266 3317 158 108 266 Hs 7.298273 7.297260 269 3338 159 110 269 Ds 7.250154 7.250154 270 3344 160 110 270 Ds 7.253775 7.253775 [267 rows x 6 columns] 0.00988361564671618 \end{sphinxVerbatim} \subsubsection{And what about using neural networks?} \label{\detokenize{chapter2:and-what-about-using-neural-networks}} The \sphinxstylestrong{seaborn} package allows us to visualize data in an efficient way. Note that we use \sphinxstylestrong{scikit\sphinxhyphen{}learn}’s multi\sphinxhyphen{}layer perceptron (or feed forward neural network) functionality. \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{neural\PYGZus{}network} \PYG{k+kn}{import} \PYG{n}{MLPRegressor} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{metrics} \PYG{k+kn}{import} \PYG{n}{accuracy\PYGZus{}score} \PYG{k+kn}{import} \PYG{n+nn}{seaborn} \PYG{k}{as} \PYG{n+nn}{sns} \PYG{n}{X\PYGZus{}train} \PYG{o}{=} \PYG{n}{X} \PYG{n}{Y\PYGZus{}train} \PYG{o}{=} \PYG{n}{Energies} \PYG{n}{n\PYGZus{}hidden\PYGZus{}neurons} \PYG{o}{=} \PYG{l+m+mi}{100} \PYG{n}{epochs} \PYG{o}{=} \PYG{l+m+mi}{100} \PYG{c+c1}{\PYGZsh{} store models for later use} \PYG{n}{eta\PYGZus{}vals} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{logspace}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{5}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{7}\PYG{p}{)} \PYG{n}{lmbd\PYGZus{}vals} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{logspace}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{5}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{7}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} store the models for later use} \PYG{n}{DNN\PYGZus{}scikit} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{eta\PYGZus{}vals}\PYG{p}{)}\PYG{p}{,} \PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{lmbd\PYGZus{}vals}\PYG{p}{)}\PYG{p}{)}\PYG{p}{,} \PYG{n}{dtype}\PYG{o}{=}\PYG{n+nb}{object}\PYG{p}{)} \PYG{n}{train\PYGZus{}accuracy} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{eta\PYGZus{}vals}\PYG{p}{)}\PYG{p}{,} \PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{lmbd\PYGZus{}vals}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)} \PYG{n}{sns}\PYG{o}{.}\PYG{n}{set}\PYG{p}{(}\PYG{p}{)} \PYG{k}{for} \PYG{n}{i}\PYG{p}{,} \PYG{n}{eta} \PYG{o+ow}{in} \PYG{n+nb}{enumerate}\PYG{p}{(}\PYG{n}{eta\PYGZus{}vals}\PYG{p}{)}\PYG{p}{:} \PYG{k}{for} \PYG{n}{j}\PYG{p}{,} \PYG{n}{lmbd} \PYG{o+ow}{in} \PYG{n+nb}{enumerate}\PYG{p}{(}\PYG{n}{lmbd\PYGZus{}vals}\PYG{p}{)}\PYG{p}{:} \PYG{n}{dnn} \PYG{o}{=} \PYG{n}{MLPRegressor}\PYG{p}{(}\PYG{n}{hidden\PYGZus{}layer\PYGZus{}sizes}\PYG{o}{=}\PYG{p}{(}\PYG{n}{n\PYGZus{}hidden\PYGZus{}neurons}\PYG{p}{)}\PYG{p}{,} \PYG{n}{activation}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{logistic}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{alpha}\PYG{o}{=}\PYG{n}{lmbd}\PYG{p}{,} \PYG{n}{learning\PYGZus{}rate\PYGZus{}init}\PYG{o}{=}\PYG{n}{eta}\PYG{p}{,} \PYG{n}{max\PYGZus{}iter}\PYG{o}{=}\PYG{n}{epochs}\PYG{p}{)} \PYG{n}{dnn}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{p}{,} \PYG{n}{Y\PYGZus{}train}\PYG{p}{)} \PYG{n}{DNN\PYGZus{}scikit}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{[}\PYG{n}{j}\PYG{p}{]} \PYG{o}{=} \PYG{n}{dnn} \PYG{n}{train\PYGZus{}accuracy}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{[}\PYG{n}{j}\PYG{p}{]} \PYG{o}{=} \PYG{n}{dnn}\PYG{o}{.}\PYG{n}{score}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{p}{,} \PYG{n}{Y\PYGZus{}train}\PYG{p}{)} \PYG{n}{fig}\PYG{p}{,} \PYG{n}{ax} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{subplots}\PYG{p}{(}\PYG{n}{figsize} \PYG{o}{=} \PYG{p}{(}\PYG{l+m+mi}{10}\PYG{p}{,} \PYG{l+m+mi}{10}\PYG{p}{)}\PYG{p}{)} \PYG{n}{sns}\PYG{o}{.}\PYG{n}{heatmap}\PYG{p}{(}\PYG{n}{train\PYGZus{}accuracy}\PYG{p}{,} \PYG{n}{annot}\PYG{o}{=}\PYG{k+kc}{True}\PYG{p}{,} \PYG{n}{ax}\PYG{o}{=}\PYG{n}{ax}\PYG{p}{,} \PYG{n}{cmap}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{viridis}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)} \PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}title}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Training Accuracy}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)} \PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}ylabel}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{\PYGZdl{}}\PYG{l+s+s2}{\PYGZbs{}}\PYG{l+s+s2}{eta\PYGZdl{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)} \PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}xlabel}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{\PYGZdl{}}\PYG{l+s+s2}{\PYGZbs{}}\PYG{l+s+s2}{lambda\PYGZdl{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( /Users/hjensen/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/sklearn/neural\PYGZus{}network/\PYGZus{}multilayer\PYGZus{}perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (100) reached and the optimization hasn\PYGZsq{}t converged yet. warnings.warn( \end{sphinxVerbatim} \noindent\sphinxincludegraphics{{chapter2_118_16}.png} \subsection{A first summary} \label{\detokenize{chapter2:a-first-summary}} The aim behind these introductory words was to present to you various Python libraries and their functionalities, in particular libraries like \sphinxstylestrong{numpy}, \sphinxstylestrong{pandas}, \sphinxstylestrong{xarray} and \sphinxstylestrong{matplotlib} and other that make our life much easier in handling various data sets and visualizing data. Furthermore, \sphinxstylestrong{Scikit\sphinxhyphen{}Learn} allows us with few lines of code to implement popular Machine Learning algorithms for supervised learning. Later we will meet \sphinxstylestrong{Tensorflow}, a powerful library for deep learning. Now it is time to dive more into the details of various methods. We will start with linear regression and try to take a deeper look at what it entails. \section{Data Analysis and Machine Learning: Elements of Probability Theory and Statistical Data Analysis} \label{\detokenize{chapter3:data-analysis-and-machine-learning-elements-of-probability-theory-and-statistical-data-analysis}}\label{\detokenize{chapter3::doc}} \sphinxstylestrong{Morten Hjorth\sphinxhyphen{}Jensen}, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University Date: \sphinxstylestrong{Sep 20, 2020} Copyright 1999\sphinxhyphen{}2020, Morten Hjorth\sphinxhyphen{}Jensen. Released under CC Attribution\sphinxhyphen{}NonCommercial 4.0 license \subsection{To do list} \label{\detokenize{chapter3:to-do-list}}\begin{itemize} \item {} add math about MVN and define MLE and other quantities \item {} rewrite about covariance matrix \item {} add KL theorem \end{itemize} \subsection{Domains and probabilities} \label{\detokenize{chapter3:domains-and-probabilities}} Consider the following simple example, namely the tossing of two dice, resulting in the following possible values \begin{equation*} \begin{split} \{2,3,4,5,6,7,8,9,10,11,12\}. \end{split} \end{equation*} These values are called the \sphinxstyleemphasis{domain}. To this domain we have the corresponding \sphinxstyleemphasis{probabilities} \begin{equation*} \begin{split} \{1/36,2/36/,3/36,4/36,5/36,6/36,5/36,4/36,3/36,2/36,1/36\}. \end{split} \end{equation*} \subsection{Tossing the dice} \label{\detokenize{chapter3:tossing-the-dice}} The numbers in the domain are the outcomes of the physical process of tossing say two dice. We cannot tell beforehand whether the outcome is 3 or 5 or any other number in this domain. This defines the randomness of the outcome, or unexpectedness or any other synonimous word which encompasses the uncertitude of the final outcome. The only thing we can tell beforehand is that say the outcome 2 has a certain probability.If our favorite hobby is to spend an hour every evening throwing dice and registering the sequence of outcomes, we will note that the numbers in the above domain \begin{equation*} \begin{split} \{2,3,4,5,6,7,8,9,10,11,12\}, \end{split} \end{equation*} appear in a random order. After 11 throws the results may look like \begin{equation*} \begin{split} \{10,8,6,3,6,9,11,8,12,4,5\}. \end{split} \end{equation*} \subsection{Stochastic variables} \label{\detokenize{chapter3:stochastic-variables}} \sphinxstylestrong{Random variables are characterized by a domain which contains all possible values that the random value may take. This domain has a corresponding probability distribution function(PDF)}. \subsection{Stochastic variables and the main concepts, the discrete case} \label{\detokenize{chapter3:stochastic-variables-and-the-main-concepts-the-discrete-case}} There are two main concepts associated with a stochastic variable. The \sphinxstyleemphasis{domain} is the set \(\mathbb D = \{x\}\) of all accessible values the variable can assume, so that \(X \in \mathbb D\). An example of a discrete domain is the set of six different numbers that we may get by throwing of a dice, \(x\in\{1,\,2,\,3,\,4,\,5,\,6\}\). The \sphinxstyleemphasis{probability distribution function (PDF)} is a function \(p(x)\) on the domain which, in the discrete case, gives us the probability or relative frequency with which these values of \(X\) occur \begin{equation*} \begin{split} p(x) = \mathrm{Prob}(X=x). \end{split} \end{equation*} \subsection{Stochastic variables and the main concepts, the continuous case} \label{\detokenize{chapter3:stochastic-variables-and-the-main-concepts-the-continuous-case}} In the continuous case, the PDF does not directly depict the actual probability. Instead we define the probability for the stochastic variable to assume any value on an infinitesimal interval around \(x\) to be \(p(x)dx\). The continuous function \(p(x)\) then gives us the \sphinxstyleemphasis{density} of the probability rather than the probability itself. The probability for a stochastic variable to assume any value on a non\sphinxhyphen{}infinitesimal interval \([a,\,b]\) is then just the integral \begin{equation*} \begin{split} \mathrm{Prob}(a\leq X\leq b) = \int_a^b p(x)dx. \end{split} \end{equation*} Qualitatively speaking, a stochastic variable represents the values of numbers chosen as if by chance from some specified PDF so that the selection of a large set of these numbers reproduces this PDF. \subsection{The cumulative probability} \label{\detokenize{chapter3:the-cumulative-probability}} Of interest to us is the \sphinxstyleemphasis{cumulative probability distribution function} (\sphinxstylestrong{CDF}), \(P(x)\), which is just the probability for a stochastic variable \(X\) to assume any value less than \(x\) \begin{equation*} \begin{split} P(x)=\mathrm{Prob(}X\leq x\mathrm{)} = \int_{-\infty}^x p(x^{\prime})dx^{\prime}. \end{split} \end{equation*} The relation between a CDF and its corresponding PDF is then \begin{equation*} \begin{split} p(x) = \frac{d}{dx}P(x). \end{split} \end{equation*} \subsection{Properties of PDFs} \label{\detokenize{chapter3:properties-of-pdfs}} There are two properties that all PDFs must satisfy. The first one is positivity (assuming that the PDF is normalized) \begin{equation*} \begin{split} 0 \leq p(x) \leq 1. \end{split} \end{equation*} Naturally, it would be nonsensical for any of the values of the domain to occur with a probability greater than \(1\) or less than \(0\). Also, the PDF must be normalized. That is, all the probabilities must add up to unity. The probability of “anything” to happen is always unity. For both discrete and continuous PDFs, this condition is \begin{equation*} \begin{split} \begin{align*} \sum_{x_i\in\mathbb D} p(x_i) & = 1,\\ \int_{x\in\mathbb D} p(x)\,dx & = 1. \end{align*} \end{split} \end{equation*} \subsection{Important distributions, the uniform distribution} \label{\detokenize{chapter3:important-distributions-the-uniform-distribution}} The first one is the most basic PDF; namely the uniform distribution \begin{equation*} \begin{split} \begin{equation} p(x) = \frac{1}{b-a}\theta(x-a)\theta(b-x). \label{eq:unifromPDF} \tag{1} \end{equation} \end{split} \end{equation*} For \(a=0\) and \(b=1\) we have \begin{equation*} \begin{split} \begin{array}{ll} p(x)dx = dx & \in [0,1]. \end{array} \end{split} \end{equation*} The latter distribution is used to generate random numbers. For other PDFs, one needs normally a mapping from this distribution to say for example the exponential distribution. \subsection{Gaussian distribution} \label{\detokenize{chapter3:gaussian-distribution}} The second one is the Gaussian Distribution \begin{equation*} \begin{split} p(x) = \frac{1}{\sigma\sqrt{2\pi}} \exp{(-\frac{(x-\mu)^2}{2\sigma^2})}, \end{split} \end{equation*} with mean value \(\mu\) and standard deviation \(\sigma\). If \(\mu=0\) and \(\sigma=1\), it is normally called the \sphinxstylestrong{standard normal distribution} \begin{equation*} \begin{split} p(x) = \frac{1}{\sqrt{2\pi}} \exp{(-\frac{x^2}{2})}, \end{split} \end{equation*} The following simple Python code plots the above distribution for different values of \(\mu\) and \(\sigma\). \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{o}{\PYGZpc{}}\PYG{k}{matplotlib} inline \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{k+kn}{from} \PYG{n+nn}{math} \PYG{k+kn}{import} \PYG{n}{acos}\PYG{p}{,} \PYG{n}{exp}\PYG{p}{,} \PYG{n}{sqrt} \PYG{k+kn}{from} \PYG{n+nn}{matplotlib} \PYG{k+kn}{import} \PYG{n}{pyplot} \PYG{k}{as} \PYG{n}{plt} \PYG{k+kn}{from} \PYG{n+nn}{matplotlib} \PYG{k+kn}{import} \PYG{n}{rc}\PYG{p}{,} \PYG{n}{rcParams} \PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{units} \PYG{k}{as} \PYG{n+nn}{units} \PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{ticker} \PYG{k}{as} \PYG{n+nn}{ticker} \PYG{n}{rc}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{text}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,}\PYG{n}{usetex}\PYG{o}{=}\PYG{k+kc}{True}\PYG{p}{)} \PYG{n}{rc}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{font}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,}\PYG{o}{*}\PYG{o}{*}\PYG{p}{\PYGZob{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{family}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{:}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{serif}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{serif}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{:}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Gaussian distribution}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{\PYGZcb{}}\PYG{p}{)} \PYG{n}{font} \PYG{o}{=} \PYG{p}{\PYGZob{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{family}\PYG{l+s+s1}{\PYGZsq{}} \PYG{p}{:} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{serif}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{color}\PYG{l+s+s1}{\PYGZsq{}} \PYG{p}{:} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{darkred}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{weight}\PYG{l+s+s1}{\PYGZsq{}} \PYG{p}{:} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{normal}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{size}\PYG{l+s+s1}{\PYGZsq{}} \PYG{p}{:} \PYG{l+m+mi}{16}\PYG{p}{,} \PYG{p}{\PYGZcb{}} \PYG{n}{pi} \PYG{o}{=} \PYG{n}{acos}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mf}{1.0}\PYG{p}{)} \PYG{n}{mu0} \PYG{o}{=} \PYG{l+m+mf}{0.0} \PYG{n}{sigma0} \PYG{o}{=} \PYG{l+m+mf}{1.0} \PYG{n}{mu1}\PYG{o}{=} \PYG{l+m+mf}{1.0} \PYG{n}{sigma1} \PYG{o}{=} \PYG{l+m+mf}{2.0} \PYG{n}{mu2} \PYG{o}{=} \PYG{l+m+mf}{2.0} \PYG{n}{sigma2} \PYG{o}{=} \PYG{l+m+mf}{4.0} \PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{linspace}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mf}{20.0}\PYG{p}{,} \PYG{l+m+mf}{20.0}\PYG{p}{)} \PYG{n}{v0} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{exp}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{x}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{n}{mu0}\PYG{o}{+}\PYG{n}{mu0}\PYG{o}{*}\PYG{n}{mu0}\PYG{p}{)}\PYG{o}{/}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{sigma0}\PYG{o}{*}\PYG{n}{sigma0}\PYG{p}{)}\PYG{p}{)}\PYG{o}{/}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{pi}\PYG{o}{*}\PYG{n}{sigma0}\PYG{o}{*}\PYG{n}{sigma0}\PYG{p}{)} \PYG{n}{v1} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{exp}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{x}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{n}{mu1}\PYG{o}{+}\PYG{n}{mu1}\PYG{o}{*}\PYG{n}{mu1}\PYG{p}{)}\PYG{o}{/}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{sigma1}\PYG{o}{*}\PYG{n}{sigma1}\PYG{p}{)}\PYG{p}{)}\PYG{o}{/}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{pi}\PYG{o}{*}\PYG{n}{sigma1}\PYG{o}{*}\PYG{n}{sigma1}\PYG{p}{)} \PYG{n}{v2} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{exp}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{x}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{n}{mu2}\PYG{o}{+}\PYG{n}{mu2}\PYG{o}{*}\PYG{n}{mu2}\PYG{p}{)}\PYG{o}{/}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{sigma2}\PYG{o}{*}\PYG{n}{sigma2}\PYG{p}{)}\PYG{p}{)}\PYG{o}{/}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{pi}\PYG{o}{*}\PYG{n}{sigma2}\PYG{o}{*}\PYG{n}{sigma2}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{v0}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{b\PYGZhy{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{n}{v1}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{r\PYGZhy{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{n}{v2}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{g\PYGZhy{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{title}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{\PYGZob{}}\PYG{l+s+s1}{\PYGZbs{}}\PYG{l+s+s1}{bf Gaussian distributions\PYGZcb{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{20}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{text}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{19}\PYG{p}{,} \PYG{l+m+mf}{0.3}\PYG{p}{,} \PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Parameters: \PYGZdl{}}\PYG{l+s+s1}{\PYGZbs{}}\PYG{l+s+s1}{mu = 0\PYGZdl{}, \PYGZdl{}}\PYG{l+s+s1}{\PYGZbs{}}\PYG{l+s+s1}{sigma = 1\PYGZdl{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{fontdict}\PYG{o}{=}\PYG{n}{font}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{text}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{19}\PYG{p}{,} \PYG{l+m+mf}{0.18}\PYG{p}{,} \PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Parameters: \PYGZdl{}}\PYG{l+s+s1}{\PYGZbs{}}\PYG{l+s+s1}{mu = 1\PYGZdl{}, \PYGZdl{}}\PYG{l+s+s1}{\PYGZbs{}}\PYG{l+s+s1}{sigma = 2\PYGZdl{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{fontdict}\PYG{o}{=}\PYG{n}{font}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{text}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{19}\PYG{p}{,} \PYG{l+m+mf}{0.08}\PYG{p}{,} \PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Parameters: \PYGZdl{}}\PYG{l+s+s1}{\PYGZbs{}}\PYG{l+s+s1}{mu = 2\PYGZdl{}, \PYGZdl{}}\PYG{l+s+s1}{\PYGZbs{}}\PYG{l+s+s1}{sigma = 4\PYGZdl{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{fontdict}\PYG{o}{=}\PYG{n}{font}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{xlabel}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{\PYGZdl{}x\PYGZdl{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,}\PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{20}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{ylabel}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{\PYGZdl{}p(x)\PYGZdl{} [MeV]}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,}\PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{20}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Tweak spacing to prevent clipping of ylabel } \PYG{n}{plt}\PYG{o}{.}\PYG{n}{subplots\PYGZus{}adjust}\PYG{p}{(}\PYG{n}{left}\PYG{o}{=}\PYG{l+m+mf}{0.15}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{savefig}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{gaussian.pdf}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n+nb}{format}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{pdf}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \end{sphinxVerbatim} \noindent\sphinxincludegraphics{{chapter3_29_0}.png} \subsection{Exponential distribution} \label{\detokenize{chapter3:exponential-distribution}} Another important distribution in science is the exponential distribution \begin{equation*} \begin{split} p(x) = \alpha\exp{-(\alpha x)}. \end{split} \end{equation*} \subsection{Expectation values} \label{\detokenize{chapter3:expectation-values}} Let \(h(x)\) be an arbitrary continuous function on the domain of the stochastic variable \(X\) whose PDF is \(p(x)\). We define the \sphinxstyleemphasis{expectation value} of \(h\) with respect to \(p\) as follows \begin{equation*} \begin{split} \begin{equation} \langle h \rangle_X \equiv \int\! h(x)p(x)\,dx \label{eq:expectation_value_of_h_wrt_p} \tag{2} \end{equation} \end{split} \end{equation*} Whenever the PDF is known implicitly, like in this case, we will drop the index \(X\) for clarity.A particularly useful class of special expectation values are the \sphinxstyleemphasis{moments}. The \(n\)\sphinxhyphen{}th moment of the PDF \(p\) is defined as follows \begin{equation*} \begin{split} \langle x^n \rangle \equiv \int\! x^n p(x)\,dx \end{split} \end{equation*} \subsection{Stochastic variables and the main concepts, mean values} \label{\detokenize{chapter3:stochastic-variables-and-the-main-concepts-mean-values}} The zero\sphinxhyphen{}th moment \(\langle 1\rangle\) is just the normalization condition of \(p\). The first moment, \(\langle x\rangle\), is called the \sphinxstyleemphasis{mean} of \(p\) and often denoted by the letter \(\mu\) \begin{equation*} \begin{split} \langle x\rangle = \mu \equiv \int x p(x)dx, \end{split} \end{equation*} for a continuous distribution and \begin{equation*} \begin{split} \langle x\rangle = \mu \equiv \sum_{i=1}^N x_i p(x_i), \end{split} \end{equation*} for a discrete distribution. Qualitatively it represents the centroid or the average value of the PDF and is therefore simply called the expectation value of \(p(x)\). \subsection{Stochastic variables and the main concepts, central moments, the variance} \label{\detokenize{chapter3:stochastic-variables-and-the-main-concepts-central-moments-the-variance}} A special version of the moments is the set of \sphinxstyleemphasis{central moments}, the n\sphinxhyphen{}th central moment defined as \begin{equation*} \begin{split} \langle (x-\langle x\rangle )^n\rangle \equiv \int\! (x-\langle x\rangle)^n p(x)\,dx \end{split} \end{equation*} The zero\sphinxhyphen{}th and first central moments are both trivial, equal \(1\) and \(0\), respectively. But the second central moment, known as the \sphinxstyleemphasis{variance} of \(p\), is of particular interest. For the stochastic variable \(X\), the variance is denoted as \(\sigma^2_X\) or \(\mathrm{Var}(X)\) \begin{equation*} \begin{split} \begin{align*} \sigma^2_X &=\mathrm{Var}(X) = \langle (x-\langle x\rangle)^2\rangle = \int (x-\langle x\rangle)^2 p(x)dx\\ & = \int\left(x^2 - 2 x \langle x\rangle^{2} +\langle x\rangle^2\right)p(x)dx\\ & = \langle x^2\rangle - 2 \langle x\rangle\langle x\rangle + \langle x\rangle^2\\ & = \langle x^2 \rangle - \langle x\rangle^2 \end{align*} \end{split} \end{equation*} The square root of the variance, \(\sigma =\sqrt{\langle (x-\langle x\rangle)^2\rangle}\) is called the \sphinxstylestrong{standard deviation} of \(p\). It is the RMS (root\sphinxhyphen{}mean\sphinxhyphen{}square) value of the deviation of the PDF from its mean value, interpreted qualitatively as the “spread” of \(p\) around its mean. \subsection{Probability Distribution Functions} \label{\detokenize{chapter3:probability-distribution-functions}} The following table collects properties of probability distribution functions. In our notation we reserve the label \(p(x)\) for the probability of a certain event, while \(P(x)\) is the cumulative probability. \subsection{Probability Distribution Functions} \label{\detokenize{chapter3:id1}} With a PDF we can compute expectation values of selected quantities such as \begin{equation*} \begin{split} \langle x^k\rangle=\sum_{i=1}^{N}x_i^kp(x_i), \end{split} \end{equation*} if we have a discrete PDF or \begin{equation*} \begin{split} \langle x^k\rangle=\int_a^b x^kp(x)dx, \end{split} \end{equation*} in the case of a continuous PDF. We have already defined the mean value \(\mu\) and the variance \(\sigma^2\). \subsection{The three famous Probability Distribution Functions} \label{\detokenize{chapter3:the-three-famous-probability-distribution-functions}} There are at least three PDFs which one may encounter. These are the \sphinxstylestrong{Uniform distribution} \begin{equation*} \begin{split} p(x)=\frac{1}{b-a}\Theta(x-a)\Theta(b-x), \end{split} \end{equation*} yielding probabilities different from zero in the interval \([a,b]\). \sphinxstylestrong{The exponential distribution} \begin{equation*} \begin{split} p(x)=\alpha \exp{(-\alpha x)}, \end{split} \end{equation*} yielding probabilities different from zero in the interval \([0,\infty)\) and with mean value \begin{equation*} \begin{split} \mu = \int_0^{\infty}xp(x)dx=\int_0^{\infty}x\alpha \exp{(-\alpha x)}dx=\frac{1}{\alpha}, \end{split} \end{equation*} with variance \begin{equation*} \begin{split} \sigma^2=\int_0^{\infty}x^2p(x)dx-\mu^2 = \frac{1}{\alpha^2}. \end{split} \end{equation*} \subsection{Probability Distribution Functions, the normal distribution} \label{\detokenize{chapter3:probability-distribution-functions-the-normal-distribution}} Finally, we have the so\sphinxhyphen{}called univariate normal distribution, or just the \sphinxstylestrong{normal distribution} \begin{equation*} \begin{split} p(x)=\frac{1}{b\sqrt{2\pi}}\exp{\left(-\frac{(x-a)^2}{2b^2}\right)} \end{split} \end{equation*} with probabilities different from zero in the interval \((-\infty,\infty)\). The integral \(\int_{-\infty}^{\infty}\exp{\left(-(x^2\right)}dx\) appears in many calculations, its value is \(\sqrt{\pi}\), a result we will need when we compute the mean value and the variance. The mean value is \begin{equation*} \begin{split} \mu = \int_0^{\infty}xp(x)dx=\frac{1}{b\sqrt{2\pi}}\int_{-\infty}^{\infty}x \exp{\left(-\frac{(x-a)^2}{2b^2}\right)}dx, \end{split} \end{equation*} which becomes with a suitable change of variables \begin{equation*} \begin{split} \mu =\frac{1}{b\sqrt{2\pi}}\int_{-\infty}^{\infty}b\sqrt{2}(a+b\sqrt{2}y)\exp{-y^2}dy=a. \end{split} \end{equation*} \subsection{Probability Distribution Functions, the normal distribution} \label{\detokenize{chapter3:id2}} Similarly, the variance becomes \begin{equation*} \begin{split} \sigma^2 = \frac{1}{b\sqrt{2\pi}}\int_{-\infty}^{\infty}(x-\mu)^2 \exp{\left(-\frac{(x-a)^2}{2b^2}\right)}dx, \end{split} \end{equation*} and inserting the mean value and performing a variable change we obtain \begin{equation*} \begin{split} \sigma^2 = \frac{1}{b\sqrt{2\pi}}\int_{-\infty}^{\infty}b\sqrt{2}(b\sqrt{2}y)^2\exp{\left(-y^2\right)}dy= \frac{2b^2}{\sqrt{\pi}}\int_{-\infty}^{\infty}y^2\exp{\left(-y^2\right)}dy, \end{split} \end{equation*} and performing a final integration by parts we obtain the well\sphinxhyphen{}known result \(\sigma^2=b^2\). It is useful to introduce the standard normal distribution as well, defined by \(\mu=a=0\), viz. a distribution centered around zero and with a variance \(\sigma^2=1\), leading to \begin{equation*} \begin{split} \begin{equation} p(x)=\frac{1}{\sqrt{2\pi}}\exp{\left(-\frac{x^2}{2}\right)}. \label{_auto1} \tag{3} \end{equation} \end{split} \end{equation*} \subsection{Probability Distribution Functions, the cumulative distribution} \label{\detokenize{chapter3:probability-distribution-functions-the-cumulative-distribution}} The exponential and uniform distributions have simple cumulative functions, whereas the normal distribution does not, being proportional to the so\sphinxhyphen{}called error function \(erf(x)\), given by \begin{equation*} \begin{split} P(x) = \frac{1}{\sqrt{2\pi}}\int_{-\infty}^x\exp{\left(-\frac{t^2}{2}\right)}dt, \end{split} \end{equation*} which is difficult to evaluate in a quick way. \subsection{Probability Distribution Functions, other important distribution} \label{\detokenize{chapter3:probability-distribution-functions-other-important-distribution}} Some other PDFs which one encounters often in the natural sciences are the binomial distribution \begin{equation*} \begin{split} p(x) = \left(\begin{array}{c} n \\ x\end{array}\right)y^x(1-y)^{n-x} \hspace{0.5cm}x=0,1,\dots,n, \end{split} \end{equation*} where \(y\) is the probability for a specific event, such as the tossing of a coin or moving left or right in case of a random walker. Note that \(x\) is a discrete stochastic variable. The sequence of binomial trials is characterized by the following definitions \begin{itemize} \item {} Every experiment is thought to consist of \(N\) independent trials. \item {} In every independent trial one registers if a specific situation happens or not, such as the jump to the left or right of a random walker. \item {} The probability for every outcome in a single trial has the same value, for example the outcome of tossing (either heads or tails) a coin is always \(1/2\). \end{itemize} \subsection{Probability Distribution Functions, the binomial distribution} \label{\detokenize{chapter3:probability-distribution-functions-the-binomial-distribution}} In order to compute the mean and variance we need to recall Newton’s binomial formula \begin{equation*} \begin{split} (a+b)^m=\sum_{n=0}^m \left(\begin{array}{c} m \\ n\end{array}\right)a^nb^{m-n}, \end{split} \end{equation*} which can be used to show that \begin{equation*} \begin{split} \sum_{x=0}^n\left(\begin{array}{c} n \\ x\end{array}\right)y^x(1-y)^{n-x} = (y+1-y)^n = 1, \end{split} \end{equation*} the PDF is normalized to one. The mean value is \begin{equation*} \begin{split} \mu = \sum_{x=0}^n x\left(\begin{array}{c} n \\ x\end{array}\right)y^x(1-y)^{n-x} = \sum_{x=0}^n x\frac{n!}{x!(n-x)!}y^x(1-y)^{n-x}, \end{split} \end{equation*} resulting in \begin{equation*} \begin{split} \mu = \sum_{x=0}^n x\frac{(n-1)!}{(x-1)!(n-1-(x-1))!}y^{x-1}(1-y)^{n-1-(x-1)}, \end{split} \end{equation*} which we rewrite as \begin{equation*} \begin{split} \mu=ny\sum_{\nu=0}^n\left(\begin{array}{c} n-1 \\ \nu\end{array}\right)y^{\nu}(1-y)^{n-1-\nu} =ny(y+1-y)^{n-1}=ny. \end{split} \end{equation*} The variance is slightly trickier to get. It reads \(\sigma^2=ny(1-y)\). \subsection{Probability Distribution Functions, Poisson’s distribution} \label{\detokenize{chapter3:probability-distribution-functions-poisson-s-distribution}} Another important distribution with discrete stochastic variables \(x\) isthe Poisson model, which resembles the exponential distribution and reads \begin{equation*} \begin{split} p(x) = \frac{\lambda^x}{x!} e^{-\lambda} \hspace{0.5cm}x=0,1,\dots,;\lambda > 0. \end{split} \end{equation*} In this case both the mean value and the variance are easier to calculate, \begin{equation*} \begin{split} \mu = \sum_{x=0}^{\infty} x \frac{\lambda^x}{x!} e^{-\lambda} = \lambda e^{-\lambda}\sum_{x=1}^{\infty} \frac{\lambda^{x-1}}{(x-1)!}=\lambda, \end{split} \end{equation*} and the variance is \(\sigma^2=\lambda\). \subsection{Probability Distribution Functions, Poisson’s distribution} \label{\detokenize{chapter3:id3}} An example of applications of the Poisson distribution could be the counting of the number of \(\alpha\)\sphinxhyphen{}particles emitted from a radioactive source in a given time interval. In the limit of \(n\rightarrow \infty\) and for small probabilities \(y\), the binomial distribution approaches the Poisson distribution. Setting \(\lambda = ny\), with \(y\) the probability for an event in the binomial distribution we can show that \begin{equation*} \begin{split} \lim_{n\rightarrow \infty}\left(\begin{array}{c} n \\ x\end{array}\right)y^x(1-y)^{n-x} e^{-\lambda}=\sum_{x=1}^{\infty}\frac{\lambda^x}{x!} e^{-\lambda}. \end{split} \end{equation*} \subsection{Meet the covariance!} \label{\detokenize{chapter3:meet-the-covariance}} An important quantity in a statistical analysis is the so\sphinxhyphen{}called covariance. Consider the set \(\{X_i\}\) of \(n\) stochastic variables (not necessarily uncorrelated) with the multivariate PDF \(P(x_1,\dots,x_n)\). The \sphinxstyleemphasis{covariance} of two of the stochastic variables, \(X_i\) and \(X_j\), is defined as follows \begin{equation*} \begin{split} \begin{equation} \mathrm{Cov}(X_i,\,X_j) = \langle (x_i-\langle x_i\rangle)(x_j-\langle x_j\rangle)\rangle \label{_auto2} \tag{4} \end{equation} \end{split} \end{equation*} \begin{equation*} \begin{split} \begin{equation} =\int\cdots\int (x_i-\langle x_i\rangle)(x_j-\langle x_j\rangle)P(x_1,\dots,x_n)\,dx_1\dots dx_n, \label{eq:def_covariance} \tag{5} \end{equation} \end{split} \end{equation*} with \begin{equation*} \begin{split} \langle x_i\rangle = \int\cdots\int x_i P(x_1,\dots,x_n)\,dx_1\dots dx_n. \end{split} \end{equation*} \subsection{Meet the covariance in matrix disguise} \label{\detokenize{chapter3:meet-the-covariance-in-matrix-disguise}} If we consider the above covariance as a matrix \begin{equation*} \begin{split} C_{ij} =\mathrm{Cov}(X_i,\,X_j), \end{split} \end{equation*} then the diagonal elements are just the familiar variances, \(C_{ii} = \mathrm{Cov}(X_i,\,X_i) = \mathrm{Var}(X_i)\). It turns out that all the off\sphinxhyphen{}diagonal elements are zero if the stochastic variables are uncorrelated. \subsection{Covariance} \label{\detokenize{chapter3:covariance}} \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{c+c1}{\PYGZsh{} Importing various packages} \PYG{k+kn}{from} \PYG{n+nn}{math} \PYG{k+kn}{import} \PYG{n}{exp}\PYG{p}{,} \PYG{n}{sqrt} \PYG{k+kn}{from} \PYG{n+nn}{random} \PYG{k+kn}{import} \PYG{n}{random}\PYG{p}{,} \PYG{n}{seed} \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt} \PYG{k}{def} \PYG{n+nf}{covariance}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{n}\PYG{p}{)}\PYG{p}{:} \PYG{n+nb}{sum} \PYG{o}{=} \PYG{l+m+mf}{0.0} \PYG{n}{mean\PYGZus{}x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{n}{mean\PYGZus{}y} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{n}{n}\PYG{p}{)}\PYG{p}{:} \PYG{n+nb}{sum} \PYG{o}{+}\PYG{o}{=} \PYG{p}{(}\PYG{n}{x}\PYG{p}{[}\PYG{p}{(}\PYG{n}{i}\PYG{p}{)}\PYG{p}{]}\PYG{o}{\PYGZhy{}}\PYG{n}{mean\PYGZus{}x}\PYG{p}{)}\PYG{o}{*}\PYG{p}{(}\PYG{n}{y}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{o}{\PYGZhy{}}\PYG{n}{mean\PYGZus{}y}\PYG{p}{)} \PYG{k}{return} \PYG{n+nb}{sum}\PYG{o}{/}\PYG{n}{n} \PYG{n}{n} \PYG{o}{=} \PYG{l+m+mi}{10} \PYG{n}{x}\PYG{o}{=}\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{normal}\PYG{p}{(}\PYG{n}{size}\PYG{o}{=}\PYG{n}{n}\PYG{p}{)} \PYG{n}{y} \PYG{o}{=} \PYG{l+m+mi}{4}\PYG{o}{+}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x}\PYG{o}{+}\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{normal}\PYG{p}{(}\PYG{n}{size}\PYG{o}{=}\PYG{n}{n}\PYG{p}{)} \PYG{n}{covxy} \PYG{o}{=} \PYG{n}{covariance}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,}\PYG{n}{y}\PYG{p}{,}\PYG{n}{n}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{covxy}\PYG{p}{)} \PYG{n}{z} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{vstack}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)}\PYG{p}{)} \PYG{n}{c} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{cov}\PYG{p}{(}\PYG{n}{z}\PYG{o}{.}\PYG{n}{T}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{c}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] 1.3311287509199639 [[14.31659816 13.99268172 19.21873233 14.42933266 18.41189432 16.6094975 11.00141571 4.4034975 14.34205163 7.63509233] [13.99268172 13.67609397 18.78390394 14.10286557 17.99532083 16.23370367 10.75250606 4.30386732 14.0175593 7.46234655] [19.21873233 18.78390394 25.79940208 19.37006815 24.71629535 22.29674138 14.76840108 5.91129532 19.2529013 10.24941779] [14.42933266 14.10286557 19.37006815 14.54295487 18.55687678 16.74028719 11.08804516 4.43817236 14.45498656 7.69521404] [18.41189432 17.99532083 24.71629535 18.55687678 23.67865945 21.36068284 14.14839622 5.66312819 18.44462881 9.81912823] [16.6094975 16.23370367 22.29674138 16.74028719 21.36068284 19.26962007 12.76336631 5.1087472 16.63902752 8.85790364] [11.00141571 10.75250606 14.76840108 11.08804516 14.14839622 12.76336631 8.45390408 3.38381409 11.02097512 5.86709383] [ 4.4034975 4.30386732 5.91129532 4.43817236 5.66312819 5.1087472 3.38381409 1.35442722 4.41132648 2.34840076] [14.34205163 14.0175593 19.2529013 14.45498656 18.44462881 16.63902752 11.02097512 4.41132648 14.36755036 7.64866676] [ 7.63509233 7.46234655 10.24941779 7.69521404 9.81912823 8.85790364 5.86709383 2.34840076 7.64866676 4.07182169]] \end{sphinxVerbatim} \subsection{Meet the covariance, uncorrelated events} \label{\detokenize{chapter3:meet-the-covariance-uncorrelated-events}} Consider the stochastic variables \(X_i\) and \(X_j\), (\(i\neq j\)). We have \begin{equation*} \begin{split} \begin{align*} Cov(X_i,\,X_j) &= \langle (x_i-\langle x_i\rangle)(x_j-\langle x_j\rangle)\rangle\\ &=\langle x_i x_j - x_i\langle x_j\rangle - \langle x_i\rangle x_j + \langle x_i\rangle\langle x_j\rangle\rangle\\ &=\langle x_i x_j\rangle - \langle x_i\langle x_j\rangle\rangle - \langle \langle x_i\rangle x_j \rangle + \langle \langle x_i\rangle\langle x_j\rangle\rangle \\ &=\langle x_i x_j\rangle - \langle x_i\rangle\langle x_j\rangle - \langle x_i\rangle\langle x_j\rangle + \langle x_i\rangle\langle x_j\rangle \\ &=\langle x_i x_j\rangle - \langle x_i\rangle\langle x_j\rangle \end{align*} \end{split} \end{equation*} If \(X_i\) and \(X_j\) are independent (assuming \(i \neq j\)), we have that \begin{equation*} \begin{split} \langle x_i x_j\rangle = \langle x_i\rangle\langle x_j\rangle, \end{split} \end{equation*} leading to \begin{equation*} \begin{split} Cov(X_i, X_j) = 0 \hspace{0.1cm} (i\neq j). \end{split} \end{equation*} \subsection{Numerical experiments and the covariance} \label{\detokenize{chapter3:numerical-experiments-and-the-covariance}} Now that we have constructed an idealized mathematical framework, let us try to apply it to empirical observations. Examples of relevant physical phenomena may be spontaneous decays of nuclei, or a purely mathematical set of numbers produced by some deterministic mechanism. It is the latter we will deal with, using so\sphinxhyphen{}called pseudo\sphinxhyphen{}random number generators. In general our observations will contain only a limited set of observables. We remind the reader that a \sphinxstyleemphasis{stochastic process} is a process that produces sequentially a chain of values \begin{equation*} \begin{split} \{x_1, x_2,\dots\,x_k,\dots\}. \end{split} \end{equation*} \subsection{Numerical experiments and the covariance} \label{\detokenize{chapter3:id4}} We will call these values our \sphinxstyleemphasis{measurements} and the entire set as our measured \sphinxstyleemphasis{sample}. The action of measuring all the elements of a sample we will call a stochastic \sphinxstyleemphasis{experiment} (since, operationally, they are often associated with results of empirical observation of some physical or mathematical phenomena; precisely an experiment). We assume that these values are distributed according to some PDF \(p_X^{\phantom X}(x)\), where \(X\) is just the formal symbol for the stochastic variable whose PDF is \(p_X^{\phantom X}(x)\). Instead of trying to determine the full distribution \(p\) we are often only interested in finding the few lowest moments, like the mean \(\mu_X^{\phantom X}\) and the variance \(\sigma_X^{\phantom X}\). \subsection{Numerical experiments and the covariance, actual situations} \label{\detokenize{chapter3:numerical-experiments-and-the-covariance-actual-situations}} In practical situations however, a sample is always of finite size. Let that size be \(n\). The expectation value of a sample \(\alpha\), the \sphinxstylestrong{sample mean}, is then defined as follows \begin{equation*} \begin{split} \langle x_{\alpha} \rangle \equiv \frac{1}{n}\sum_{k=1}^n x_{\alpha,k}. \end{split} \end{equation*} The \sphinxstyleemphasis{sample variance} is: \begin{equation*} \begin{split} \mathrm{Var}(x) \equiv \frac{1}{n}\sum_{k=1}^n (x_{\alpha,k} - \langle x_{\alpha} \rangle)^2, \end{split} \end{equation*} with its square root being the \sphinxstyleemphasis{standard deviation of the sample}. \subsection{Numerical experiments and the covariance, our observables} \label{\detokenize{chapter3:numerical-experiments-and-the-covariance-our-observables}} You can think of the above observables as a set of quantities which define a given experiment. This experiment is then repeated several times, say \(m\) times. The total average is then \begin{equation*} \begin{split} \begin{equation} \langle X_m \rangle= \frac{1}{m}\sum_{\alpha=1}^mx_{\alpha}=\frac{1}{mn}\sum_{\alpha, k} x_{\alpha,k}, \label{eq:exptmean} \tag{6} \end{equation} \end{split} \end{equation*} where the last sums end at \(m\) and \(n\). The total variance is \begin{equation*} \begin{split} \sigma^2_m= \frac{1}{mn^2}\sum_{\alpha=1}^m(\langle x_{\alpha} \rangle-\langle X_m \rangle)^2, \end{split} \end{equation*} which we rewrite as \begin{equation*} \begin{split} \begin{equation} \sigma^2_m=\frac{1}{m}\sum_{\alpha=1}^m\sum_{kl=1}^n (x_{\alpha,k}-\langle X_m \rangle)(x_{\alpha,l}-\langle X_m \rangle). \label{eq:exptvariance} \tag{7} \end{equation} \end{split} \end{equation*} \subsection{Numerical experiments and the covariance, the sample variance} \label{\detokenize{chapter3:numerical-experiments-and-the-covariance-the-sample-variance}} We define also the sample variance \(\sigma^2\) of all \(mn\) individual experiments as \begin{equation*} \begin{split} \begin{equation} \sigma^2=\frac{1}{mn}\sum_{\alpha=1}^m\sum_{k=1}^n (x_{\alpha,k}-\langle X_m \rangle)^2. \label{eq:sampleexptvariance} \tag{8} \end{equation} \end{split} \end{equation*} These quantities, being known experimental values or the results from our calculations, may differ, in some cases significantly, from the similarly named exact values for the mean value \(\mu_X\), the variance \(\mathrm{Var}(X)\) and the covariance \(\mathrm{Cov}(X,Y)\). \subsection{Numerical experiments and the covariance, central limit theorem} \label{\detokenize{chapter3:numerical-experiments-and-the-covariance-central-limit-theorem}} The central limit theorem states that the PDF \(\tilde{p}(z)\) of the average of \(m\) random values corresponding to a PDF \(p(x)\) is a normal distribution whose mean is the mean value of the PDF \(p(x)\) and whose variance is the variance of the PDF \(p(x)\) divided by \(m\), the number of values used to compute \(z\). The central limit theorem leads then to the well\sphinxhyphen{}known expression for the standard deviation, given by \begin{equation*} \begin{split} \sigma_m= \frac{\sigma}{\sqrt{m}}. \end{split} \end{equation*} In many cases the above estimate for the standard deviation, in particular if correlations are strong, may be too simplistic. We need therefore a more precise defintion of the error and the variance in our results. \subsection{Definition of Correlation Functions and Standard Deviation} \label{\detokenize{chapter3:definition-of-correlation-functions-and-standard-deviation}} Our estimate of the true average \(\mu_{X}\) is the sample mean \(\langle X_m \rangle\) \begin{equation*} \begin{split} \mu_{X}^{\phantom X} \approx X_m=\frac{1}{mn}\sum_{\alpha=1}^m\sum_{k=1}^n x_{\alpha,k}. \end{split} \end{equation*} We can then use Eq. ({\hyperref[\detokenize{chapter3:eq:exptvariance}]{\emph{7}}}) \begin{equation*} \begin{split} \sigma^2_m=\frac{1}{mn^2}\sum_{\alpha=1}^m\sum_{kl=1}^n (x_{\alpha,k}-\langle X_m \rangle)(x_{\alpha,l}-\langle X_m \rangle), \end{split} \end{equation*} and rewrite it as \begin{equation*} \begin{split} \sigma^2_m=\frac{\sigma^2}{n}+\frac{2}{mn^2}\sum_{\alpha=1}^m\sum_{k p\). In our regression examples for the nuclear masses and the equation of state this is indeed the case, while for the Ising model we have \(p > n\). These are often cases that lead to near singular or singular matrices. The columns of \(\boldsymbol{U}\) are called the left singular vectors while the columns of \(\boldsymbol{V}\) are the right singular vectors. \subsection{Economy\sphinxhyphen{}size SVD} \label{\detokenize{chapter4:economy-size-svd}} If we assume that \(n > p\), then our matrix \(\boldsymbol{U}\) has dimension \(n \times n\). The last \(n-p\) columns of \(\boldsymbol{U}\) become however irrelevant in our calculations since they are multiplied with the zeros in \(\boldsymbol{\Sigma}\). The economy\sphinxhyphen{}size decomposition removes extra rows or columns of zeros from the diagonal matrix of singular values, \(\boldsymbol{\Sigma}\), along with the columns in either \(\boldsymbol{U}\) or \(\boldsymbol{V}\) that multiply those zeros in the expression. Removing these zeros and columns can improve execution time and reduce storage requirements without compromising the accuracy of the decomposition. If \(n > p\), we keep only the first \(p\) columns of \(\boldsymbol{U}\) and \(\boldsymbol{\Sigma}\) has dimension \(p\times p\). If \(p > n\), then only the first \(n\) columns of \(\boldsymbol{V}\) are computed and \(\boldsymbol{\Sigma}\) has dimension \(n\times n\). The \(n=p\) case is obvious, we retain the full SVD. In general the economy\sphinxhyphen{}size SVD leads to less FLOPS and still conserving the desired accuracy. \subsection{Codes for the SVD} \label{\detokenize{chapter4:codes-for-the-svd}} \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{c+c1}{\PYGZsh{} SVD inversion} \PYG{k}{def} \PYG{n+nf}{SVDinv}\PYG{p}{(}\PYG{n}{A}\PYG{p}{)}\PYG{p}{:} \PYG{l+s+sd}{\PYGZsq{}\PYGZsq{}\PYGZsq{} Takes as input a numpy matrix A and returns inv(A) based on singular value decomposition (SVD).} \PYG{l+s+sd}{ SVD is numerically more stable than the inversion algorithms provided by} \PYG{l+s+sd}{ numpy and scipy.linalg at the cost of being slower.} \PYG{l+s+sd}{ \PYGZsq{}\PYGZsq{}\PYGZsq{}} \PYG{n}{U}\PYG{p}{,} \PYG{n}{s}\PYG{p}{,} \PYG{n}{VT} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{linalg}\PYG{o}{.}\PYG{n}{svd}\PYG{p}{(}\PYG{n}{A}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} print(\PYGZsq{}test U\PYGZsq{})} \PYG{c+c1}{\PYGZsh{} print( (np.transpose(U) @ U \PYGZhy{} U @np.transpose(U)))} \PYG{c+c1}{\PYGZsh{} print(\PYGZsq{}test VT\PYGZsq{})} \PYG{c+c1}{\PYGZsh{} print( (np.transpose(VT) @ VT \PYGZhy{} VT @np.transpose(VT)))} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{U}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{s}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{VT}\PYG{p}{)} \PYG{n}{D} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{U}\PYG{p}{)}\PYG{p}{,}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{VT}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{VT}\PYG{p}{)}\PYG{p}{)}\PYG{p}{:} \PYG{n}{D}\PYG{p}{[}\PYG{n}{i}\PYG{p}{,}\PYG{n}{i}\PYG{p}{]}\PYG{o}{=}\PYG{n}{s}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} \PYG{n}{UT} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{transpose}\PYG{p}{(}\PYG{n}{U}\PYG{p}{)}\PYG{p}{;} \PYG{n}{V} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{transpose}\PYG{p}{(}\PYG{n}{VT}\PYG{p}{)}\PYG{p}{;} \PYG{n}{invD} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{linalg}\PYG{o}{.}\PYG{n}{inv}\PYG{p}{(}\PYG{n}{D}\PYG{p}{)} \PYG{k}{return} \PYG{n}{np}\PYG{o}{.}\PYG{n}{matmul}\PYG{p}{(}\PYG{n}{V}\PYG{p}{,}\PYG{n}{np}\PYG{o}{.}\PYG{n}{matmul}\PYG{p}{(}\PYG{n}{invD}\PYG{p}{,}\PYG{n}{UT}\PYG{p}{)}\PYG{p}{)} \PYG{n}{X} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{array}\PYG{p}{(}\PYG{p}{[} \PYG{p}{[}\PYG{l+m+mf}{1.0}\PYG{p}{,} \PYG{o}{\PYGZhy{}}\PYG{l+m+mf}{1.0}\PYG{p}{,} \PYG{l+m+mf}{2.0}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mf}{1.0}\PYG{p}{,} \PYG{l+m+mf}{0.0}\PYG{p}{,} \PYG{l+m+mf}{1.0}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mf}{1.0}\PYG{p}{,} \PYG{l+m+mf}{2.0}\PYG{p}{,} \PYG{o}{\PYGZhy{}}\PYG{l+m+mf}{1.0}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mf}{1.0}\PYG{p}{,} \PYG{l+m+mf}{1.0}\PYG{p}{,} \PYG{l+m+mf}{0.0}\PYG{p}{]} \PYG{p}{]}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{X}\PYG{p}{)} \PYG{n}{A} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{transpose}\PYG{p}{(}\PYG{n}{X}\PYG{p}{)} \PYG{o}{@} \PYG{n}{X} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{A}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Brute force inversion of super\PYGZhy{}collinear matrix} \PYG{c+c1}{\PYGZsh{}B = np.linalg.inv(A)} \PYG{c+c1}{\PYGZsh{}print(B)} \PYG{n}{C} \PYG{o}{=} \PYG{n}{SVDinv}\PYG{p}{(}\PYG{n}{A}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{C}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] [[ 1. \PYGZhy{}1. 2.] [ 1. 0. 1.] [ 1. 2. \PYGZhy{}1.] [ 1. 1. 0.]] [[ 4. 2. 2.] [ 2. 6. \PYGZhy{}4.] [ 2. \PYGZhy{}4. 6.]] [[\PYGZhy{}9.57425734e\PYGZhy{}17 8.16496581e\PYGZhy{}01 \PYGZhy{}5.77350269e\PYGZhy{}01] [\PYGZhy{}7.07106781e\PYGZhy{}01 4.08248290e\PYGZhy{}01 5.77350269e\PYGZhy{}01] [ 7.07106781e\PYGZhy{}01 4.08248290e\PYGZhy{}01 5.77350269e\PYGZhy{}01]] [1.00000000e+01 6.00000000e+00 9.10898112e\PYGZhy{}32] [[ 3.33066907e\PYGZhy{}17 \PYGZhy{}7.07106781e\PYGZhy{}01 7.07106781e\PYGZhy{}01] [ 8.16496581e\PYGZhy{}01 4.08248290e\PYGZhy{}01 4.08248290e\PYGZhy{}01] [ 5.77350269e\PYGZhy{}01 \PYGZhy{}5.77350269e\PYGZhy{}01 \PYGZhy{}5.77350269e\PYGZhy{}01]] [[\PYGZhy{}3.65939208e+30 3.65939208e+30 3.65939208e+30] [ 3.65939208e+30 \PYGZhy{}3.65939208e+30 \PYGZhy{}3.65939208e+30] [ 3.65939208e+30 \PYGZhy{}3.65939208e+30 \PYGZhy{}3.65939208e+30]] \end{sphinxVerbatim} The matrix \(\boldsymbol{X}\) has columns that are linearly dependent. The first column is the row\sphinxhyphen{}wise sum of the other two columns. The rank of a matrix (the column rank) is the dimension of space spanned by the column vectors. The rank of the matrix is the number of linearly independent columns, in this case just \(2\). We see this from the singular values when running the above code. Running the standard inversion algorithm for matrix inversion with \(\boldsymbol{X}^T\boldsymbol{X}\) results in the program terminating due to a singular matrix. \subsection{Mathematical Properties} \label{\detokenize{chapter4:mathematical-properties}} There are several interesting mathematical properties which will be relevant when we are going to discuss the differences between say ordinary least squares (OLS) and \sphinxstylestrong{Ridge} regression. We have from OLS that the parameters of the linear approximation are given by \begin{equation*} \begin{split} \boldsymbol{\tilde{y}} = \boldsymbol{X}\boldsymbol{\beta} = \boldsymbol{X}\left(\boldsymbol{X}^T\boldsymbol{X}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}. \end{split} \end{equation*} The matrix to invert can be rewritten in terms of our SVD decomposition as \begin{equation*} \begin{split} \boldsymbol{X}^T\boldsymbol{X} = \boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{U}^T\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T. \end{split} \end{equation*} Using the orthogonality properties of \(\boldsymbol{U}\) we have \begin{equation*} \begin{split} \boldsymbol{X}^T\boldsymbol{X} = \boldsymbol{V}\boldsymbol{\Sigma}^T\boldsymbol{\Sigma}\boldsymbol{V}^T = \boldsymbol{V}\boldsymbol{D}\boldsymbol{V}^T, \end{split} \end{equation*} with \(\boldsymbol{D}\) being a diagonal matrix with values along the diagonal given by the singular values squared. This means that \begin{equation*} \begin{split} (\boldsymbol{X}^T\boldsymbol{X})\boldsymbol{V} = \boldsymbol{V}\boldsymbol{D}, \end{split} \end{equation*} that is the eigenvectors of \((\boldsymbol{X}^T\boldsymbol{X})\) are given by the columns of the right singular matrix of \(\boldsymbol{X}\) and the eigenvalues are the squared singular values. It is easy to show (show this) that \begin{equation*} \begin{split} (\boldsymbol{X}\boldsymbol{X}^T)\boldsymbol{U} = \boldsymbol{U}\boldsymbol{D}, \end{split} \end{equation*} that is, the eigenvectors of \((\boldsymbol{X}\boldsymbol{X})^T\) are the columns of the left singular matrix and the eigenvalues are the same. Going back to our OLS equation we have \begin{equation*} \begin{split} \boldsymbol{X}\boldsymbol{\beta} = \boldsymbol{X}\left(\boldsymbol{V}\boldsymbol{D}\boldsymbol{V}^T \right)^{-1}\boldsymbol{X}^T\boldsymbol{y}=\boldsymbol{U\Sigma V^T}\left(\boldsymbol{V}\boldsymbol{D}\boldsymbol{V}^T \right)^{-1}(\boldsymbol{U\Sigma V^T})^T\boldsymbol{y}=\boldsymbol{U}\boldsymbol{U}^T\boldsymbol{y}. \end{split} \end{equation*} We will come back to this expression when we discuss Ridge regression. \subsection{Ridge and LASSO Regression} \label{\detokenize{chapter4:ridge-and-lasso-regression}} Let us remind ourselves about the expression for the standard Mean Squared Error (MSE) which we used to define our cost function and the equations for the ordinary least squares (OLS) method, that is our optimization problem is \begin{equation*} \begin{split} {\displaystyle \min_{\boldsymbol{\beta}\in {\mathbb{R}}^{p}}}\frac{1}{n}\left\{\left(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right)^T\left(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\right)\right\}. \end{split} \end{equation*} or we can state it as \begin{equation*} \begin{split} {\displaystyle \min_{\boldsymbol{\beta}\in {\mathbb{R}}^{p}}}\frac{1}{n}\sum_{i=0}^{n-1}\left(y_i-\tilde{y}_i\right)^2=\frac{1}{n}\vert\vert \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\vert\vert_2^2, \end{split} \end{equation*} where we have used the definition of a norm\sphinxhyphen{}2 vector, that is \begin{equation*} \begin{split} \vert\vert \boldsymbol{x}\vert\vert_2 = \sqrt{\sum_i x_i^2}. \end{split} \end{equation*} By minimizing the above equation with respect to the parameters \(\boldsymbol{\beta}\) we could then obtain an analytical expression for the parameters \(\boldsymbol{\beta}\). We can add a regularization parameter \(\lambda\) by defining a new cost function to be optimized, that is \begin{equation*} \begin{split} {\displaystyle \min_{\boldsymbol{\beta}\in {\mathbb{R}}^{p}}}\frac{1}{n}\vert\vert \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\vert\vert_2^2+\lambda\vert\vert \boldsymbol{\beta}\vert\vert_2^2 \end{split} \end{equation*} which leads to the Ridge regression minimization problem where we require that \(\vert\vert \boldsymbol{\beta}\vert\vert_2^2\le t\), where \(t\) is a finite number larger than zero. By defining \begin{equation*} \begin{split} C(\boldsymbol{X},\boldsymbol{\beta})=\frac{1}{n}\vert\vert \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\vert\vert_2^2+\lambda\vert\vert \boldsymbol{\beta}\vert\vert_1, \end{split} \end{equation*} we have a new optimization equation \begin{equation*} \begin{split} {\displaystyle \min_{\boldsymbol{\beta}\in {\mathbb{R}}^{p}}}\frac{1}{n}\vert\vert \boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta}\vert\vert_2^2+\lambda\vert\vert \boldsymbol{\beta}\vert\vert_1 \end{split} \end{equation*} which leads to Lasso regression. Lasso stands for least absolute shrinkage and selection operator. Here we have defined the norm\sphinxhyphen{}1 as \begin{equation*} \begin{split} \vert\vert \boldsymbol{x}\vert\vert_1 = \sum_i \vert x_i\vert. \end{split} \end{equation*} \subsection{More on Ridge Regression} \label{\detokenize{chapter4:more-on-ridge-regression}} Using the matrix\sphinxhyphen{}vector expression for Ridge regression, \begin{equation*} \begin{split} C(\boldsymbol{X},\boldsymbol{\beta})=\frac{1}{n}\left\{(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})^T(\boldsymbol{y}-\boldsymbol{X}\boldsymbol{\beta})\right\}+\lambda\boldsymbol{\beta}^T\boldsymbol{\beta}, \end{split} \end{equation*} by taking the derivatives with respect to \(\boldsymbol{\beta}\) we obtain then a slightly modified matrix inversion problem which for finite values of \(\lambda\) does not suffer from singularity problems. We obtain \begin{equation*} \begin{split} \boldsymbol{\beta}^{\mathrm{Ridge}} = \left(\boldsymbol{X}^T\boldsymbol{X}+\lambda\boldsymbol{I}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}, \end{split} \end{equation*} with \(\boldsymbol{I}\) being a \(p\times p\) identity matrix with the constraint that \begin{equation*} \begin{split} \sum_{i=0}^{p-1} \beta_i^2 \leq t, \end{split} \end{equation*} with \(t\) a finite positive number. We see that Ridge regression is nothing but the standard OLS with a modified diagonal term added to \(\boldsymbol{X}^T\boldsymbol{X}\). The consequences, in particular for our discussion of the bias\sphinxhyphen{}variance tradeoff are rather interesting. Furthermore, if we use the result above in terms of the SVD decomposition (our analysis was done for the OLS method), we had \begin{equation*} \begin{split} (\boldsymbol{X}\boldsymbol{X}^T)\boldsymbol{U} = \boldsymbol{U}\boldsymbol{D}. \end{split} \end{equation*} We can analyse the OLS solutions in terms of the eigenvectors (the columns) of the right singular value matrix \(\boldsymbol{U}\) as \begin{equation*} \begin{split} \boldsymbol{X}\boldsymbol{\beta} = \boldsymbol{X}\left(\boldsymbol{V}\boldsymbol{D}\boldsymbol{V}^T \right)^{-1}\boldsymbol{X}^T\boldsymbol{y}=\boldsymbol{U\Sigma V^T}\left(\boldsymbol{V}\boldsymbol{D}\boldsymbol{V}^T \right)^{-1}(\boldsymbol{U\Sigma V^T})^T\boldsymbol{y}=\boldsymbol{U}\boldsymbol{U}^T\boldsymbol{y} \end{split} \end{equation*} For Ridge regression this becomes \begin{equation*} \begin{split} \boldsymbol{X}\boldsymbol{\beta}^{\mathrm{Ridge}} = \boldsymbol{U\Sigma V^T}\left(\boldsymbol{V}\boldsymbol{D}\boldsymbol{V}^T+\lambda\boldsymbol{I} \right)^{-1}(\boldsymbol{U\Sigma V^T})^T\boldsymbol{y}=\sum_{j=0}^{p-1}\boldsymbol{u}_j\boldsymbol{u}_j^T\frac{\sigma_j^2}{\sigma_j^2+\lambda}\boldsymbol{y}, \end{split} \end{equation*} with the vectors \(\boldsymbol{u}_j\) being the columns of \(\boldsymbol{U}\). \subsection{Interpreting the Ridge results} \label{\detokenize{chapter4:interpreting-the-ridge-results}} Since \(\lambda \geq 0\), it means that compared to OLS, we have \begin{equation*} \begin{split} \frac{\sigma_j^2}{\sigma_j^2+\lambda} \leq 1. \end{split} \end{equation*} Ridge regression finds the coordinates of \(\boldsymbol{y}\) with respect to the orthonormal basis \(\boldsymbol{U}\), it then shrinks the coordinates by \(\frac{\sigma_j^2}{\sigma_j^2+\lambda}\). Recall that the SVD has eigenvalues ordered in a descending way, that is \(\sigma_i \geq \sigma_{i+1}\). For small eigenvalues \(\sigma_i\) it means that their contributions become less important, a fact which can be used to reduce the number of degrees of freedom. Actually, calculating the variance of \(\boldsymbol{X}\boldsymbol{v}_j\) shows that this quantity is equal to \(\sigma_j^2/n\). With a parameter \(\lambda\) we can thus shrink the role of specific parameters. \subsection{More interpretations} \label{\detokenize{chapter4:more-interpretations}} For the sake of simplicity, let us assume that the design matrix is orthonormal, that is \begin{equation*} \begin{split} \boldsymbol{X}^T\boldsymbol{X}=(\boldsymbol{X}^T\boldsymbol{X})^{-1} =\boldsymbol{I}. \end{split} \end{equation*} In this case the standard OLS results in \begin{equation*} \begin{split} \boldsymbol{\beta}^{\mathrm{OLS}} = \boldsymbol{X}^T\boldsymbol{y}=\sum_{i=0}^{p-1}\boldsymbol{u}_j\boldsymbol{u}_j^T\boldsymbol{y}, \end{split} \end{equation*} and \begin{equation*} \begin{split} \boldsymbol{\beta}^{\mathrm{Ridge}} = \left(\boldsymbol{I}+\lambda\boldsymbol{I}\right)^{-1}\boldsymbol{X}^T\boldsymbol{y}=\left(1+\lambda\right)^{-1}\boldsymbol{\beta}^{\mathrm{OLS}}, \end{split} \end{equation*} that is the Ridge estimator scales the OLS estimator by the inverse of a factor \(1+\lambda\), and the Ridge estimator converges to zero when the hyperparameter goes to infinity. We will come back to more interpreations after we have gone through some of the statistical analysis part. For more discussions of Ridge and Lasso regression, \sphinxhref{https://arxiv.org/abs/1509.09169}{Wessel van Wieringen’s} article is highly recommended. Similarly, \sphinxhref{https://arxiv.org/abs/1803.08823}{Mehta et al’s article} is also recommended. \subsection{A better understanding of regularization} \label{\detokenize{chapter4:a-better-understanding-of-regularization}} The parameter \(\lambda\) that we have introduced in the Ridge (and Lasso as well) regression is often called a regularization parameter or shrinkage parameter. It is common to call it a hyperparameter. What does it mean mathemtically? Here we will first look at how to analyze the difference between the standard OLS equations and the Ridge expressions in terms of a linear algebra analysis using the SVD algorithm. Thereafter, we will link (see the material on the bias\sphinxhyphen{}variance tradeoff below) these observation to the statisical analysis of the results. In particular we consider how the variance of the parameters \(\boldsymbol{\beta}\) is affected by changing the parameter \(\lambda\). \subsection{Decomposing the OLS and Ridge expressions} \label{\detokenize{chapter4:decomposing-the-ols-and-ridge-expressions}} We have our design matrix \(\boldsymbol{X}\in {\mathbb{R}}^{n\times p}\). With the SVD we decompose it as \begin{equation*} \begin{split} \boldsymbol{X} = \boldsymbol{U\Sigma V^T}, \end{split} \end{equation*} with \(\boldsymbol{U}\in {\mathbb{R}}^{n\times n}\), \(\boldsymbol{\Sigma}\in {\mathbb{R}}^{n\times p}\) and \(\boldsymbol{V}\in {\mathbb{R}}^{p\times p}\). The matrices \(\boldsymbol{U}\) and \(\boldsymbol{V}\) are unitary/orthonormal matrices, that is in case the matrices are real we have \(\boldsymbol{U}^T\boldsymbol{U}=\boldsymbol{U}\boldsymbol{U}^T=\boldsymbol{I}\) and \(\boldsymbol{V}^T\boldsymbol{V}=\boldsymbol{V}\boldsymbol{V}^T=\boldsymbol{I}\). \subsection{Introducing the Covariance and Correlation functions} \label{\detokenize{chapter4:introducing-the-covariance-and-correlation-functions}} Before we discuss the link between for example Ridge regression and the singular value decomposition, we need to remind ourselves about the definition of the covariance and the correlation function. These are quantities Suppose we have defined two vectors \(\hat{x}\) and \(\hat{y}\) with \(n\) elements each. The covariance matrix \(\boldsymbol{C}\) is defined as \begin{equation*} \begin{split} \boldsymbol{C}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} \mathrm{cov}[\boldsymbol{x},\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] \\ \mathrm{cov}[\boldsymbol{y},\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{y},\boldsymbol{y}] \\ \end{bmatrix}, \end{split} \end{equation*} where for example \begin{equation*} \begin{split} \mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] =\frac{1}{n} \sum_{i=0}^{n-1}(x_i- \overline{x})(y_i- \overline{y}). \end{split} \end{equation*} With this definition and recalling that the variance is defined as \begin{equation*} \begin{split} \mathrm{var}[\boldsymbol{x}]=\frac{1}{n} \sum_{i=0}^{n-1}(x_i- \overline{x})^2, \end{split} \end{equation*} we can rewrite the covariance matrix as \begin{equation*} \begin{split} \boldsymbol{C}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} \mathrm{var}[\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] \\ \mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] & \mathrm{var}[\boldsymbol{y}] \\ \end{bmatrix}. \end{split} \end{equation*} The covariance takes values between zero and infinity and may thus lead to problems with loss of numerical precision for particularly large values. It is common to scale the covariance matrix by introducing instead the correlation matrix defined via the so\sphinxhyphen{}called correlation function \begin{equation*} \begin{split} \mathrm{corr}[\boldsymbol{x},\boldsymbol{y}]=\frac{\mathrm{cov}[\boldsymbol{x},\boldsymbol{y}]}{\sqrt{\mathrm{var}[\boldsymbol{x}] \mathrm{var}[\boldsymbol{y}]}}. \end{split} \end{equation*} The correlation function is then given by values \(\mathrm{corr}[\boldsymbol{x},\boldsymbol{y}] \in [-1,1]\). This avoids eventual problems with too large values. We can then define the correlation matrix for the two vectors \(\boldsymbol{x}\) and \(\boldsymbol{y}\) as \begin{equation*} \begin{split} \boldsymbol{K}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} 1 & \mathrm{corr}[\boldsymbol{x},\boldsymbol{y}] \\ \mathrm{corr}[\boldsymbol{y},\boldsymbol{x}] & 1 \\ \end{bmatrix}, \end{split} \end{equation*} In the above example this is the function we constructed using \sphinxstylestrong{pandas}. \subsection{Correlation Function and Design/Feature Matrix} \label{\detokenize{chapter4:correlation-function-and-design-feature-matrix}} In our derivation of the various regression algorithms like \sphinxstylestrong{Ordinary Least Squares} or \sphinxstylestrong{Ridge regression} we defined the design/feature matrix \(\boldsymbol{X}\) as \begin{equation*} \begin{split} \boldsymbol{X}=\begin{bmatrix} x_{0,0} & x_{0,1} & x_{0,2}& \dots & \dots x_{0,p-1}\\ x_{1,0} & x_{1,1} & x_{1,2}& \dots & \dots x_{1,p-1}\\ x_{2,0} & x_{2,1} & x_{2,2}& \dots & \dots x_{2,p-1}\\ \dots & \dots & \dots & \dots \dots & \dots \\ x_{n-2,0} & x_{n-2,1} & x_{n-2,2}& \dots & \dots x_{n-2,p-1}\\ x_{n-1,0} & x_{n-1,1} & x_{n-1,2}& \dots & \dots x_{n-1,p-1}\\ \end{bmatrix}, \end{split} \end{equation*} with \(\boldsymbol{X}\in {\mathbb{R}}^{n\times p}\), with the predictors/features \(p\) refering to the column numbers and the entries \(n\) being the row elements. We can rewrite the design/feature matrix in terms of its column vectors as \begin{equation*} \begin{split} \boldsymbol{X}=\begin{bmatrix} \boldsymbol{x}_0 & \boldsymbol{x}_1 & \boldsymbol{x}_2 & \dots & \dots & \boldsymbol{x}_{p-1}\end{bmatrix}, \end{split} \end{equation*} with a given vector \begin{equation*} \begin{split} \boldsymbol{x}_i^T = \begin{bmatrix}x_{0,i} & x_{1,i} & x_{2,i}& \dots & \dots x_{n-1,i}\end{bmatrix}. \end{split} \end{equation*} With these definitions, we can now rewrite our \(2\times 2\) correaltion/covariance matrix in terms of a moe general design/feature matrix \(\boldsymbol{X}\in {\mathbb{R}}^{n\times p}\). This leads to a \(p\times p\) covariance matrix for the vectors \(\boldsymbol{x}_i\) with \(i=0,1,\dots,p-1\) \begin{equation*} \begin{split} \boldsymbol{C}[\boldsymbol{x}] = \begin{bmatrix} \mathrm{var}[\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_1] & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_2] & \dots & \dots & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_{p-1}]\\ \mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_0] & \mathrm{var}[\boldsymbol{x}_1] & \mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_2] & \dots & \dots & \mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_{p-1}]\\ \mathrm{cov}[\boldsymbol{x}_2,\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_2,\boldsymbol{x}_1] & \mathrm{var}[\boldsymbol{x}_2] & \dots & \dots & \mathrm{cov}[\boldsymbol{x}_2,\boldsymbol{x}_{p-1}]\\ \dots & \dots & \dots & \dots & \dots & \dots \\ \dots & \dots & \dots & \dots & \dots & \dots \\ \mathrm{cov}[\boldsymbol{x}_{p-1},\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_{p-1},\boldsymbol{x}_1] & \mathrm{cov}[\boldsymbol{x}_{p-1},\boldsymbol{x}_{2}] & \dots & \dots & \mathrm{var}[\boldsymbol{x}_{p-1}]\\ \end{bmatrix}, \end{split} \end{equation*} and the correlation matrix \begin{equation*} \begin{split} \boldsymbol{K}[\boldsymbol{x}] = \begin{bmatrix} 1 & \mathrm{corr}[\boldsymbol{x}_0,\boldsymbol{x}_1] & \mathrm{corr}[\boldsymbol{x}_0,\boldsymbol{x}_2] & \dots & \dots & \mathrm{corr}[\boldsymbol{x}_0,\boldsymbol{x}_{p-1}]\\ \mathrm{corr}[\boldsymbol{x}_1,\boldsymbol{x}_0] & 1 & \mathrm{corr}[\boldsymbol{x}_1,\boldsymbol{x}_2] & \dots & \dots & \mathrm{corr}[\boldsymbol{x}_1,\boldsymbol{x}_{p-1}]\\ \mathrm{corr}[\boldsymbol{x}_2,\boldsymbol{x}_0] & \mathrm{corr}[\boldsymbol{x}_2,\boldsymbol{x}_1] & 1 & \dots & \dots & \mathrm{corr}[\boldsymbol{x}_2,\boldsymbol{x}_{p-1}]\\ \dots & \dots & \dots & \dots & \dots & \dots \\ \dots & \dots & \dots & \dots & \dots & \dots \\ \mathrm{corr}[\boldsymbol{x}_{p-1},\boldsymbol{x}_0] & \mathrm{corr}[\boldsymbol{x}_{p-1},\boldsymbol{x}_1] & \mathrm{corr}[\boldsymbol{x}_{p-1},\boldsymbol{x}_{2}] & \dots & \dots & 1\\ \end{bmatrix}, \end{split} \end{equation*} \subsection{Covariance Matrix Examples} \label{\detokenize{chapter4:covariance-matrix-examples}} The Numpy function \sphinxstylestrong{np.cov} calculates the covariance elements using the factor \(1/(n-1)\) instead of \(1/n\) since it assumes we do not have the exact mean values. The following simple function uses the \sphinxstylestrong{np.vstack} function which takes each vector of dimension \(1\times n\) and produces a \(2\times n\) matrix \(\boldsymbol{W}\) \begin{equation*} \begin{split} \boldsymbol{W} = \begin{bmatrix} x_0 & y_0 \\ x_1 & y_1 \\ x_2 & y_2\\ \dots & \dots \\ x_{n-2} & y_{n-2}\\ x_{n-1} & y_{n-1} & \end{bmatrix}, \end{split} \end{equation*} which in turn is converted into into the \(2\times 2\) covariance matrix \(\boldsymbol{C}\) via the Numpy function \sphinxstylestrong{np.cov()}. We note that we can also calculate the mean value of each set of samples \(\boldsymbol{x}\) etc using the Numpy function \sphinxstylestrong{np.mean(x)}. We can also extract the eigenvalues of the covariance matrix through the \sphinxstylestrong{np.linalg.eig()} function. \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{c+c1}{\PYGZsh{} Importing various packages} \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{n}{n} \PYG{o}{=} \PYG{l+m+mi}{100} \PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{normal}\PYG{p}{(}\PYG{n}{size}\PYG{o}{=}\PYG{n}{n}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)} \PYG{n}{y} \PYG{o}{=} \PYG{l+m+mi}{4}\PYG{o}{+}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x}\PYG{o}{+}\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{normal}\PYG{p}{(}\PYG{n}{size}\PYG{o}{=}\PYG{n}{n}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)} \PYG{n}{W} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{vstack}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)}\PYG{p}{)} \PYG{n}{C} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{cov}\PYG{p}{(}\PYG{n}{W}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{C}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] 0.04144248019085487 4.194659964041423 [[ 1.10772753 3.41408669] [ 3.41408669 11.38266616]] \end{sphinxVerbatim} \subsection{Correlation Matrix} \label{\detokenize{chapter4:correlation-matrix}} The previous example can be converted into the correlation matrix by simply scaling the matrix elements with the variances. We should also subtract the mean values for each column. This leads to the following code which sets up the correlations matrix for the previous example in a more brute force way. Here we scale the mean values for each column of the design matrix, calculate the relevant mean values and variances and then finally set up the \(2\times 2\) correlation matrix (since we have only two vectors). \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{n}{n} \PYG{o}{=} \PYG{l+m+mi}{100} \PYG{c+c1}{\PYGZsh{} define two vectors } \PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{random}\PYG{p}{(}\PYG{n}{size}\PYG{o}{=}\PYG{n}{n}\PYG{p}{)} \PYG{n}{y} \PYG{o}{=} \PYG{l+m+mi}{4}\PYG{o}{+}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x}\PYG{o}{+}\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{normal}\PYG{p}{(}\PYG{n}{size}\PYG{o}{=}\PYG{n}{n}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{}scaling the x and y vectors } \PYG{n}{x} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{n}{y} \PYG{o}{=} \PYG{n}{y} \PYG{o}{\PYGZhy{}} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)} \PYG{n}{variance\PYGZus{}x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{sum}\PYG{p}{(}\PYG{n}{x}\PYG{n+nd}{@x}\PYG{p}{)}\PYG{o}{/}\PYG{n}{n} \PYG{n}{variance\PYGZus{}y} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{sum}\PYG{p}{(}\PYG{n}{y}\PYG{n+nd}{@y}\PYG{p}{)}\PYG{o}{/}\PYG{n}{n} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{variance\PYGZus{}x}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{variance\PYGZus{}y}\PYG{p}{)} \PYG{n}{cov\PYGZus{}xy} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{sum}\PYG{p}{(}\PYG{n}{x}\PYG{n+nd}{@y}\PYG{p}{)}\PYG{o}{/}\PYG{n}{n} \PYG{n}{cov\PYGZus{}xx} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{sum}\PYG{p}{(}\PYG{n}{x}\PYG{n+nd}{@x}\PYG{p}{)}\PYG{o}{/}\PYG{n}{n} \PYG{n}{cov\PYGZus{}yy} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{sum}\PYG{p}{(}\PYG{n}{y}\PYG{n+nd}{@y}\PYG{p}{)}\PYG{o}{/}\PYG{n}{n} \PYG{n}{C} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)} \PYG{n}{C}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{o}{=} \PYG{n}{cov\PYGZus{}xx}\PYG{o}{/}\PYG{n}{variance\PYGZus{}x} \PYG{n}{C}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{]}\PYG{o}{=} \PYG{n}{cov\PYGZus{}yy}\PYG{o}{/}\PYG{n}{variance\PYGZus{}y} \PYG{n}{C}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{]}\PYG{o}{=} \PYG{n}{cov\PYGZus{}xy}\PYG{o}{/}\PYG{n}{np}\PYG{o}{.}\PYG{n}{sqrt}\PYG{p}{(}\PYG{n}{variance\PYGZus{}y}\PYG{o}{*}\PYG{n}{variance\PYGZus{}x}\PYG{p}{)} \PYG{n}{C}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{o}{=} \PYG{n}{C}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{]} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{C}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] 0.09052813127366242 1.5055457430392476 [[1. 0.56806976] [0.56806976 1. ]] \end{sphinxVerbatim} We see that the matrix elements along the diagonal are one as they should be and that the matrix is symmetric. Furthermore, diagonalizing this matrix we easily see that it is a positive definite matrix. The above procedure with \sphinxstylestrong{numpy} can be made more compact if we use \sphinxstylestrong{pandas}. \subsection{Correlation Matrix with Pandas} \label{\detokenize{chapter4:correlation-matrix-with-pandas}} We whow here how we can set up the correlation matrix using \sphinxstylestrong{pandas}, as done in this simple code \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{k+kn}{import} \PYG{n+nn}{pandas} \PYG{k}{as} \PYG{n+nn}{pd} \PYG{n}{n} \PYG{o}{=} \PYG{l+m+mi}{10} \PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{normal}\PYG{p}{(}\PYG{n}{size}\PYG{o}{=}\PYG{n}{n}\PYG{p}{)} \PYG{n}{x} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{n}{y} \PYG{o}{=} \PYG{l+m+mi}{4}\PYG{o}{+}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x}\PYG{o}{+}\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{normal}\PYG{p}{(}\PYG{n}{size}\PYG{o}{=}\PYG{n}{n}\PYG{p}{)} \PYG{n}{y} \PYG{o}{=} \PYG{n}{y} \PYG{o}{\PYGZhy{}} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)} \PYG{n}{X} \PYG{o}{=} \PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{vstack}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{T} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{X}\PYG{p}{)} \PYG{n}{Xpd} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{DataFrame}\PYG{p}{(}\PYG{n}{X}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{Xpd}\PYG{p}{)} \PYG{n}{correlation\PYGZus{}matrix} \PYG{o}{=} \PYG{n}{Xpd}\PYG{o}{.}\PYG{n}{corr}\PYG{p}{(}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{correlation\PYGZus{}matrix}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] [[ 0.19619662 \PYGZhy{}0.78402323] [ 0.31757369 1.22304781] [\PYGZhy{}0.69338544 \PYGZhy{}1.67201132] [\PYGZhy{}0.83049344 \PYGZhy{}3.62540546] [ 1.07466493 3.158262 ] [\PYGZhy{}0.63779754 \PYGZhy{}0.28923407] [\PYGZhy{}0.50263516 \PYGZhy{}2.66065903] [ 0.59499124 1.87464536] [\PYGZhy{}0.05490614 0.80385431] [ 0.53579124 1.97152362]] 0 1 0 0.196197 \PYGZhy{}0.784023 1 0.317574 1.223048 2 \PYGZhy{}0.693385 \PYGZhy{}1.672011 3 \PYGZhy{}0.830493 \PYGZhy{}3.625405 4 1.074665 3.158262 5 \PYGZhy{}0.637798 \PYGZhy{}0.289234 6 \PYGZhy{}0.502635 \PYGZhy{}2.660659 7 0.594991 1.874645 8 \PYGZhy{}0.054906 0.803854 9 0.535791 1.971524 0 1 0 1.000000 0.896919 1 0.896919 1.000000 \end{sphinxVerbatim} We expand this model to the Franke function discussed above. \subsection{Correlation Matrix with Pandas and the Franke function} \label{\detokenize{chapter4:correlation-matrix-with-pandas-and-the-franke-function}} \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{c+c1}{\PYGZsh{} Common imports} \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{k+kn}{import} \PYG{n+nn}{pandas} \PYG{k}{as} \PYG{n+nn}{pd} \PYG{k}{def} \PYG{n+nf}{FrankeFunction}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,}\PYG{n}{y}\PYG{p}{)}\PYG{p}{:} \PYG{n}{term1} \PYG{o}{=} \PYG{l+m+mf}{0.75}\PYG{o}{*}\PYG{n}{np}\PYG{o}{.}\PYG{n}{exp}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{p}{(}\PYG{l+m+mf}{0.25}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{9}\PYG{o}{*}\PYG{n}{x}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} \PYG{o}{\PYGZhy{}} \PYG{l+m+mf}{0.25}\PYG{o}{*}\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{9}\PYG{o}{*}\PYG{n}{y}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)} \PYG{n}{term2} \PYG{o}{=} \PYG{l+m+mf}{0.75}\PYG{o}{*}\PYG{n}{np}\PYG{o}{.}\PYG{n}{exp}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{9}\PYG{o}{*}\PYG{n}{x}\PYG{o}{+}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{/}\PYG{l+m+mf}{49.0} \PYG{o}{\PYGZhy{}} \PYG{l+m+mf}{0.1}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{9}\PYG{o}{*}\PYG{n}{y}\PYG{o}{+}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{)} \PYG{n}{term3} \PYG{o}{=} \PYG{l+m+mf}{0.5}\PYG{o}{*}\PYG{n}{np}\PYG{o}{.}\PYG{n}{exp}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{p}{(}\PYG{l+m+mi}{9}\PYG{o}{*}\PYG{n}{x}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{7}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{o}{/}\PYG{l+m+mf}{4.0} \PYG{o}{\PYGZhy{}} \PYG{l+m+mf}{0.25}\PYG{o}{*}\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{9}\PYG{o}{*}\PYG{n}{y}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{3}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)} \PYG{n}{term4} \PYG{o}{=} \PYG{o}{\PYGZhy{}}\PYG{l+m+mf}{0.2}\PYG{o}{*}\PYG{n}{np}\PYG{o}{.}\PYG{n}{exp}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{p}{(}\PYG{l+m+mi}{9}\PYG{o}{*}\PYG{n}{x}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{4}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{\PYGZhy{}} \PYG{p}{(}\PYG{l+m+mi}{9}\PYG{o}{*}\PYG{n}{y}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{7}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} \PYG{k}{return} \PYG{n}{term1} \PYG{o}{+} \PYG{n}{term2} \PYG{o}{+} \PYG{n}{term3} \PYG{o}{+} \PYG{n}{term4} \PYG{k}{def} \PYG{n+nf}{create\PYGZus{}X}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{n} \PYG{p}{)}\PYG{p}{:} \PYG{k}{if} \PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{x}\PYG{o}{.}\PYG{n}{shape}\PYG{p}{)} \PYG{o}{\PYGZgt{}} \PYG{l+m+mi}{1}\PYG{p}{:} \PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{ravel}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{n}{y} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{ravel}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)} \PYG{n}{N} \PYG{o}{=} \PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{n}{l} \PYG{o}{=} \PYG{n+nb}{int}\PYG{p}{(}\PYG{p}{(}\PYG{n}{n}\PYG{o}{+}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{p}{(}\PYG{n}{n}\PYG{o}{+}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{/}\PYG{l+m+mi}{2}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Number of elements in beta} \PYG{n}{X} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{ones}\PYG{p}{(}\PYG{p}{(}\PYG{n}{N}\PYG{p}{,}\PYG{n}{l}\PYG{p}{)}\PYG{p}{)} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{n}{n}\PYG{o}{+}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{:} \PYG{n}{q} \PYG{o}{=} \PYG{n+nb}{int}\PYG{p}{(}\PYG{p}{(}\PYG{n}{i}\PYG{p}{)}\PYG{o}{*}\PYG{p}{(}\PYG{n}{i}\PYG{o}{+}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{/}\PYG{l+m+mi}{2}\PYG{p}{)} \PYG{k}{for} \PYG{n}{k} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{i}\PYG{o}{+}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{:} \PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{n}{q}\PYG{o}{+}\PYG{n}{k}\PYG{p}{]} \PYG{o}{=} \PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{n}{i}\PYG{o}{\PYGZhy{}}\PYG{n}{k}\PYG{p}{)}\PYG{p}{)}\PYG{o}{*}\PYG{p}{(}\PYG{n}{y}\PYG{o}{*}\PYG{o}{*}\PYG{n}{k}\PYG{p}{)} \PYG{k}{return} \PYG{n}{X} \PYG{c+c1}{\PYGZsh{} Making meshgrid of datapoints and compute Franke\PYGZsq{}s function} \PYG{n}{n} \PYG{o}{=} \PYG{l+m+mi}{4} \PYG{n}{N} \PYG{o}{=} \PYG{l+m+mi}{100} \PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{sort}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{uniform}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{N}\PYG{p}{)}\PYG{p}{)} \PYG{n}{y} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{sort}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{uniform}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{N}\PYG{p}{)}\PYG{p}{)} \PYG{n}{z} \PYG{o}{=} \PYG{n}{FrankeFunction}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)} \PYG{n}{X} \PYG{o}{=} \PYG{n}{create\PYGZus{}X}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{n}\PYG{o}{=}\PYG{n}{n}\PYG{p}{)} \PYG{n}{Xpd} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{DataFrame}\PYG{p}{(}\PYG{n}{X}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} subtract the mean values and set up the covariance matrix} \PYG{n}{Xpd} \PYG{o}{=} \PYG{n}{Xpd} \PYG{o}{\PYGZhy{}} \PYG{n}{Xpd}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{p}{)} \PYG{n}{covariance\PYGZus{}matrix} \PYG{o}{=} \PYG{n}{Xpd}\PYG{o}{.}\PYG{n}{cov}\PYG{p}{(}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n}{covariance\PYGZus{}matrix}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] 0 1 2 3 4 5 6 7 \PYGZbs{} 0 0.0 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 1 0.0 0.089183 0.084797 0.089147 0.085535 0.082266 0.080527 0.077480 2 0.0 0.084797 0.082978 0.087978 0.085822 0.083785 0.081609 0.079503 3 0.0 0.089147 0.087978 0.094876 0.092810 0.090814 0.088924 0.086714 4 0.0 0.085535 0.085822 0.092810 0.091736 0.090594 0.088276 0.086777 5 0.0 0.082266 0.083785 0.090814 0.090594 0.090194 0.087493 0.086618 6 0.0 0.080527 0.081609 0.088924 0.088276 0.087493 0.085397 0.084155 7 0.0 0.077480 0.079503 0.086714 0.086777 0.086618 0.084155 0.083459 8 0.0 0.074802 0.077623 0.084723 0.085401 0.085786 0.082993 0.082776 9 0.0 0.072449 0.075952 0.082939 0.084154 0.085015 0.081923 0.082128 10 0.0 0.071557 0.073969 0.080992 0.081340 0.081431 0.079151 0.078671 11 0.0 0.069117 0.072168 0.079031 0.079900 0.080455 0.077870 0.077808 12 0.0 0.066984 0.070584 0.077297 0.078621 0.079584 0.076722 0.077031 13 0.0 0.065125 0.069200 0.075772 0.077496 0.078821 0.075706 0.076344 14 0.0 0.063508 0.067997 0.074439 0.076518 0.078164 0.074816 0.075749 8 9 10 11 12 13 14 0 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 1 0.074802 0.072449 0.071557 0.069117 0.066984 0.065125 0.063508 2 0.077623 0.075952 0.073969 0.072168 0.070584 0.069200 0.067997 3 0.084723 0.082939 0.080992 0.079031 0.077297 0.075772 0.074439 4 0.085401 0.084154 0.081340 0.079900 0.078621 0.077496 0.076518 5 0.085786 0.085015 0.081431 0.080455 0.079584 0.078821 0.078164 6 0.082993 0.081923 0.079151 0.077870 0.076722 0.075706 0.074816 7 0.082776 0.082128 0.078671 0.077808 0.077031 0.076344 0.075749 8 0.082517 0.082246 0.078172 0.077682 0.077236 0.076846 0.076519 9 0.082246 0.082311 0.077682 0.077525 0.077378 0.077256 0.077172 10 0.078172 0.077682 0.074339 0.073633 0.072989 0.072416 0.071916 11 0.077682 0.077525 0.073633 0.073260 0.072914 0.072607 0.072349 12 0.077236 0.077378 0.072989 0.072914 0.072835 0.072771 0.072733 13 0.076846 0.077256 0.072416 0.072607 0.072771 0.072926 0.073089 14 0.076519 0.077172 0.071916 0.072349 0.072733 0.073089 0.073437 \end{sphinxVerbatim} We note here that the covariance is zero for the first rows and columns since all matrix elements in the design matrix were set to one (we are fitting the function in terms of a polynomial of degree \(n\)). This means that the variance for these elements will be zero and will cause problems when we set up the correlation matrix. We can simply drop these elements and construct a correlation matrix without these elements. \subsection{Rewriting the Covariance and/or Correlation Matrix} \label{\detokenize{chapter4:rewriting-the-covariance-and-or-correlation-matrix}} We can rewrite the covariance matrix in a more compact form in terms of the design/feature matrix \(\boldsymbol{X}\) as \begin{equation*} \begin{split} \boldsymbol{C}[\boldsymbol{x}] = \frac{1}{n}\boldsymbol{X}^T\boldsymbol{X}= \mathbb{E}[\boldsymbol{X}^T\boldsymbol{X}]. \end{split} \end{equation*} To see this let us simply look at a design matrix \(\boldsymbol{X}\in {\mathbb{R}}^{2\times 2}\) \begin{equation*} \begin{split} \boldsymbol{X}=\begin{bmatrix} x_{00} & x_{01}\\ x_{10} & x_{11}\\ \end{bmatrix}=\begin{bmatrix} \boldsymbol{x}_{0} & \boldsymbol{x}_{1}\\ \end{bmatrix}. \end{split} \end{equation*} If we then compute the expectation value \begin{equation*} \begin{split} \mathbb{E}[\boldsymbol{X}^T\boldsymbol{X}] = \frac{1}{n}\boldsymbol{X}^T\boldsymbol{X}=\begin{bmatrix} x_{00}^2+x_{01}^2 & x_{00}x_{10}+x_{01}x_{11}\\ x_{10}x_{00}+x_{11}x_{01} & x_{10}^2+x_{11}^2\\ \end{bmatrix}, \end{split} \end{equation*} which is just \begin{equation*} \begin{split} \boldsymbol{C}[\boldsymbol{x}_0,\boldsymbol{x}_1] = \boldsymbol{C}[\boldsymbol{x}]=\begin{bmatrix} \mathrm{var}[\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_1] \\ \mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_0] & \mathrm{var}[\boldsymbol{x}_1] \\ \end{bmatrix}, \end{split} \end{equation*} where we wrote \$\(\boldsymbol{C}[\boldsymbol{x}_0,\boldsymbol{x}_1] = \boldsymbol{C}[\boldsymbol{x}]\)\( to indicate that this the covariance of the vectors \)\textbackslash{}boldsymbol\{x\}\( of the design/feature matrix \)\textbackslash{}boldsymbol\{X\}\$. It is easy to generalize this to a matrix \(\boldsymbol{X}\in {\mathbb{R}}^{n\times p}\). \subsection{Linking with SVD} \label{\detokenize{chapter4:linking-with-svd}} See lecture september 11. More text to be added here soon. \subsection{Where are we going?} \label{\detokenize{chapter4:where-are-we-going}} Before we proceed, we need to rethink what we have been doing. In our eager to fit the data, we have omitted several important elements in our regression analysis. In what follows we will \begin{enumerate} \sphinxsetlistlabels{\arabic}{enumi}{enumii}{}{.}% \item {} look at statistical properties, including a discussion of mean values, variance and the so\sphinxhyphen{}called bias\sphinxhyphen{}variance tradeoff \item {} introduce resampling techniques like cross\sphinxhyphen{}validation, bootstrapping and jackknife and more \end{enumerate} This will allow us to link the standard linear algebra methods we have discussed above to a statistical interpretation of the methods. \subsection{Resampling methods} \label{\detokenize{chapter4:resampling-methods}} Resampling methods are an indispensable tool in modern statistics. They involve repeatedly drawing samples from a training set and refitting a model of interest on each sample in order to obtain additional information about the fitted model. For example, in order to estimate the variability of a linear regression fit, we can repeatedly draw different samples from the training data, fit a linear regression to each new sample, and then examine the extent to which the resulting fits differ. Such an approach may allow us to obtain information that would not be available from fitting the model only once using the original training sample. Two resampling methods are often used in Machine Learning analyses, \begin{enumerate} \sphinxsetlistlabels{\arabic}{enumi}{enumii}{}{.}% \item {} The \sphinxstylestrong{bootstrap method} \item {} and \sphinxstylestrong{Cross\sphinxhyphen{}Validation} \end{enumerate} In addition there are several other methods such as the Jackknife and the Blocking methods. We will discuss in particular cross\sphinxhyphen{}validation and the bootstrap method. \subsection{Resampling approaches can be computationally expensive} \label{\detokenize{chapter4:resampling-approaches-can-be-computationally-expensive}} Resampling approaches can be computationally expensive, because they involve fitting the same statistical method multiple times using different subsets of the training data. However, due to recent advances in computing power, the computational requirements of resampling methods generally are not prohibitive. In this chapter, we discuss two of the most commonly used resampling methods, cross\sphinxhyphen{}validation and the bootstrap. Both methods are important tools in the practical application of many statistical learning procedures. For example, cross\sphinxhyphen{}validation can be used to estimate the test error associated with a given statistical learning method in order to evaluate its performance, or to select the appropriate level of flexibility. The process of evaluating a model’s performance is known as model assessment, whereas the process of selecting the proper level of flexibility for a model is known as model selection. The bootstrap is widely used. \subsection{Why resampling methods ?} \label{\detokenize{chapter4:why-resampling-methods}} \sphinxstylestrong{Statistical analysis.} \begin{itemize} \item {} Our simulations can be treated as \sphinxstyleemphasis{computer experiments}. This is particularly the case for Monte Carlo methods \item {} The results can be analysed with the same statistical tools as we would use analysing experimental data. \item {} As in all experiments, we are looking for expectation values and an estimate of how accurate they are, i.e., possible sources for errors. \end{itemize} \subsection{Statistical analysis} \label{\detokenize{chapter4:statistical-analysis}}\begin{itemize} \item {} As in other experiments, many numerical experiments have two classes of errors: \begin{itemize} \item {} Statistical errors \item {} Systematical errors \end{itemize} \item {} Statistical errors can be estimated using standard tools from statistics \item {} Systematical errors are method specific and must be treated differently from case to case. \end{itemize} \subsection{Linking the regression analysis with a statistical interpretation} \label{\detokenize{chapter4:linking-the-regression-analysis-with-a-statistical-interpretation}} The advantage of doing linear regression is that we actually end up with analytical expressions for several statistical quantities.Standard least squares and Ridge regression allow us to derive quantities like the variance and other expectation values in a rather straightforward way. It is assumed that \(\varepsilon_i \sim \mathcal{N}(0, \sigma^2)\) and the \(\varepsilon_{i}\) are independent, i.e.: \begin{equation*} \begin{split} \begin{align*} \mbox{Cov}(\varepsilon_{i_1}, \varepsilon_{i_2}) & = \left\{ \begin{array}{lcc} \sigma^2 & \mbox{if} & i_1 = i_2, \\ 0 & \mbox{if} & i_1 \not= i_2. \end{array} \right. \end{align*} \end{split} \end{equation*} The randomness of \(\varepsilon_i\) implies that \(\mathbf{y}_i\) is also a random variable. In particular, \(\mathbf{y}_i\) is normally distributed, because \(\varepsilon_i \sim \mathcal{N}(0, \sigma^2)\) and \(\mathbf{X}_{i,\ast} \, \boldsymbol{\beta}\) is a non\sphinxhyphen{}random scalar. To specify the parameters of the distribution of \(\mathbf{y}_i\) we need to calculate its first two moments. Recall that \(\boldsymbol{X}\) is a matrix of dimensionality \(n\times p\). The notation above \(\mathbf{X}_{i,\ast}\) means that we are looking at the row number \(i\) and perform a sum over all values \(p\). \subsection{Assumptions made} \label{\detokenize{chapter4:assumptions-made}} The assumption we have made here can be summarized as (and this is going to be useful when we discuss the bias\sphinxhyphen{}variance trade off) that there exists a function \(f(\boldsymbol{x})\) and a normal distributed error \(\boldsymbol{\varepsilon}\sim \mathcal{N}(0, \sigma^2)\) which describe our data \begin{equation*} \begin{split} \boldsymbol{y} = f(\boldsymbol{x})+\boldsymbol{\varepsilon} \end{split} \end{equation*} We approximate this function with our model from the solution of the linear regression equations, that is our function \(f\) is approximated by \(\boldsymbol{\tilde{y}}\) where we want to minimize \((\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\), our MSE, with \begin{equation*} \begin{split} \boldsymbol{\tilde{y}} = \boldsymbol{X}\boldsymbol{\beta}. \end{split} \end{equation*} \subsection{Expectation value and variance} \label{\detokenize{chapter4:expectation-value-and-variance}} We can calculate the expectation value of \(\boldsymbol{y}\) for a given element \(i\) \begin{equation*} \begin{split} \begin{align*} \mathbb{E}(y_i) & = \mathbb{E}(\mathbf{X}_{i, \ast} \, \boldsymbol{\beta}) + \mathbb{E}(\varepsilon_i) \, \, \, = \, \, \, \mathbf{X}_{i, \ast} \, \beta, \end{align*} \end{split} \end{equation*} while its variance is \begin{equation*} \begin{split} \begin{align*} \mbox{Var}(y_i) & = \mathbb{E} \{ [y_i - \mathbb{E}(y_i)]^2 \} \, \, \, = \, \, \, \mathbb{E} ( y_i^2 ) - [\mathbb{E}(y_i)]^2 \\ & = \mathbb{E} [ ( \mathbf{X}_{i, \ast} \, \beta + \varepsilon_i )^2] - ( \mathbf{X}_{i, \ast} \, \boldsymbol{\beta})^2 \\ & = \mathbb{E} [ ( \mathbf{X}_{i, \ast} \, \boldsymbol{\beta})^2 + 2 \varepsilon_i \mathbf{X}_{i, \ast} \, \boldsymbol{\beta} + \varepsilon_i^2 ] - ( \mathbf{X}_{i, \ast} \, \beta)^2 \\ & = ( \mathbf{X}_{i, \ast} \, \boldsymbol{\beta})^2 + 2 \mathbb{E}(\varepsilon_i) \mathbf{X}_{i, \ast} \, \boldsymbol{\beta} + \mathbb{E}(\varepsilon_i^2 ) - ( \mathbf{X}_{i, \ast} \, \boldsymbol{\beta})^2 \\ & = \mathbb{E}(\varepsilon_i^2 ) \, \, \, = \, \, \, \mbox{Var}(\varepsilon_i) \, \, \, = \, \, \, \sigma^2. \end{align*} \end{split} \end{equation*} Hence, \(y_i \sim \mathcal{N}( \mathbf{X}_{i, \ast} \, \boldsymbol{\beta}, \sigma^2)\), that is \(\boldsymbol{y}\) follows a normal distribution with mean value \(\boldsymbol{X}\boldsymbol{\beta}\) and variance \(\sigma^2\) (not be confused with the singular values of the SVD). \subsection{Expectation value and variance for \protect\(\boldsymbol{\beta}\protect\)} \label{\detokenize{chapter4:expectation-value-and-variance-for-boldsymbol-beta}} With the OLS expressions for the parameters \(\boldsymbol{\beta}\) we can evaluate the expectation value \begin{equation*} \begin{split} \mathbb{E}(\boldsymbol{\beta}) = \mathbb{E}[ (\mathbf{X}^{\top} \mathbf{X})^{-1}\mathbf{X}^{T} \mathbf{Y}]=(\mathbf{X}^{T} \mathbf{X})^{-1}\mathbf{X}^{T} \mathbb{E}[ \mathbf{Y}]=(\mathbf{X}^{T} \mathbf{X})^{-1} \mathbf{X}^{T}\mathbf{X}\boldsymbol{\beta}=\boldsymbol{\beta}. \end{split} \end{equation*} This means that the estimator of the regression parameters is unbiased. We can also calculate the variance The variance of \(\boldsymbol{\beta}\) is \begin{equation*} \begin{split} \begin{eqnarray*} \mbox{Var}(\boldsymbol{\beta}) & = & \mathbb{E} \{ [\boldsymbol{\beta} - \mathbb{E}(\boldsymbol{\beta})] [\boldsymbol{\beta} - \mathbb{E}(\boldsymbol{\beta})]^{T} \} \\ & = & \mathbb{E} \{ [(\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \mathbf{Y} - \boldsymbol{\beta}] \, [(\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \mathbf{Y} - \boldsymbol{\beta}]^{T} \} \\ % & = & \mathbb{E} \{ [(\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \mathbf{Y}] \, [(\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \mathbf{Y}]^{T} \} - \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} % \\ % & = & \mathbb{E} \{ (\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \mathbf{Y} \, \mathbf{Y}^{T} \, \mathbf{X} \, (\mathbf{X}^{T} \mathbf{X})^{-1} \} - \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} % \\ & = & (\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \, \mathbb{E} \{ \mathbf{Y} \, \mathbf{Y}^{T} \} \, \mathbf{X} \, (\mathbf{X}^{T} \mathbf{X})^{-1} - \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} \\ & = & (\mathbf{X}^{T} \mathbf{X})^{-1} \, \mathbf{X}^{T} \, \{ \mathbf{X} \, \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} \, \mathbf{X}^{T} + \sigma^2 \} \, \mathbf{X} \, (\mathbf{X}^{T} \mathbf{X})^{-1} - \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} % \\ % & = & (\mathbf{X}^T \mathbf{X})^{-1} \, \mathbf{X}^T \, \mathbf{X} \, \boldsymbol{\beta} \, \boldsymbol{\beta}^T \, \mathbf{X}^T \, \mathbf{X} \, (\mathbf{X}^T % \mathbf{X})^{-1} % \\ % & & + \, \, \sigma^2 \, (\mathbf{X}^T \mathbf{X})^{-1} \, \mathbf{X}^T \, \mathbf{X} \, (\mathbf{X}^T \mathbf{X})^{-1} - \boldsymbol{\beta} \boldsymbol{\beta}^T \\ & = & \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} + \sigma^2 \, (\mathbf{X}^{T} \mathbf{X})^{-1} - \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} \, \, \, = \, \, \, \sigma^2 \, (\mathbf{X}^{T} \mathbf{X})^{-1}, \end{eqnarray*} \end{split} \end{equation*} where we have used that \(\mathbb{E} (\mathbf{Y} \mathbf{Y}^{T}) = \mathbf{X} \, \boldsymbol{\beta} \, \boldsymbol{\beta}^{T} \, \mathbf{X}^{T} + \sigma^2 \, \mathbf{I}_{nn}\). From \(\mbox{Var}(\boldsymbol{\beta}) = \sigma^2 \, (\mathbf{X}^{T} \mathbf{X})^{-1}\), one obtains an estimate of the variance of the estimate of the \(j\)\sphinxhyphen{}th regression coefficient: \(\boldsymbol{\sigma}^2 (\boldsymbol{\beta}_j ) = \boldsymbol{\sigma}^2 \sqrt{ [(\mathbf{X}^{T} \mathbf{X})^{-1}]_{jj} }\). This may be used to construct a confidence interval for the estimates. In a similar way, we can obtain analytical expressions for say the expectation values of the parameters \(\boldsymbol{\beta}\) and their variance when we employ Ridge regression, allowing us again to define a confidence interval. It is rather straightforward to show that \begin{equation*} \begin{split} \mathbb{E} \big[ \boldsymbol{\beta}^{\mathrm{Ridge}} \big]=(\mathbf{X}^{T} \mathbf{X} + \lambda \mathbf{I}_{pp})^{-1} (\mathbf{X}^{\top} \mathbf{X})\boldsymbol{\beta}^{\mathrm{OLS}}. \end{split} \end{equation*} We see clearly that \(\mathbb{E} \big[ \boldsymbol{\beta}^{\mathrm{Ridge}} \big] \not= \boldsymbol{\beta}^{\mathrm{OLS}}\) for any \(\lambda > 0\). We say then that the ridge estimator is biased. We can also compute the variance as \begin{equation*} \begin{split} \mbox{Var}[\boldsymbol{\beta}^{\mathrm{Ridge}}]=\sigma^2[ \mathbf{X}^{T} \mathbf{X} + \lambda \mathbf{I} ]^{-1} \mathbf{X}^{T} \mathbf{X} \{ [ \mathbf{X}^{\top} \mathbf{X} + \lambda \mathbf{I} ]^{-1}\}^{T}, \end{split} \end{equation*} and it is easy to see that if the parameter \(\lambda\) goes to infinity then the variance of Ridge parameters \(\boldsymbol{\beta}\) goes to zero. With this, we can compute the difference \begin{equation*} \begin{split} \mbox{Var}[\boldsymbol{\beta}^{\mathrm{OLS}}]-\mbox{Var}(\boldsymbol{\beta}^{\mathrm{Ridge}})=\sigma^2 [ \mathbf{X}^{T} \mathbf{X} + \lambda \mathbf{I} ]^{-1}[ 2\lambda\mathbf{I} + \lambda^2 (\mathbf{X}^{T} \mathbf{X})^{-1} ] \{ [ \mathbf{X}^{T} \mathbf{X} + \lambda \mathbf{I} ]^{-1}\}^{T}. \end{split} \end{equation*} The difference is non\sphinxhyphen{}negative definite since each component of the matrix product is non\sphinxhyphen{}negative definite. This means the variance we obtain with the standard OLS will always for \(\lambda > 0\) be larger than the variance of \(\boldsymbol{\beta}\) obtained with the Ridge estimator. This has interesting consequences when we discuss the so\sphinxhyphen{}called bias\sphinxhyphen{}variance trade\sphinxhyphen{}off below. \subsection{Resampling methods} \label{\detokenize{chapter4:id9}} With all these analytical equations for both the OLS and Ridge regression, we will now outline how to assess a given model. This will lead us to a discussion of the so\sphinxhyphen{}called bias\sphinxhyphen{}variance tradeoff (see below) and so\sphinxhyphen{}called resampling methods. One of the quantities we have discussed as a way to measure errors is the mean\sphinxhyphen{}squared error (MSE), mainly used for fitting of continuous functions. Another choice is the absolute error. In the discussions below we will focus on the MSE and in particular since we will split the data into test and training data, we discuss the \begin{enumerate} \sphinxsetlistlabels{\arabic}{enumi}{enumii}{}{.}% \item {} prediction error or simply the \sphinxstylestrong{test error} \(\mathrm{Err_{Test}}\), where we have a fixed training set and the test error is the MSE arising from the data reserved for testing. We discuss also the \item {} training error \(\mathrm{Err_{Train}}\), which is the average loss over the training data. \end{enumerate} As our model becomes more and more complex, more of the training data tends to used. The training may thence adapt to more complicated structures in the data. This may lead to a decrease in the bias (see below for code example) and a slight increase of the variance for the test error. For a certain level of complexity the test error will reach minimum, before starting to increase again. The training error reaches a saturation. \subsection{Resampling methods: Jackknife and Bootstrap} \label{\detokenize{chapter4:resampling-methods-jackknife-and-bootstrap}} Two famous resampling methods are the \sphinxstylestrong{independent bootstrap} and \sphinxstylestrong{the jackknife}. The jackknife is a special case of the independent bootstrap. Still, the jackknife was made popular prior to the independent bootstrap. And as the popularity of the independent bootstrap soared, new variants, such as \sphinxstylestrong{the dependent bootstrap}. The Jackknife and independent bootstrap work for independent, identically distributed random variables. If these conditions are not satisfied, the methods will fail. Yet, it should be said that if the data are independent, identically distributed, and we only want to estimate the variance of \(\overline{X}\) (which often is the case), then there is no need for bootstrapping. \subsection{Resampling methods: Jackknife} \label{\detokenize{chapter4:resampling-methods-jackknife}} The Jackknife works by making many replicas of the estimator \(\widehat{\theta}\). The jackknife is a resampling method where we systematically leave out one observation from the vector of observed values \(\boldsymbol{x} = (x_1,x_2,\cdots,X_n)\). Let \(\boldsymbol{x}_i\) denote the vector \begin{equation*} \begin{split} \boldsymbol{x}_i = (x_1,x_2,\cdots,x_{i-1},x_{i+1},\cdots,x_n), \end{split} \end{equation*} which equals the vector \(\boldsymbol{x}\) with the exception that observation number \(i\) is left out. Using this notation, define \(\widehat{\theta}_i\) to be the estimator \(\widehat{\theta}\) computed using \(\vec{X}_i\). \subsection{Jackknife code example} \label{\detokenize{chapter4:jackknife-code-example}} \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{from} \PYG{n+nn}{numpy} \PYG{k+kn}{import} \PYG{o}{*} \PYG{k+kn}{from} \PYG{n+nn}{numpy}\PYG{n+nn}{.}\PYG{n+nn}{random} \PYG{k+kn}{import} \PYG{n}{randint}\PYG{p}{,} \PYG{n}{randn} \PYG{k+kn}{from} \PYG{n+nn}{time} \PYG{k+kn}{import} \PYG{n}{time} \PYG{k}{def} \PYG{n+nf}{jackknife}\PYG{p}{(}\PYG{n}{data}\PYG{p}{,} \PYG{n}{stat}\PYG{p}{)}\PYG{p}{:} \PYG{n}{n} \PYG{o}{=} \PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{data}\PYG{p}{)}\PYG{p}{;}\PYG{n}{t} \PYG{o}{=} \PYG{n}{zeros}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)}\PYG{p}{;} \PYG{n}{inds} \PYG{o}{=} \PYG{n}{arange}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)}\PYG{p}{;} \PYG{n}{t0} \PYG{o}{=} \PYG{n}{time}\PYG{p}{(}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{}\PYGZsh{} \PYGZsq{}jackknifing\PYGZsq{} by leaving out an observation for each i } \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)}\PYG{p}{:} \PYG{n}{t}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{n}{stat}\PYG{p}{(}\PYG{n}{delete}\PYG{p}{(}\PYG{n}{data}\PYG{p}{,}\PYG{n}{i}\PYG{p}{)} \PYG{p}{)} \PYG{c+c1}{\PYGZsh{} analysis } \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Runtime: }\PYG{l+s+si}{\PYGZpc{}g}\PYG{l+s+s2}{ sec}\PYG{l+s+s2}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{time}\PYG{p}{(}\PYG{p}{)}\PYG{o}{\PYGZhy{}}\PYG{n}{t0}\PYG{p}{)}\PYG{p}{)}\PYG{p}{;} \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Jackknife Statistics :}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{original bias std. error}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+si}{\PYGZpc{}8g}\PYG{l+s+s2}{ }\PYG{l+s+si}{\PYGZpc{}14g}\PYG{l+s+s2}{ }\PYG{l+s+si}{\PYGZpc{}15g}\PYG{l+s+s2}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{stat}\PYG{p}{(}\PYG{n}{data}\PYG{p}{)}\PYG{p}{,}\PYG{p}{(}\PYG{n}{n}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{t}\PYG{p}{)}\PYG{o}{/}\PYG{n}{n}\PYG{p}{,} \PYG{p}{(}\PYG{n}{n}\PYG{o}{*}\PYG{n}{var}\PYG{p}{(}\PYG{n}{t}\PYG{p}{)}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{o}{.}\PYG{l+m+mi}{5}\PYG{p}{)}\PYG{p}{)} \PYG{k}{return} \PYG{n}{t} \PYG{c+c1}{\PYGZsh{} Returns mean of data samples } \PYG{k}{def} \PYG{n+nf}{stat}\PYG{p}{(}\PYG{n}{data}\PYG{p}{)}\PYG{p}{:} \PYG{k}{return} \PYG{n}{mean}\PYG{p}{(}\PYG{n}{data}\PYG{p}{)} \PYG{n}{mu}\PYG{p}{,} \PYG{n}{sigma} \PYG{o}{=} \PYG{l+m+mi}{100}\PYG{p}{,} \PYG{l+m+mi}{15} \PYG{n}{datapoints} \PYG{o}{=} \PYG{l+m+mi}{10000} \PYG{n}{x} \PYG{o}{=} \PYG{n}{mu} \PYG{o}{+} \PYG{n}{sigma}\PYG{o}{*}\PYG{n}{random}\PYG{o}{.}\PYG{n}{randn}\PYG{p}{(}\PYG{n}{datapoints}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} jackknife returns the data sample } \PYG{n}{t} \PYG{o}{=} \PYG{n}{jackknife}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{stat}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] Runtime: 0.389789 sec Jackknife Statistics : original bias std. error 100.259 100.249 0.149656 \end{sphinxVerbatim} \subsection{Resampling methods: Bootstrap} \label{\detokenize{chapter4:resampling-methods-bootstrap}} Bootstrapping is a nonparametric approach to statistical inference that substitutes computation for more traditional distributional assumptions and asymptotic results. Bootstrapping offers a number of advantages: \begin{enumerate} \sphinxsetlistlabels{\arabic}{enumi}{enumii}{}{.}% \item {} The bootstrap is quite general, although there are some cases in which it fails. \item {} Because it does not require distributional assumptions (such as normally distributed errors), the bootstrap can provide more accurate inferences when the data are not well behaved or when the sample size is small. \item {} It is possible to apply the bootstrap to statistics with sampling distributions that are difficult to derive, even asymptotically. \item {} It is relatively simple to apply the bootstrap to complex data\sphinxhyphen{}collection plans (such as stratified and clustered samples). \end{enumerate} \subsection{Resampling methods: Bootstrap background} \label{\detokenize{chapter4:resampling-methods-bootstrap-background}} Since \(\widehat{\theta} = \widehat{\theta}(\boldsymbol{X})\) is a function of random variables, \(\widehat{\theta}\) itself must be a random variable. Thus it has a pdf, call this function \(p(\boldsymbol{t})\). The aim of the bootstrap is to estimate \(p(\boldsymbol{t})\) by the relative frequency of \(\widehat{\theta}\). You can think of this as using a histogram in the place of \(p(\boldsymbol{t})\). If the relative frequency closely resembles \(p(\vec{t})\), then using numerics, it is straight forward to estimate all the interesting parameters of \(p(\boldsymbol{t})\) using point estimators. \subsection{Resampling methods: More Bootstrap background} \label{\detokenize{chapter4:resampling-methods-more-bootstrap-background}} In the case that \(\widehat{\theta}\) has more than one component, and the components are independent, we use the same estimator on each component separately. If the probability density function of \(X_i\), \(p(x)\), had been known, then it would have been straight forward to do this by: \begin{enumerate} \sphinxsetlistlabels{\arabic}{enumi}{enumii}{}{.}% \item {} Drawing lots of numbers from \(p(x)\), suppose we call one such set of numbers \((X_1^*, X_2^*, \cdots, X_n^*)\). \item {} Then using these numbers, we could compute a replica of \(\widehat{\theta}\) called \(\widehat{\theta}^*\). \end{enumerate} By repeated use of (1) and (2), many estimates of \(\widehat{\theta}\) could have been obtained. The idea is to use the relative frequency of \(\widehat{\theta}^*\) (think of a histogram) as an estimate of \(p(\boldsymbol{t})\). \subsection{Resampling methods: Bootstrap approach} \label{\detokenize{chapter4:resampling-methods-bootstrap-approach}} But unless there is enough information available about the process that generated \(X_1,X_2,\cdots,X_n\), \(p(x)\) is in general unknown. Therefore, \sphinxhref{https://projecteuclid.org/euclid.aos/1176344552}{Efron in 1979} asked the question: What if we replace \(p(x)\) by the relative frequency of the observation \(X_i\); if we draw observations in accordance with the relative frequency of the observations, will we obtain the same result in some asymptotic sense? The answer is yes. Instead of generating the histogram for the relative frequency of the observation \(X_i\), just draw the values \((X_1^*,X_2^*,\cdots,X_n^*)\) with replacement from the vector \(\boldsymbol{X}\). \subsection{Resampling methods: Bootstrap steps} \label{\detokenize{chapter4:resampling-methods-bootstrap-steps}} The independent bootstrap works like this: \begin{enumerate} \sphinxsetlistlabels{\arabic}{enumi}{enumii}{}{.}% \item {} Draw with replacement \(n\) numbers for the observed variables \(\boldsymbol{x} = (x_1,x_2,\cdots,x_n)\). \item {} Define a vector \(\boldsymbol{x}^*\) containing the values which were drawn from \(\boldsymbol{x}\). \item {} Using the vector \(\boldsymbol{x}^*\) compute \(\widehat{\theta}^*\) by evaluating \(\widehat \theta\) under the observations \(\boldsymbol{x}^*\). \item {} Repeat this process \(k\) times. \end{enumerate} When you are done, you can draw a histogram of the relative frequency of \(\widehat \theta^*\). This is your estimate of the probability distribution \(p(t)\). Using this probability distribution you can estimate any statistics thereof. In principle you never draw the histogram of the relative frequency of \(\widehat{\theta}^*\). Instead you use the estimators corresponding to the statistic of interest. For example, if you are interested in estimating the variance of \(\widehat \theta\), apply the etsimator \(\widehat \sigma^2\) to the values \(\widehat \theta ^*\). \subsection{Code example for the Bootstrap method} \label{\detokenize{chapter4:code-example-for-the-bootstrap-method}} The following code starts with a Gaussian distribution with mean value \(\mu =100\) and variance \(\sigma=15\). We use this to generate the data used in the bootstrap analysis. The bootstrap analysis returns a data set after a given number of bootstrap operations (as many as we have data points). This data set consists of estimated mean values for each bootstrap operation. The histogram generated by the bootstrap method shows that the distribution for these mean values is also a Gaussian, centered around the mean value \(\mu=100\) but with standard deviation \(\sigma/\sqrt{n}\), where \(n\) is the number of bootstrap samples (in this case the same as the number of original data points). The value of the standard deviation is what we expect from the central limit theorem. \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{from} \PYG{n+nn}{numpy} \PYG{k+kn}{import} \PYG{o}{*} \PYG{k+kn}{from} \PYG{n+nn}{numpy}\PYG{n+nn}{.}\PYG{n+nn}{random} \PYG{k+kn}{import} \PYG{n}{randint}\PYG{p}{,} \PYG{n}{randn} \PYG{k+kn}{from} \PYG{n+nn}{time} \PYG{k+kn}{import} \PYG{n}{time} \PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{mlab} \PYG{k}{as} \PYG{n+nn}{mlab} \PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt} \PYG{c+c1}{\PYGZsh{} Returns mean of bootstrap samples } \PYG{k}{def} \PYG{n+nf}{stat}\PYG{p}{(}\PYG{n}{data}\PYG{p}{)}\PYG{p}{:} \PYG{k}{return} \PYG{n}{mean}\PYG{p}{(}\PYG{n}{data}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Bootstrap algorithm} \PYG{k}{def} \PYG{n+nf}{bootstrap}\PYG{p}{(}\PYG{n}{data}\PYG{p}{,} \PYG{n}{statistic}\PYG{p}{,} \PYG{n}{R}\PYG{p}{)}\PYG{p}{:} \PYG{n}{t} \PYG{o}{=} \PYG{n}{zeros}\PYG{p}{(}\PYG{n}{R}\PYG{p}{)}\PYG{p}{;} \PYG{n}{n} \PYG{o}{=} \PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{data}\PYG{p}{)}\PYG{p}{;} \PYG{n}{inds} \PYG{o}{=} \PYG{n}{arange}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)}\PYG{p}{;} \PYG{n}{t0} \PYG{o}{=} \PYG{n}{time}\PYG{p}{(}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} non\PYGZhy{}parametric bootstrap } \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{R}\PYG{p}{)}\PYG{p}{:} \PYG{n}{t}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{n}{statistic}\PYG{p}{(}\PYG{n}{data}\PYG{p}{[}\PYG{n}{randint}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{n}{n}\PYG{p}{,}\PYG{n}{n}\PYG{p}{)}\PYG{p}{]}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} analysis } \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Runtime: }\PYG{l+s+si}{\PYGZpc{}g}\PYG{l+s+s2}{ sec}\PYG{l+s+s2}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{time}\PYG{p}{(}\PYG{p}{)}\PYG{o}{\PYGZhy{}}\PYG{n}{t0}\PYG{p}{)}\PYG{p}{)}\PYG{p}{;} \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Bootstrap Statistics :}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{original bias std. error}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+si}{\PYGZpc{}8g}\PYG{l+s+s2}{ }\PYG{l+s+si}{\PYGZpc{}8g}\PYG{l+s+s2}{ }\PYG{l+s+si}{\PYGZpc{}14g}\PYG{l+s+s2}{ }\PYG{l+s+si}{\PYGZpc{}15g}\PYG{l+s+s2}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{statistic}\PYG{p}{(}\PYG{n}{data}\PYG{p}{)}\PYG{p}{,} \PYG{n}{std}\PYG{p}{(}\PYG{n}{data}\PYG{p}{)}\PYG{p}{,}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{t}\PYG{p}{)}\PYG{p}{,}\PYG{n}{std}\PYG{p}{(}\PYG{n}{t}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)} \PYG{k}{return} \PYG{n}{t} \PYG{n}{mu}\PYG{p}{,} \PYG{n}{sigma} \PYG{o}{=} \PYG{l+m+mi}{100}\PYG{p}{,} \PYG{l+m+mi}{15} \PYG{n}{datapoints} \PYG{o}{=} \PYG{l+m+mi}{10000} \PYG{n}{x} \PYG{o}{=} \PYG{n}{mu} \PYG{o}{+} \PYG{n}{sigma}\PYG{o}{*}\PYG{n}{random}\PYG{o}{.}\PYG{n}{randn}\PYG{p}{(}\PYG{n}{datapoints}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} bootstrap returns the data sample } \PYG{n}{t} \PYG{o}{=} \PYG{n}{bootstrap}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{stat}\PYG{p}{,} \PYG{n}{datapoints}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} the histogram of the bootstrapped data } \PYG{n}{n}\PYG{p}{,} \PYG{n}{binsboot}\PYG{p}{,} \PYG{n}{patches} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{hist}\PYG{p}{(}\PYG{n}{t}\PYG{p}{,} \PYG{l+m+mi}{50}\PYG{p}{,} \PYG{n}{normed}\PYG{o}{=}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{facecolor}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{red}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{alpha}\PYG{o}{=}\PYG{l+m+mf}{0.75}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} add a \PYGZsq{}best fit\PYGZsq{} line } \PYG{n}{y} \PYG{o}{=} \PYG{n}{mlab}\PYG{o}{.}\PYG{n}{normpdf}\PYG{p}{(} \PYG{n}{binsboot}\PYG{p}{,} \PYG{n}{mean}\PYG{p}{(}\PYG{n}{t}\PYG{p}{)}\PYG{p}{,} \PYG{n}{std}\PYG{p}{(}\PYG{n}{t}\PYG{p}{)}\PYG{p}{)} \PYG{n}{lt} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{binsboot}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{r\PYGZhy{}\PYGZhy{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{linewidth}\PYG{o}{=}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{xlabel}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Smarts}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{ylabel}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Probability}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{axis}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mf}{99.5}\PYG{p}{,} \PYG{l+m+mf}{100.6}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mf}{3.0}\PYG{p}{]}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{grid}\PYG{p}{(}\PYG{k+kc}{True}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] Runtime: 2.00056 sec Bootstrap Statistics : original bias std. error 99.9087 15.0416 99.9098 0.150465 \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{g+gt}{\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}} \PYG{n+ne}{AttributeError}\PYG{g+gWhitespace}{ }Traceback (most recent call last) \PYG{o}{\PYGZlt{}}\PYG{n}{ipython}\PYG{o}{\PYGZhy{}}\PYG{n+nb}{input}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{29}\PYG{o}{\PYGZhy{}}\PYG{l+m+mf}{53990135e988}\PYG{o}{\PYGZgt{}} \PYG{o+ow}{in} \PYG{o}{\PYGZlt{}}\PYG{n}{module}\PYG{o}{\PYGZgt{}} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{29} \PYG{n}{t} \PYG{o}{=} \PYG{n}{bootstrap}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{stat}\PYG{p}{,} \PYG{n}{datapoints}\PYG{p}{)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{30} \PYG{c+c1}{\PYGZsh{} the histogram of the bootstrapped data} \PYG{n+ne}{\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{31} \PYG{n}{n}\PYG{p}{,} \PYG{n}{binsboot}\PYG{p}{,} \PYG{n}{patches} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{hist}\PYG{p}{(}\PYG{n}{t}\PYG{p}{,} \PYG{l+m+mi}{50}\PYG{p}{,} \PYG{n}{normed}\PYG{o}{=}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{facecolor}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{red}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{alpha}\PYG{o}{=}\PYG{l+m+mf}{0.75}\PYG{p}{)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{32} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{33} \PYG{c+c1}{\PYGZsh{} add a \PYGZsq{}best fit\PYGZsq{} line} \PYG{n+nn}{\PYGZti{}/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/matplotlib/pyplot.py} in \PYG{n+ni}{hist}\PYG{n+nt}{(x, bins, range, density, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, data, **kwargs)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{2603} \PYG{n}{orientation}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{vertical}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{rwidth}\PYG{o}{=}\PYG{k+kc}{None}\PYG{p}{,} \PYG{n}{log}\PYG{o}{=}\PYG{k+kc}{False}\PYG{p}{,} \PYG{n}{color}\PYG{o}{=}\PYG{k+kc}{None}\PYG{p}{,} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{2604} \PYG{n}{label}\PYG{o}{=}\PYG{k+kc}{None}\PYG{p}{,} \PYG{n}{stacked}\PYG{o}{=}\PYG{k+kc}{False}\PYG{p}{,} \PYG{o}{*}\PYG{p}{,} \PYG{n}{data}\PYG{o}{=}\PYG{k+kc}{None}\PYG{p}{,} \PYG{o}{*}\PYG{o}{*}\PYG{n}{kwargs}\PYG{p}{)}\PYG{p}{:} \PYG{n+ne}{\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{2605} \PYG{k}{return} \PYG{n}{gca}\PYG{p}{(}\PYG{p}{)}\PYG{o}{.}\PYG{n}{hist}\PYG{p}{(} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{2606} \PYG{n}{x}\PYG{p}{,} \PYG{n}{bins}\PYG{o}{=}\PYG{n}{bins}\PYG{p}{,} \PYG{n+nb}{range}\PYG{o}{=}\PYG{n+nb}{range}\PYG{p}{,} \PYG{n}{density}\PYG{o}{=}\PYG{n}{density}\PYG{p}{,} \PYG{n}{weights}\PYG{o}{=}\PYG{n}{weights}\PYG{p}{,} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{2607} \PYG{n}{cumulative}\PYG{o}{=}\PYG{n}{cumulative}\PYG{p}{,} \PYG{n}{bottom}\PYG{o}{=}\PYG{n}{bottom}\PYG{p}{,} \PYG{n}{histtype}\PYG{o}{=}\PYG{n}{histtype}\PYG{p}{,} \PYG{n+nn}{\PYGZti{}/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/matplotlib/\PYGZus{}\PYGZus{}init\PYGZus{}\PYGZus{}.py} in \PYG{n+ni}{inner}\PYG{n+nt}{(ax, data, *args, **kwargs)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{1563} \PYG{k}{def} \PYG{n+nf}{inner}\PYG{p}{(}\PYG{n}{ax}\PYG{p}{,} \PYG{o}{*}\PYG{n}{args}\PYG{p}{,} \PYG{n}{data}\PYG{o}{=}\PYG{k+kc}{None}\PYG{p}{,} \PYG{o}{*}\PYG{o}{*}\PYG{n}{kwargs}\PYG{p}{)}\PYG{p}{:} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{1564} \PYG{k}{if} \PYG{n}{data} \PYG{o+ow}{is} \PYG{k+kc}{None}\PYG{p}{:} \PYG{n+ne}{\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{1565} \PYG{k}{return} \PYG{n}{func}\PYG{p}{(}\PYG{n}{ax}\PYG{p}{,} \PYG{o}{*}\PYG{n+nb}{map}\PYG{p}{(}\PYG{n}{sanitize\PYGZus{}sequence}\PYG{p}{,} \PYG{n}{args}\PYG{p}{)}\PYG{p}{,} \PYG{o}{*}\PYG{o}{*}\PYG{n}{kwargs}\PYG{p}{)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{1566} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{1567} \PYG{n}{bound} \PYG{o}{=} \PYG{n}{new\PYGZus{}sig}\PYG{o}{.}\PYG{n}{bind}\PYG{p}{(}\PYG{n}{ax}\PYG{p}{,} \PYG{o}{*}\PYG{n}{args}\PYG{p}{,} \PYG{o}{*}\PYG{o}{*}\PYG{n}{kwargs}\PYG{p}{)} \PYG{n+nn}{\PYGZti{}/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/matplotlib/axes/\PYGZus{}axes.py} in \PYG{n+ni}{hist}\PYG{n+nt}{(self, x, bins, range, density, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, **kwargs)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{6817} \PYG{k}{if} \PYG{n}{patch}\PYG{p}{:} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{6818} \PYG{n}{p} \PYG{o}{=} \PYG{n}{patch}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]} \PYG{n+ne}{\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{6819} \PYG{n}{p}\PYG{o}{.}\PYG{n}{update}\PYG{p}{(}\PYG{n}{kwargs}\PYG{p}{)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{6820} \PYG{k}{if} \PYG{n}{lbl} \PYG{o+ow}{is} \PYG{o+ow}{not} \PYG{k+kc}{None}\PYG{p}{:} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{6821} \PYG{n}{p}\PYG{o}{.}\PYG{n}{set\PYGZus{}label}\PYG{p}{(}\PYG{n}{lbl}\PYG{p}{)} \PYG{n+nn}{\PYGZti{}/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/matplotlib/artist.py} in \PYG{n+ni}{update}\PYG{n+nt}{(self, props)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{1004} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{1005} \PYG{k}{with} \PYG{n}{cbook}\PYG{o}{.}\PYG{n}{\PYGZus{}setattr\PYGZus{}cm}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{,} \PYG{n}{eventson}\PYG{o}{=}\PYG{k+kc}{False}\PYG{p}{)}\PYG{p}{:} \PYG{n+ne}{\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{1006} \PYG{n}{ret} \PYG{o}{=} \PYG{p}{[}\PYG{n}{\PYGZus{}update\PYGZus{}property}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{,} \PYG{n}{k}\PYG{p}{,} \PYG{n}{v}\PYG{p}{)} \PYG{k}{for} \PYG{n}{k}\PYG{p}{,} \PYG{n}{v} \PYG{o+ow}{in} \PYG{n}{props}\PYG{o}{.}\PYG{n}{items}\PYG{p}{(}\PYG{p}{)}\PYG{p}{]} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{1007} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{1008} \PYG{k}{if} \PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{ret}\PYG{p}{)}\PYG{p}{:} \PYG{n+nn}{\PYGZti{}/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/matplotlib/artist.py} in \PYG{n+ni}{\PYGZlt{}listcomp\PYGZgt{}}\PYG{n+nt}{(.0)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{1004} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{1005} \PYG{k}{with} \PYG{n}{cbook}\PYG{o}{.}\PYG{n}{\PYGZus{}setattr\PYGZus{}cm}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{,} \PYG{n}{eventson}\PYG{o}{=}\PYG{k+kc}{False}\PYG{p}{)}\PYG{p}{:} \PYG{n+ne}{\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{1006} \PYG{n}{ret} \PYG{o}{=} \PYG{p}{[}\PYG{n}{\PYGZus{}update\PYGZus{}property}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{,} \PYG{n}{k}\PYG{p}{,} \PYG{n}{v}\PYG{p}{)} \PYG{k}{for} \PYG{n}{k}\PYG{p}{,} \PYG{n}{v} \PYG{o+ow}{in} \PYG{n}{props}\PYG{o}{.}\PYG{n}{items}\PYG{p}{(}\PYG{p}{)}\PYG{p}{]} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{1007} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{1008} \PYG{k}{if} \PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{ret}\PYG{p}{)}\PYG{p}{:} \PYG{n+nn}{\PYGZti{}/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/matplotlib/artist.py} in \PYG{n+ni}{\PYGZus{}update\PYGZus{}property}\PYG{n+nt}{(self, k, v)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{999} \PYG{n}{func} \PYG{o}{=} \PYG{n+nb}{getattr}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{set\PYGZus{}}\PYG{l+s+s1}{\PYGZsq{}} \PYG{o}{+} \PYG{n}{k}\PYG{p}{,} \PYG{k+kc}{None}\PYG{p}{)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{1000} \PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{callable}\PYG{p}{(}\PYG{n}{func}\PYG{p}{)}\PYG{p}{:} \PYG{n+ne}{\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{1001} \PYG{k}{raise} \PYG{n+ne}{AttributeError}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+si}{\PYGZob{}!r\PYGZcb{}}\PYG{l+s+s1}{ object has no property }\PYG{l+s+si}{\PYGZob{}!r\PYGZcb{}}\PYG{l+s+s1}{\PYGZsq{}} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{1002} \PYG{o}{.}\PYG{n}{format}\PYG{p}{(}\PYG{n+nb}{type}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{)}\PYG{o}{.}\PYG{n+nv+vm}{\PYGZus{}\PYGZus{}name\PYGZus{}\PYGZus{}}\PYG{p}{,} \PYG{n}{k}\PYG{p}{)}\PYG{p}{)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{1003} \PYG{k}{return} \PYG{n}{func}\PYG{p}{(}\PYG{n}{v}\PYG{p}{)} \PYG{n+ne}{AttributeError}: \PYGZsq{}Rectangle\PYGZsq{} object has no property \PYGZsq{}normed\PYGZsq{} \end{sphinxVerbatim} \noindent\sphinxincludegraphics{{chapter4_278_2}.png} \subsection{Various steps in cross\sphinxhyphen{}validation} \label{\detokenize{chapter4:various-steps-in-cross-validation}} When the repetitive splitting of the data set is done randomly, samples may accidently end up in a fast majority of the splits in either training or test set. Such samples may have an unbalanced influence on either model building or prediction evaluation. To avoid this \(k\)\sphinxhyphen{}fold cross\sphinxhyphen{}validation structures the data splitting. The samples are divided into \(k\) more or less equally sized exhaustive and mutually exclusive subsets. In turn (at each split) one of these subsets plays the role of the test set while the union of the remaining subsets constitutes the training set. Such a splitting warrants a balanced representation of each sample in both training and test set over the splits. Still the division into the \(k\) subsets involves a degree of randomness. This may be fully excluded when choosing \(k=n\). This particular case is referred to as leave\sphinxhyphen{}one\sphinxhyphen{}out cross\sphinxhyphen{}validation (LOOCV). \subsection{How to set up the cross\sphinxhyphen{}validation for Ridge and/or Lasso} \label{\detokenize{chapter4:how-to-set-up-the-cross-validation-for-ridge-and-or-lasso}}\begin{itemize} \item {} Define a range of interest for the penalty parameter. \item {} Divide the data set into training and test set comprising samples \(\{1, \ldots, n\} \setminus i\) and \(\{ i \}\), respectively. \item {} Fit the linear regression model by means of ridge estimation for each \(\lambda\) in the grid using the training set, and the corresponding estimate of the error variance \(\boldsymbol{\sigma}_{-i}^2(\lambda)\), as \end{itemize} \begin{equation*} \begin{split} \begin{align*} \boldsymbol{\beta}_{-i}(\lambda) & = ( \boldsymbol{X}_{-i, \ast}^{T} \boldsymbol{X}_{-i, \ast} + \lambda \boldsymbol{I}_{pp})^{-1} \boldsymbol{X}_{-i, \ast}^{T} \boldsymbol{y}_{-i} \end{align*} \end{split} \end{equation*}\begin{itemize} \item {} Evaluate the prediction performance of these models on the test set by \(\log\{L[y_i, \boldsymbol{X}_{i, \ast}; \boldsymbol{\beta}_{-i}(\lambda), \boldsymbol{\sigma}_{-i}^2(\lambda)]\}\). Or, by the prediction error \(|y_i - \boldsymbol{X}_{i, \ast} \boldsymbol{\beta}_{-i}(\lambda)|\), the relative error, the error squared or the R2 score function. \item {} Repeat the first three steps such that each sample plays the role of the test set once. \item {} Average the prediction performances of the test sets at each grid point of the penalty bias/parameter. It is an estimate of the prediction performance of the model corresponding to this value of the penalty parameter on novel data. It is defined as \end{itemize} \begin{equation*} \begin{split} \begin{align*} \frac{1}{n} \sum_{i = 1}^n \log\{L[y_i, \mathbf{X}_{i, \ast}; \boldsymbol{\beta}_{-i}(\lambda), \boldsymbol{\sigma}_{-i}^2(\lambda)]\}. \end{align*} \end{split} \end{equation*} \subsection{Cross\sphinxhyphen{}validation in brief} \label{\detokenize{chapter4:cross-validation-in-brief}} For the various values of \(k\) \begin{enumerate} \sphinxsetlistlabels{\arabic}{enumi}{enumii}{}{.}% \item {} shuffle the dataset randomly. \item {} Split the dataset into \(k\) groups. \item {} For each unique group: \end{enumerate} a. Decide which group to use as set for test data b. Take the remaining groups as a training data set c. Fit a model on the training set and evaluate it on the test set d. Retain the evaluation score and discard the model \begin{enumerate} \sphinxsetlistlabels{\arabic}{enumi}{enumii}{}{.}% \item {} Summarize the model using the sample of model evaluation scores \end{enumerate} \subsection{Code Example for Cross\sphinxhyphen{}validation and \protect\(k\protect\)\sphinxhyphen{}fold Cross\sphinxhyphen{}validation} \label{\detokenize{chapter4:code-example-for-cross-validation-and-k-fold-cross-validation}} The code here uses Ridge regression with cross\sphinxhyphen{}validation (CV) resampling and \(k\)\sphinxhyphen{}fold CV in order to fit a specific polynomial. \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{KFold} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n}{Ridge} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{cross\PYGZus{}val\PYGZus{}score} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{preprocessing} \PYG{k+kn}{import} \PYG{n}{PolynomialFeatures} \PYG{c+c1}{\PYGZsh{} A seed just to ensure that the random numbers are the same for every run.} \PYG{c+c1}{\PYGZsh{} Useful for eventual debugging.} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{seed}\PYG{p}{(}\PYG{l+m+mi}{3155}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Generate the data.} \PYG{n}{nsamples} \PYG{o}{=} \PYG{l+m+mi}{100} \PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{randn}\PYG{p}{(}\PYG{n}{nsamples}\PYG{p}{)} \PYG{n}{y} \PYG{o}{=} \PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{randn}\PYG{p}{(}\PYG{n}{nsamples}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{}\PYGZsh{} Cross\PYGZhy{}validation on Ridge regression using KFold only} \PYG{c+c1}{\PYGZsh{} Decide degree on polynomial to fit} \PYG{n}{poly} \PYG{o}{=} \PYG{n}{PolynomialFeatures}\PYG{p}{(}\PYG{n}{degree} \PYG{o}{=} \PYG{l+m+mi}{6}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Decide which values of lambda to use} \PYG{n}{nlambdas} \PYG{o}{=} \PYG{l+m+mi}{500} \PYG{n}{lambdas} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{logspace}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{,} \PYG{n}{nlambdas}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Initialize a KFold instance} \PYG{n}{k} \PYG{o}{=} \PYG{l+m+mi}{5} \PYG{n}{kfold} \PYG{o}{=} \PYG{n}{KFold}\PYG{p}{(}\PYG{n}{n\PYGZus{}splits} \PYG{o}{=} \PYG{n}{k}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Perform the cross\PYGZhy{}validation to estimate MSE} \PYG{n}{scores\PYGZus{}KFold} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{p}{(}\PYG{n}{nlambdas}\PYG{p}{,} \PYG{n}{k}\PYG{p}{)}\PYG{p}{)} \PYG{n}{i} \PYG{o}{=} \PYG{l+m+mi}{0} \PYG{k}{for} \PYG{n}{lmb} \PYG{o+ow}{in} \PYG{n}{lambdas}\PYG{p}{:} \PYG{n}{ridge} \PYG{o}{=} \PYG{n}{Ridge}\PYG{p}{(}\PYG{n}{alpha} \PYG{o}{=} \PYG{n}{lmb}\PYG{p}{)} \PYG{n}{j} \PYG{o}{=} \PYG{l+m+mi}{0} \PYG{k}{for} \PYG{n}{train\PYGZus{}inds}\PYG{p}{,} \PYG{n}{test\PYGZus{}inds} \PYG{o+ow}{in} \PYG{n}{kfold}\PYG{o}{.}\PYG{n}{split}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{:} \PYG{n}{xtrain} \PYG{o}{=} \PYG{n}{x}\PYG{p}{[}\PYG{n}{train\PYGZus{}inds}\PYG{p}{]} \PYG{n}{ytrain} \PYG{o}{=} \PYG{n}{y}\PYG{p}{[}\PYG{n}{train\PYGZus{}inds}\PYG{p}{]} \PYG{n}{xtest} \PYG{o}{=} \PYG{n}{x}\PYG{p}{[}\PYG{n}{test\PYGZus{}inds}\PYG{p}{]} \PYG{n}{ytest} \PYG{o}{=} \PYG{n}{y}\PYG{p}{[}\PYG{n}{test\PYGZus{}inds}\PYG{p}{]} \PYG{n}{Xtrain} \PYG{o}{=} \PYG{n}{poly}\PYG{o}{.}\PYG{n}{fit\PYGZus{}transform}\PYG{p}{(}\PYG{n}{xtrain}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{)} \PYG{n}{ridge}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{Xtrain}\PYG{p}{,} \PYG{n}{ytrain}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{)} \PYG{n}{Xtest} \PYG{o}{=} \PYG{n}{poly}\PYG{o}{.}\PYG{n}{fit\PYGZus{}transform}\PYG{p}{(}\PYG{n}{xtest}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{)} \PYG{n}{ypred} \PYG{o}{=} \PYG{n}{ridge}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{Xtest}\PYG{p}{)} \PYG{n}{scores\PYGZus{}KFold}\PYG{p}{[}\PYG{n}{i}\PYG{p}{,}\PYG{n}{j}\PYG{p}{]} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{sum}\PYG{p}{(}\PYG{p}{(}\PYG{n}{ypred} \PYG{o}{\PYGZhy{}} \PYG{n}{ytest}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{/}\PYG{n}{np}\PYG{o}{.}\PYG{n}{size}\PYG{p}{(}\PYG{n}{ypred}\PYG{p}{)} \PYG{n}{j} \PYG{o}{+}\PYG{o}{=} \PYG{l+m+mi}{1} \PYG{n}{i} \PYG{o}{+}\PYG{o}{=} \PYG{l+m+mi}{1} \PYG{n}{estimated\PYGZus{}mse\PYGZus{}KFold} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{scores\PYGZus{}KFold}\PYG{p}{,} \PYG{n}{axis} \PYG{o}{=} \PYG{l+m+mi}{1}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{}\PYGZsh{} Cross\PYGZhy{}validation using cross\PYGZus{}val\PYGZus{}score from sklearn along with KFold} \PYG{c+c1}{\PYGZsh{} kfold is an instance initialized above as:} \PYG{c+c1}{\PYGZsh{} kfold = KFold(n\PYGZus{}splits = k)} \PYG{n}{estimated\PYGZus{}mse\PYGZus{}sklearn} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{nlambdas}\PYG{p}{)} \PYG{n}{i} \PYG{o}{=} \PYG{l+m+mi}{0} \PYG{k}{for} \PYG{n}{lmb} \PYG{o+ow}{in} \PYG{n}{lambdas}\PYG{p}{:} \PYG{n}{ridge} \PYG{o}{=} \PYG{n}{Ridge}\PYG{p}{(}\PYG{n}{alpha} \PYG{o}{=} \PYG{n}{lmb}\PYG{p}{)} \PYG{n}{X} \PYG{o}{=} \PYG{n}{poly}\PYG{o}{.}\PYG{n}{fit\PYGZus{}transform}\PYG{p}{(}\PYG{n}{x}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{)} \PYG{n}{estimated\PYGZus{}mse\PYGZus{}folds} \PYG{o}{=} \PYG{n}{cross\PYGZus{}val\PYGZus{}score}\PYG{p}{(}\PYG{n}{ridge}\PYG{p}{,} \PYG{n}{X}\PYG{p}{,} \PYG{n}{y}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{,} \PYG{n}{scoring}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{neg\PYGZus{}mean\PYGZus{}squared\PYGZus{}error}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{cv}\PYG{o}{=}\PYG{n}{kfold}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} cross\PYGZus{}val\PYGZus{}score return an array containing the estimated negative mse for every fold.} \PYG{c+c1}{\PYGZsh{} we have to the the mean of every array in order to get an estimate of the mse of the model} \PYG{n}{estimated\PYGZus{}mse\PYGZus{}sklearn}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{estimated\PYGZus{}mse\PYGZus{}folds}\PYG{p}{)} \PYG{n}{i} \PYG{o}{+}\PYG{o}{=} \PYG{l+m+mi}{1} \PYG{c+c1}{\PYGZsh{}\PYGZsh{} Plot and compare the slightly different ways to perform cross\PYGZhy{}validation} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{figure}\PYG{p}{(}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{log10}\PYG{p}{(}\PYG{n}{lambdas}\PYG{p}{)}\PYG{p}{,} \PYG{n}{estimated\PYGZus{}mse\PYGZus{}sklearn}\PYG{p}{,} \PYG{n}{label} \PYG{o}{=} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{cross\PYGZus{}val\PYGZus{}score}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{log10}\PYG{p}{(}\PYG{n}{lambdas}\PYG{p}{)}\PYG{p}{,} \PYG{n}{estimated\PYGZus{}mse\PYGZus{}KFold}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{r\PYGZhy{}\PYGZhy{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{label} \PYG{o}{=} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{KFold}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{xlabel}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{log10(lambda)}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{ylabel}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{mse}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{legend}\PYG{p}{(}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \end{sphinxVerbatim} \subsection{The bias\sphinxhyphen{}variance tradeoff} \label{\detokenize{chapter4:the-bias-variance-tradeoff}} We will discuss the bias\sphinxhyphen{}variance tradeoff in the context of continuous predictions such as regression. However, many of the intuitions and ideas discussed here also carry over to classification tasks. Consider a dataset \(\mathcal{L}\) consisting of the data \(\mathbf{X}_\mathcal{L}=\{(y_j, \boldsymbol{x}_j), j=0\ldots n-1\}\). Let us assume that the true data is generated from a noisy model \begin{equation*} \begin{split} \boldsymbol{y}=f(\boldsymbol{x}) + \boldsymbol{\epsilon} \end{split} \end{equation*} where \(\epsilon\) is normally distributed with mean zero and standard deviation \(\sigma^2\). In our derivation of the ordinary least squares method we defined then an approximation to the function \(f\) in terms of the parameters \(\boldsymbol{\beta}\) and the design matrix \(\boldsymbol{X}\) which embody our model, that is \(\boldsymbol{\tilde{y}}=\boldsymbol{X}\boldsymbol{\beta}\). Thereafter we found the parameters \(\boldsymbol{\beta}\) by optimizing the means squared error via the so\sphinxhyphen{}called cost function \begin{equation*} \begin{split} C(\boldsymbol{X},\boldsymbol{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]. \end{split} \end{equation*} We can rewrite this as \begin{equation*} \begin{split} \mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\frac{1}{n}\sum_i(f_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2+\sigma^2. \end{split} \end{equation*} The three terms represent the square of the bias of the learning method, which can be thought of as the error caused by the simplifying assumptions built into the method. The second term represents the variance of the chosen model and finally the last terms is variance of the error \(\boldsymbol{\epsilon}\). To derive this equation, we need to recall that the variance of \(\boldsymbol{y}\) and \(\boldsymbol{\epsilon}\) are both equal to \(\sigma^2\). The mean value of \(\boldsymbol{\epsilon}\) is by definition equal to zero. Furthermore, the function \(f\) is not a stochastics variable, idem for \(\boldsymbol{\tilde{y}}\). We use a more compact notation in terms of the expectation value \begin{equation*} \begin{split} \mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\mathbb{E}\left[(\boldsymbol{f}+\boldsymbol{\epsilon}-\boldsymbol{\tilde{y}})^2\right], \end{split} \end{equation*} and adding and subtracting \(\mathbb{E}\left[\boldsymbol{\tilde{y}}\right]\) we get \begin{equation*} \begin{split} \mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\mathbb{E}\left[(\boldsymbol{f}+\boldsymbol{\epsilon}-\boldsymbol{\tilde{y}}+\mathbb{E}\left[\boldsymbol{\tilde{y}}\right]-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2\right], \end{split} \end{equation*} which, using the abovementioned expectation values can be rewritten as \begin{equation*} \begin{split} \mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\mathbb{E}\left[(\boldsymbol{y}-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2\right]+\mathrm{Var}\left[\boldsymbol{\tilde{y}}\right]+\sigma^2, \end{split} \end{equation*} that is the rewriting in terms of the so\sphinxhyphen{}called bias, the variance of the model \(\boldsymbol{\tilde{y}}\) and the variance of \(\boldsymbol{\epsilon}\). \subsection{Example code for Bias\sphinxhyphen{}Variance tradeoff} \label{\detokenize{chapter4:example-code-for-bias-variance-tradeoff}} \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt} \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n}{LinearRegression}\PYG{p}{,} \PYG{n}{Ridge}\PYG{p}{,} \PYG{n}{Lasso} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{preprocessing} \PYG{k+kn}{import} \PYG{n}{PolynomialFeatures} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{train\PYGZus{}test\PYGZus{}split} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{pipeline} \PYG{k+kn}{import} \PYG{n}{make\PYGZus{}pipeline} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{utils} \PYG{k+kn}{import} \PYG{n}{resample} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{seed}\PYG{p}{(}\PYG{l+m+mi}{2018}\PYG{p}{)} \PYG{n}{n} \PYG{o}{=} \PYG{l+m+mi}{500} \PYG{n}{n\PYGZus{}boostraps} \PYG{o}{=} \PYG{l+m+mi}{100} \PYG{n}{degree} \PYG{o}{=} \PYG{l+m+mi}{18} \PYG{c+c1}{\PYGZsh{} A quite high value, just to show.} \PYG{n}{noise} \PYG{o}{=} \PYG{l+m+mf}{0.1} \PYG{c+c1}{\PYGZsh{} Make data set.} \PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{linspace}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{n}{n}\PYG{p}{)}\PYG{o}{.}\PYG{n}{reshape}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)} \PYG{n}{y} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{exp}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} \PYG{o}{+} \PYG{l+m+mf}{1.5} \PYG{o}{*} \PYG{n}{np}\PYG{o}{.}\PYG{n}{exp}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{p}{(}\PYG{n}{x}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} \PYG{o}{+} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{normal}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mf}{0.1}\PYG{p}{,} \PYG{n}{x}\PYG{o}{.}\PYG{n}{shape}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Hold out some test data that is never used in training.} \PYG{n}{x\PYGZus{}train}\PYG{p}{,} \PYG{n}{x\PYGZus{}test}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}test} \PYG{o}{=} \PYG{n}{train\PYGZus{}test\PYGZus{}split}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{test\PYGZus{}size}\PYG{o}{=}\PYG{l+m+mf}{0.2}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Combine x transformation and model into one operation.} \PYG{c+c1}{\PYGZsh{} Not neccesary, but convenient.} \PYG{n}{model} \PYG{o}{=} \PYG{n}{make\PYGZus{}pipeline}\PYG{p}{(}\PYG{n}{PolynomialFeatures}\PYG{p}{(}\PYG{n}{degree}\PYG{o}{=}\PYG{n}{degree}\PYG{p}{)}\PYG{p}{,} \PYG{n}{LinearRegression}\PYG{p}{(}\PYG{n}{fit\PYGZus{}intercept}\PYG{o}{=}\PYG{k+kc}{False}\PYG{p}{)}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} The following (m x n\PYGZus{}bootstraps) matrix holds the column vectors y\PYGZus{}pred} \PYG{c+c1}{\PYGZsh{} for each bootstrap iteration.} \PYG{n}{y\PYGZus{}pred} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{empty}\PYG{p}{(}\PYG{p}{(}\PYG{n}{y\PYGZus{}test}\PYG{o}{.}\PYG{n}{shape}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{,} \PYG{n}{n\PYGZus{}boostraps}\PYG{p}{)}\PYG{p}{)} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{n\PYGZus{}boostraps}\PYG{p}{)}\PYG{p}{:} \PYG{n}{x\PYGZus{}}\PYG{p}{,} \PYG{n}{y\PYGZus{}} \PYG{o}{=} \PYG{n}{resample}\PYG{p}{(}\PYG{n}{x\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Evaluate the new model on the same test data each time.} \PYG{n}{y\PYGZus{}pred}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{n}{model}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{x\PYGZus{}}\PYG{p}{,} \PYG{n}{y\PYGZus{}}\PYG{p}{)}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{x\PYGZus{}test}\PYG{p}{)}\PYG{o}{.}\PYG{n}{ravel}\PYG{p}{(}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Note: Expectations and variances taken w.r.t. different training} \PYG{c+c1}{\PYGZsh{} data sets, hence the axis=1. Subsequent means are taken across the test data} \PYG{c+c1}{\PYGZsh{} set in order to obtain a total value, but before this we have error/bias/variance} \PYG{c+c1}{\PYGZsh{} calculated per data point in the test set.} \PYG{c+c1}{\PYGZsh{} Note 2: The use of keepdims=True is important in the calculation of bias as this } \PYG{c+c1}{\PYGZsh{} maintains the column vector form. Dropping this yields very unexpected results.} \PYG{n}{error} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{p}{(}\PYG{n}{y\PYGZus{}test} \PYG{o}{\PYGZhy{}} \PYG{n}{y\PYGZus{}pred}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{axis}\PYG{o}{=}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{keepdims}\PYG{o}{=}\PYG{k+kc}{True}\PYG{p}{)} \PYG{p}{)} \PYG{n}{bias} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(} \PYG{p}{(}\PYG{n}{y\PYGZus{}test} \PYG{o}{\PYGZhy{}} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{y\PYGZus{}pred}\PYG{p}{,} \PYG{n}{axis}\PYG{o}{=}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{keepdims}\PYG{o}{=}\PYG{k+kc}{True}\PYG{p}{)}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{p}{)} \PYG{n}{variance} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(} \PYG{n}{np}\PYG{o}{.}\PYG{n}{var}\PYG{p}{(}\PYG{n}{y\PYGZus{}pred}\PYG{p}{,} \PYG{n}{axis}\PYG{o}{=}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{keepdims}\PYG{o}{=}\PYG{k+kc}{True}\PYG{p}{)} \PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Error:}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{error}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Bias\PYGZca{}2:}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{bias}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Var:}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{variance}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+si}{\PYGZob{}\PYGZcb{}}\PYG{l+s+s1}{ \PYGZgt{}= }\PYG{l+s+si}{\PYGZob{}\PYGZcb{}}\PYG{l+s+s1}{ + }\PYG{l+s+si}{\PYGZob{}\PYGZcb{}}\PYG{l+s+s1}{ = }\PYG{l+s+si}{\PYGZob{}\PYGZcb{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{o}{.}\PYG{n}{format}\PYG{p}{(}\PYG{n}{error}\PYG{p}{,} \PYG{n}{bias}\PYG{p}{,} \PYG{n}{variance}\PYG{p}{,} \PYG{n}{bias}\PYG{o}{+}\PYG{n}{variance}\PYG{p}{)}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{x}\PYG{p}{[}\PYG{p}{:}\PYG{p}{:}\PYG{l+m+mi}{5}\PYG{p}{,} \PYG{p}{:}\PYG{p}{]}\PYG{p}{,} \PYG{n}{y}\PYG{p}{[}\PYG{p}{:}\PYG{p}{:}\PYG{l+m+mi}{5}\PYG{p}{,} \PYG{p}{:}\PYG{p}{]}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{f(x)}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{scatter}\PYG{p}{(}\PYG{n}{x\PYGZus{}test}\PYG{p}{,} \PYG{n}{y\PYGZus{}test}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Data points}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{scatter}\PYG{p}{(}\PYG{n}{x\PYGZus{}test}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{y\PYGZus{}pred}\PYG{p}{,} \PYG{n}{axis}\PYG{o}{=}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Pred}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{legend}\PYG{p}{(}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \end{sphinxVerbatim} \subsection{Understanding what happens} \label{\detokenize{chapter4:understanding-what-happens}} \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt} \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n}{LinearRegression}\PYG{p}{,} \PYG{n}{Ridge}\PYG{p}{,} \PYG{n}{Lasso} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{preprocessing} \PYG{k+kn}{import} \PYG{n}{PolynomialFeatures} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{train\PYGZus{}test\PYGZus{}split} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{pipeline} \PYG{k+kn}{import} \PYG{n}{make\PYGZus{}pipeline} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{utils} \PYG{k+kn}{import} \PYG{n}{resample} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{seed}\PYG{p}{(}\PYG{l+m+mi}{2018}\PYG{p}{)} \PYG{n}{n} \PYG{o}{=} \PYG{l+m+mi}{400} \PYG{n}{n\PYGZus{}boostraps} \PYG{o}{=} \PYG{l+m+mi}{100} \PYG{n}{maxdegree} \PYG{o}{=} \PYG{l+m+mi}{30} \PYG{c+c1}{\PYGZsh{} Make data set.} \PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{linspace}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{n}{n}\PYG{p}{)}\PYG{o}{.}\PYG{n}{reshape}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)} \PYG{n}{y} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{exp}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} \PYG{o}{+} \PYG{l+m+mf}{1.5} \PYG{o}{*} \PYG{n}{np}\PYG{o}{.}\PYG{n}{exp}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{p}{(}\PYG{n}{x}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{+} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{normal}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mf}{0.1}\PYG{p}{,} \PYG{n}{x}\PYG{o}{.}\PYG{n}{shape}\PYG{p}{)} \PYG{n}{error} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{maxdegree}\PYG{p}{)} \PYG{n}{bias} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{maxdegree}\PYG{p}{)} \PYG{n}{variance} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{maxdegree}\PYG{p}{)} \PYG{n}{polydegree} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{maxdegree}\PYG{p}{)} \PYG{n}{x\PYGZus{}train}\PYG{p}{,} \PYG{n}{x\PYGZus{}test}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}test} \PYG{o}{=} \PYG{n}{train\PYGZus{}test\PYGZus{}split}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{test\PYGZus{}size}\PYG{o}{=}\PYG{l+m+mf}{0.2}\PYG{p}{)} \PYG{k}{for} \PYG{n}{degree} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{maxdegree}\PYG{p}{)}\PYG{p}{:} \PYG{n}{model} \PYG{o}{=} \PYG{n}{make\PYGZus{}pipeline}\PYG{p}{(}\PYG{n}{PolynomialFeatures}\PYG{p}{(}\PYG{n}{degree}\PYG{o}{=}\PYG{n}{degree}\PYG{p}{)}\PYG{p}{,} \PYG{n}{LinearRegression}\PYG{p}{(}\PYG{n}{fit\PYGZus{}intercept}\PYG{o}{=}\PYG{k+kc}{False}\PYG{p}{)}\PYG{p}{)} \PYG{n}{y\PYGZus{}pred} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{empty}\PYG{p}{(}\PYG{p}{(}\PYG{n}{y\PYGZus{}test}\PYG{o}{.}\PYG{n}{shape}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{,} \PYG{n}{n\PYGZus{}boostraps}\PYG{p}{)}\PYG{p}{)} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{n\PYGZus{}boostraps}\PYG{p}{)}\PYG{p}{:} \PYG{n}{x\PYGZus{}}\PYG{p}{,} \PYG{n}{y\PYGZus{}} \PYG{o}{=} \PYG{n}{resample}\PYG{p}{(}\PYG{n}{x\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{)} \PYG{n}{y\PYGZus{}pred}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{n}{model}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{x\PYGZus{}}\PYG{p}{,} \PYG{n}{y\PYGZus{}}\PYG{p}{)}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{x\PYGZus{}test}\PYG{p}{)}\PYG{o}{.}\PYG{n}{ravel}\PYG{p}{(}\PYG{p}{)} \PYG{n}{polydegree}\PYG{p}{[}\PYG{n}{degree}\PYG{p}{]} \PYG{o}{=} \PYG{n}{degree} \PYG{n}{error}\PYG{p}{[}\PYG{n}{degree}\PYG{p}{]} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{p}{(}\PYG{n}{y\PYGZus{}test} \PYG{o}{\PYGZhy{}} \PYG{n}{y\PYGZus{}pred}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{axis}\PYG{o}{=}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{keepdims}\PYG{o}{=}\PYG{k+kc}{True}\PYG{p}{)} \PYG{p}{)} \PYG{n}{bias}\PYG{p}{[}\PYG{n}{degree}\PYG{p}{]} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(} \PYG{p}{(}\PYG{n}{y\PYGZus{}test} \PYG{o}{\PYGZhy{}} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{n}{y\PYGZus{}pred}\PYG{p}{,} \PYG{n}{axis}\PYG{o}{=}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{keepdims}\PYG{o}{=}\PYG{k+kc}{True}\PYG{p}{)}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{p}{)} \PYG{n}{variance}\PYG{p}{[}\PYG{n}{degree}\PYG{p}{]} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(} \PYG{n}{np}\PYG{o}{.}\PYG{n}{var}\PYG{p}{(}\PYG{n}{y\PYGZus{}pred}\PYG{p}{,} \PYG{n}{axis}\PYG{o}{=}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{keepdims}\PYG{o}{=}\PYG{k+kc}{True}\PYG{p}{)} \PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Polynomial degree:}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{degree}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Error:}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{error}\PYG{p}{[}\PYG{n}{degree}\PYG{p}{]}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Bias\PYGZca{}2:}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{bias}\PYG{p}{[}\PYG{n}{degree}\PYG{p}{]}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Var:}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{variance}\PYG{p}{[}\PYG{n}{degree}\PYG{p}{]}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+si}{\PYGZob{}\PYGZcb{}}\PYG{l+s+s1}{ \PYGZgt{}= }\PYG{l+s+si}{\PYGZob{}\PYGZcb{}}\PYG{l+s+s1}{ + }\PYG{l+s+si}{\PYGZob{}\PYGZcb{}}\PYG{l+s+s1}{ = }\PYG{l+s+si}{\PYGZob{}\PYGZcb{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{o}{.}\PYG{n}{format}\PYG{p}{(}\PYG{n}{error}\PYG{p}{[}\PYG{n}{degree}\PYG{p}{]}\PYG{p}{,} \PYG{n}{bias}\PYG{p}{[}\PYG{n}{degree}\PYG{p}{]}\PYG{p}{,} \PYG{n}{variance}\PYG{p}{[}\PYG{n}{degree}\PYG{p}{]}\PYG{p}{,} \PYG{n}{bias}\PYG{p}{[}\PYG{n}{degree}\PYG{p}{]}\PYG{o}{+}\PYG{n}{variance}\PYG{p}{[}\PYG{n}{degree}\PYG{p}{]}\PYG{p}{)}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{polydegree}\PYG{p}{,} \PYG{n}{error}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Error}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{polydegree}\PYG{p}{,} \PYG{n}{bias}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{bias}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{polydegree}\PYG{p}{,} \PYG{n}{variance}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Variance}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{legend}\PYG{p}{(}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] Polynomial degree: 0 Error: 0.2937910450030775 Bias\PYGZca{}2: 0.2929212799917661 Var: 0.0008697650113114119 0.2937910450030775 \PYGZgt{}= 0.2929212799917661 + 0.0008697650113114119 = 0.2937910450030775 Polynomial degree: 1 Error: 0.06894146856540674 Bias\PYGZca{}2: 0.06832043024896824 Var: 0.0006210383164384989 0.06894146856540674 \PYGZgt{}= 0.06832043024896824 + 0.0006210383164384989 = 0.06894146856540674 Polynomial degree: 2 Error: 0.06106765054837855 Bias\PYGZca{}2: 0.060547654220995305 Var: 0.0005199963273832372 0.06106765054837855 \PYGZgt{}= 0.060547654220995305 + 0.0005199963273832372 = 0.061067650548378545 Polynomial degree: 3 Error: 0.03346202229536659 Bias\PYGZca{}2: 0.0331409564680546 Var: 0.00032106582731199456 0.03346202229536659 \PYGZgt{}= 0.0331409564680546 + 0.00032106582731199456 = 0.03346202229536659 Polynomial degree: 4 Error: 0.0335277871704832 Bias\PYGZca{}2: 0.03311607538577367 Var: 0.0004117117847095335 0.0335277871704832 \PYGZgt{}= 0.03311607538577367 + 0.0004117117847095335 = 0.03352778717048321 Polynomial degree: 5 Error: 0.025517151530854786 Bias\PYGZca{}2: 0.024968890209256463 Var: 0.0005482613215983259 0.025517151530854786 \PYGZgt{}= 0.024968890209256463 + 0.0005482613215983259 = 0.02551715153085479 Polynomial degree: 6 Error: 0.01994607606842793 Bias\PYGZca{}2: 0.019502076889868637 Var: 0.00044399917855929527 0.01994607606842793 \PYGZgt{}= 0.019502076889868637 + 0.00044399917855929527 = 0.019946076068427934 Polynomial degree: 7 Error: 0.018695928655417676 Bias\PYGZca{}2: 0.01797984009000237 Var: 0.0007160885654153078 0.018695928655417676 \PYGZgt{}= 0.01797984009000237 + 0.0007160885654153078 = 0.01869592865541768 Polynomial degree: 8 Error: 0.010736105188369479 Bias\PYGZca{}2: 0.010376602508045063 Var: 0.00035950268032441344 0.010736105188369479 \PYGZgt{}= 0.010376602508045063 + 0.00035950268032441344 = 0.010736105188369477 Polynomial degree: 9 Error: 0.01101329065273084 Bias\PYGZca{}2: 0.010539027867197629 Var: 0.0004742627855332104 0.01101329065273084 \PYGZgt{}= 0.010539027867197629 + 0.0004742627855332104 = 0.01101329065273084 Polynomial degree: 10 Error: 0.010972468815261078 Bias\PYGZca{}2: 0.010593565969983903 Var: 0.00037890284527716995 0.010972468815261078 \PYGZgt{}= 0.010593565969983903 + 0.00037890284527716995 = 0.010972468815261073 Polynomial degree: 11 Error: 0.01084055593776807 Bias\PYGZca{}2: 0.010348475861989281 Var: 0.0004920800757787882 0.01084055593776807 \PYGZgt{}= 0.010348475861989281 + 0.0004920800757787882 = 0.01084055593776807 Polynomial degree: 12 Error: 0.010192472149429362 Bias\PYGZca{}2: 0.009610568640072627 Var: 0.0005819035093567355 0.010192472149429362 \PYGZgt{}= 0.009610568640072627 + 0.0005819035093567355 = 0.010192472149429362 Polynomial degree: 13 Error: 0.010312285920590011 Bias\PYGZca{}2: 0.009802534263801815 Var: 0.0005097516567881938 0.010312285920590011 \PYGZgt{}= 0.009802534263801815 + 0.0005097516567881938 = 0.01031228592059001 Polynomial degree: 14 Error: 0.010722455299595876 Bias\PYGZca{}2: 0.01008891676024437 Var: 0.0006335385393515036 0.010722455299595876 \PYGZgt{}= 0.01008891676024437 + 0.0006335385393515036 = 0.010722455299595875 Polynomial degree: 15 Error: 0.011155437503231998 Bias\PYGZca{}2: 0.010311761228670724 Var: 0.0008436762745612778 0.011155437503231998 \PYGZgt{}= 0.010311761228670724 + 0.0008436762745612778 = 0.011155437503232002 Polynomial degree: 16 Error: 0.011028026782676708 Bias\PYGZca{}2: 0.010223572382311492 Var: 0.0008044544003652116 0.011028026782676708 \PYGZgt{}= 0.010223572382311492 + 0.0008044544003652116 = 0.011028026782676703 Polynomial degree: 17 Error: 0.011628743129658555 Bias\PYGZca{}2: 0.010533948734129592 Var: 0.001094794395528961 0.011628743129658555 \PYGZgt{}= 0.010533948734129592 + 0.001094794395528961 = 0.011628743129658553 Polynomial degree: 18 Error: 0.014371682171531027 Bias\PYGZca{}2: 0.010922362242870073 Var: 0.0034493199286609573 0.014371682171531027 \PYGZgt{}= 0.010922362242870073 + 0.0034493199286609573 = 0.01437168217153103 Polynomial degree: 19 Error: 0.026986306199342624 Bias\PYGZca{}2: 0.01214176442858653 Var: 0.014844541770756087 0.026986306199342624 \PYGZgt{}= 0.01214176442858653 + 0.014844541770756087 = 0.026986306199342617 Polynomial degree: 20 Error: 0.012249244024160728 Bias\PYGZca{}2: 0.01006785246285396 Var: 0.002181391561306766 0.012249244024160728 \PYGZgt{}= 0.01006785246285396 + 0.002181391561306766 = 0.012249244024160727 Polynomial degree: 21 Error: 0.014973172820830053 Bias\PYGZca{}2: 0.010154371176360328 Var: 0.00481880164446972 0.014973172820830053 \PYGZgt{}= 0.010154371176360328 + 0.00481880164446972 = 0.014973172820830048 Polynomial degree: 22 Error: 0.014186606932681737 Bias\PYGZca{}2: 0.009594131981212376 Var: 0.0045924749514693625 0.014186606932681737 \PYGZgt{}= 0.009594131981212376 + 0.0045924749514693625 = 0.014186606932681738 Polynomial degree: 23 Error: 0.025574552577788824 Bias\PYGZca{}2: 0.009477519033249752 Var: 0.016097033544539077 0.025574552577788824 \PYGZgt{}= 0.009477519033249752 + 0.016097033544539077 = 0.02557455257778883 Polynomial degree: 24 Error: 0.03147298632679604 Bias\PYGZca{}2: 0.009565267585507206 Var: 0.021907718741288846 0.03147298632679604 \PYGZgt{}= 0.009565267585507206 + 0.021907718741288846 = 0.03147298632679605 Polynomial degree: 25 Error: 0.03929027799369515 Bias\PYGZca{}2: 0.009776269005896726 Var: 0.029514008987798424 0.03929027799369515 \PYGZgt{}= 0.009776269005896726 + 0.029514008987798424 = 0.03929027799369515 Polynomial degree: 26 Error: 0.15813256009613183 Bias\PYGZca{}2: 0.013239726753028333 Var: 0.14489283334310352 0.15813256009613183 \PYGZgt{}= 0.013239726753028333 + 0.14489283334310352 = 0.15813256009613186 Polynomial degree: 27 Error: 0.1360840943498259 Bias\PYGZca{}2: 0.01326608592145169 Var: 0.12281800842837416 0.1360840943498259 \PYGZgt{}= 0.01326608592145169 + 0.12281800842837416 = 0.13608409434982585 Polynomial degree: 28 Error: 0.7210723692205014 Bias\PYGZca{}2: 0.04436186918146108 Var: 0.6767105000390408 0.7210723692205014 \PYGZgt{}= 0.04436186918146108 + 0.6767105000390408 = 0.7210723692205019 Polynomial degree: 29 Error: 0.48454430745837984 Bias\PYGZca{}2: 0.011809368338879722 Var: 0.4727349391195001 0.48454430745837984 \PYGZgt{}= 0.011809368338879722 + 0.4727349391195001 = 0.48454430745837984 \end{sphinxVerbatim} \noindent\sphinxincludegraphics{{chapter4_300_1}.png} \subsection{Summing up} \label{\detokenize{chapter4:summing-up}} The bias\sphinxhyphen{}variance tradeoff summarizes the fundamental tension in machine learning, particularly supervised learning, between the complexity of a model and the amount of training data needed to train it. Since data is often limited, in practice it is often useful to use a less\sphinxhyphen{}complex model with higher bias, that is a model whose asymptotic performance is worse than another model because it is easier to train and less sensitive to sampling noise arising from having a finite\sphinxhyphen{}sized training dataset (smaller variance). The above equations tell us that in order to minimize the expected test error, we need to select a statistical learning method that simultaneously achieves low variance and low bias. Note that variance is inherently a nonnegative quantity, and squared bias is also nonnegative. Hence, we see that the expected test MSE can never lie below \(Var(\epsilon)\), the irreducible error. What do we mean by the variance and bias of a statistical learning method? The variance refers to the amount by which our model would change if we estimated it using a different training data set. Since the training data are used to fit the statistical learning method, different training data sets will result in a different estimate. But ideally the estimate for our model should not vary too much between training sets. However, if a method has high variance then small changes in the training data can result in large changes in the model. In general, more flexible statistical methods have higher variance. You may also find this recent \sphinxhref{https://www.pnas.org/content/116/32/15849}{article} of interest. \subsection{Another Example from Scikit\sphinxhyphen{}Learn’s Repository} \label{\detokenize{chapter4:another-example-from-scikit-learn-s-repository}} \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{l+s+sd}{\PYGZdq{}\PYGZdq{}\PYGZdq{}} \PYG{l+s+sd}{============================} \PYG{l+s+sd}{Underfitting vs. Overfitting} \PYG{l+s+sd}{============================} \PYG{l+s+sd}{This example demonstrates the problems of underfitting and overfitting and} \PYG{l+s+sd}{how we can use linear regression with polynomial features to approximate} \PYG{l+s+sd}{nonlinear functions. The plot shows the function that we want to approximate,} \PYG{l+s+sd}{which is a part of the cosine function. In addition, the samples from the} \PYG{l+s+sd}{real function and the approximations of different models are displayed. The} \PYG{l+s+sd}{models have polynomial features of different degrees. We can see that a} \PYG{l+s+sd}{linear function (polynomial with degree 1) is not sufficient to fit the} \PYG{l+s+sd}{training samples. This is called **underfitting**. A polynomial of degree 4} \PYG{l+s+sd}{approximates the true function almost perfectly. However, for higher degrees} \PYG{l+s+sd}{the model will **overfit** the training data, i.e. it learns the noise of the} \PYG{l+s+sd}{training data.} \PYG{l+s+sd}{We evaluate quantitatively **overfitting** / **underfitting** by using} \PYG{l+s+sd}{cross\PYGZhy{}validation. We calculate the mean squared error (MSE) on the validation} \PYG{l+s+sd}{set, the higher, the less likely the model generalizes correctly from the} \PYG{l+s+sd}{training data.} \PYG{l+s+sd}{\PYGZdq{}\PYGZdq{}\PYGZdq{}} \PYG{n+nb}{print}\PYG{p}{(}\PYG{n+nv+vm}{\PYGZus{}\PYGZus{}doc\PYGZus{}\PYGZus{}}\PYG{p}{)} \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{pipeline} \PYG{k+kn}{import} \PYG{n}{Pipeline} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{preprocessing} \PYG{k+kn}{import} \PYG{n}{PolynomialFeatures} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n}{LinearRegression} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{cross\PYGZus{}val\PYGZus{}score} \PYG{k}{def} \PYG{n+nf}{true\PYGZus{}fun}\PYG{p}{(}\PYG{n}{X}\PYG{p}{)}\PYG{p}{:} \PYG{k}{return} \PYG{n}{np}\PYG{o}{.}\PYG{n}{cos}\PYG{p}{(}\PYG{l+m+mf}{1.5} \PYG{o}{*} \PYG{n}{np}\PYG{o}{.}\PYG{n}{pi} \PYG{o}{*} \PYG{n}{X}\PYG{p}{)} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{seed}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{)} \PYG{n}{n\PYGZus{}samples} \PYG{o}{=} \PYG{l+m+mi}{30} \PYG{n}{degrees} \PYG{o}{=} \PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{15}\PYG{p}{]} \PYG{n}{X} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{sort}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{rand}\PYG{p}{(}\PYG{n}{n\PYGZus{}samples}\PYG{p}{)}\PYG{p}{)} \PYG{n}{y} \PYG{o}{=} \PYG{n}{true\PYGZus{}fun}\PYG{p}{(}\PYG{n}{X}\PYG{p}{)} \PYG{o}{+} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{randn}\PYG{p}{(}\PYG{n}{n\PYGZus{}samples}\PYG{p}{)} \PYG{o}{*} \PYG{l+m+mf}{0.1} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{figure}\PYG{p}{(}\PYG{n}{figsize}\PYG{o}{=}\PYG{p}{(}\PYG{l+m+mi}{14}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{)}\PYG{p}{)} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{degrees}\PYG{p}{)}\PYG{p}{)}\PYG{p}{:} \PYG{n}{ax} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{subplot}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{degrees}\PYG{p}{)}\PYG{p}{,} \PYG{n}{i} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{setp}\PYG{p}{(}\PYG{n}{ax}\PYG{p}{,} \PYG{n}{xticks}\PYG{o}{=}\PYG{p}{(}\PYG{p}{)}\PYG{p}{,} \PYG{n}{yticks}\PYG{o}{=}\PYG{p}{(}\PYG{p}{)}\PYG{p}{)} \PYG{n}{polynomial\PYGZus{}features} \PYG{o}{=} \PYG{n}{PolynomialFeatures}\PYG{p}{(}\PYG{n}{degree}\PYG{o}{=}\PYG{n}{degrees}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{,} \PYG{n}{include\PYGZus{}bias}\PYG{o}{=}\PYG{k+kc}{False}\PYG{p}{)} \PYG{n}{linear\PYGZus{}regression} \PYG{o}{=} \PYG{n}{LinearRegression}\PYG{p}{(}\PYG{p}{)} \PYG{n}{pipeline} \PYG{o}{=} \PYG{n}{Pipeline}\PYG{p}{(}\PYG{p}{[}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{polynomial\PYGZus{}features}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{polynomial\PYGZus{}features}\PYG{p}{)}\PYG{p}{,} \PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{linear\PYGZus{}regression}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{linear\PYGZus{}regression}\PYG{p}{)}\PYG{p}{]}\PYG{p}{)} \PYG{n}{pipeline}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Evaluate the models using crossvalidation} \PYG{n}{scores} \PYG{o}{=} \PYG{n}{cross\PYGZus{}val\PYGZus{}score}\PYG{p}{(}\PYG{n}{pipeline}\PYG{p}{,} \PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{scoring}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{neg\PYGZus{}mean\PYGZus{}squared\PYGZus{}error}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{cv}\PYG{o}{=}\PYG{l+m+mi}{10}\PYG{p}{)} \PYG{n}{X\PYGZus{}test} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{linspace}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{100}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{X\PYGZus{}test}\PYG{p}{,} \PYG{n}{pipeline}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{X\PYGZus{}test}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{)}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Model}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{X\PYGZus{}test}\PYG{p}{,} \PYG{n}{true\PYGZus{}fun}\PYG{p}{(}\PYG{n}{X\PYGZus{}test}\PYG{p}{)}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{True function}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{scatter}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{edgecolor}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{b}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{s}\PYG{o}{=}\PYG{l+m+mi}{20}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Samples}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{xlabel}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{x}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{ylabel}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{y}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{xlim}\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{ylim}\PYG{p}{(}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{legend}\PYG{p}{(}\PYG{n}{loc}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{best}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{title}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Degree }\PYG{l+s+si}{\PYGZob{}\PYGZcb{}}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+s2}{MSE = }\PYG{l+s+si}{\PYGZob{}:.2e\PYGZcb{}}\PYG{l+s+s2}{(+/\PYGZhy{} }\PYG{l+s+si}{\PYGZob{}:.2e\PYGZcb{}}\PYG{l+s+s2}{)}\PYG{l+s+s2}{\PYGZdq{}}\PYG{o}{.}\PYG{n}{format}\PYG{p}{(} \PYG{n}{degrees}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{,} \PYG{o}{\PYGZhy{}}\PYG{n}{scores}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{p}{)}\PYG{p}{,} \PYG{n}{scores}\PYG{o}{.}\PYG{n}{std}\PYG{p}{(}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \end{sphinxVerbatim} \subsection{More examples on bootstrap and cross\sphinxhyphen{}validation and errors} \label{\detokenize{chapter4:more-examples-on-bootstrap-and-cross-validation-and-errors}} \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{c+c1}{\PYGZsh{} Common imports} \PYG{k+kn}{import} \PYG{n+nn}{os} \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{k+kn}{import} \PYG{n+nn}{pandas} \PYG{k}{as} \PYG{n+nn}{pd} \PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n}{LinearRegression}\PYG{p}{,} \PYG{n}{Ridge}\PYG{p}{,} \PYG{n}{Lasso} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{train\PYGZus{}test\PYGZus{}split} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{utils} \PYG{k+kn}{import} \PYG{n}{resample} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{metrics} \PYG{k+kn}{import} \PYG{n}{mean\PYGZus{}squared\PYGZus{}error} \PYG{c+c1}{\PYGZsh{} Where to save the figures and data files} \PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Results}\PYG{l+s+s2}{\PYGZdq{}} \PYG{n}{FIGURE\PYGZus{}ID} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Results/FigureFiles}\PYG{l+s+s2}{\PYGZdq{}} \PYG{n}{DATA\PYGZus{}ID} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{DataFiles/}\PYG{l+s+s2}{\PYGZdq{}} \PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR}\PYG{p}{)}\PYG{p}{:} \PYG{n}{os}\PYG{o}{.}\PYG{n}{mkdir}\PYG{p}{(}\PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR}\PYG{p}{)} \PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{)}\PYG{p}{:} \PYG{n}{os}\PYG{o}{.}\PYG{n}{makedirs}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{)} \PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{)}\PYG{p}{:} \PYG{n}{os}\PYG{o}{.}\PYG{n}{makedirs}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{)} \PYG{k}{def} \PYG{n+nf}{image\PYGZus{}path}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)}\PYG{p}{:} \PYG{k}{return} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{join}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{,} \PYG{n}{fig\PYGZus{}id}\PYG{p}{)} \PYG{k}{def} \PYG{n+nf}{data\PYGZus{}path}\PYG{p}{(}\PYG{n}{dat\PYGZus{}id}\PYG{p}{)}\PYG{p}{:} \PYG{k}{return} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{join}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{,} \PYG{n}{dat\PYGZus{}id}\PYG{p}{)} \PYG{k}{def} \PYG{n+nf}{save\PYGZus{}fig}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)}\PYG{p}{:} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{savefig}\PYG{p}{(}\PYG{n}{image\PYGZus{}path}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)} \PYG{o}{+} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{.png}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n+nb}{format}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{png}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{infile} \PYG{o}{=} \PYG{n+nb}{open}\PYG{p}{(}\PYG{n}{data\PYGZus{}path}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{EoS.csv}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}\PYG{p}{,}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Read the EoS data as csv file and organize the data into two arrays with density and energies} \PYG{n}{EoS} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{read\PYGZus{}csv}\PYG{p}{(}\PYG{n}{infile}\PYG{p}{,} \PYG{n}{names}\PYG{o}{=}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Density}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Energy}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{)} \PYG{n}{EoS}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Energy}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{to\PYGZus{}numeric}\PYG{p}{(}\PYG{n}{EoS}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Energy}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{,} \PYG{n}{errors}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{coerce}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{EoS} \PYG{o}{=} \PYG{n}{EoS}\PYG{o}{.}\PYG{n}{dropna}\PYG{p}{(}\PYG{p}{)} \PYG{n}{Energies} \PYG{o}{=} \PYG{n}{EoS}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Energy}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{n}{Density} \PYG{o}{=} \PYG{n}{EoS}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Density}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{c+c1}{\PYGZsh{} The design matrix now as function of various polytrops} \PYG{n}{Maxpolydegree} \PYG{o}{=} \PYG{l+m+mi}{30} \PYG{n}{X} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{Density}\PYG{p}{)}\PYG{p}{,}\PYG{n}{Maxpolydegree}\PYG{p}{)}\PYG{p}{)} \PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{]} \PYG{o}{=} \PYG{l+m+mf}{1.0} \PYG{n}{testerror} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{Maxpolydegree}\PYG{p}{)} \PYG{n}{trainingerror} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{Maxpolydegree}\PYG{p}{)} \PYG{n}{polynomial} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{Maxpolydegree}\PYG{p}{)} \PYG{n}{trials} \PYG{o}{=} \PYG{l+m+mi}{100} \PYG{k}{for} \PYG{n}{polydegree} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{Maxpolydegree}\PYG{p}{)}\PYG{p}{:} \PYG{n}{polynomial}\PYG{p}{[}\PYG{n}{polydegree}\PYG{p}{]} \PYG{o}{=} \PYG{n}{polydegree} \PYG{k}{for} \PYG{n}{degree} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{polydegree}\PYG{p}{)}\PYG{p}{:} \PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{n}{degree}\PYG{p}{]} \PYG{o}{=} \PYG{n}{Density}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{n}{degree}\PYG{o}{/}\PYG{l+m+mf}{3.0}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} loop over trials in order to estimate the expectation value of the MSE} \PYG{n}{testerror}\PYG{p}{[}\PYG{n}{polydegree}\PYG{p}{]} \PYG{o}{=} \PYG{l+m+mf}{0.0} \PYG{n}{trainingerror}\PYG{p}{[}\PYG{n}{polydegree}\PYG{p}{]} \PYG{o}{=} \PYG{l+m+mf}{0.0} \PYG{k}{for} \PYG{n}{samples} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{trials}\PYG{p}{)}\PYG{p}{:} \PYG{n}{x\PYGZus{}train}\PYG{p}{,} \PYG{n}{x\PYGZus{}test}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}test} \PYG{o}{=} \PYG{n}{train\PYGZus{}test\PYGZus{}split}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{Energies}\PYG{p}{,} \PYG{n}{test\PYGZus{}size}\PYG{o}{=}\PYG{l+m+mf}{0.2}\PYG{p}{)} \PYG{n}{model} \PYG{o}{=} \PYG{n}{LinearRegression}\PYG{p}{(}\PYG{n}{fit\PYGZus{}intercept}\PYG{o}{=}\PYG{k+kc}{True}\PYG{p}{)}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{x\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{)} \PYG{n}{ypred} \PYG{o}{=} \PYG{n}{model}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{x\PYGZus{}train}\PYG{p}{)} \PYG{n}{ytilde} \PYG{o}{=} \PYG{n}{model}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{x\PYGZus{}test}\PYG{p}{)} \PYG{n}{testerror}\PYG{p}{[}\PYG{n}{polydegree}\PYG{p}{]} \PYG{o}{+}\PYG{o}{=} \PYG{n}{mean\PYGZus{}squared\PYGZus{}error}\PYG{p}{(}\PYG{n}{y\PYGZus{}test}\PYG{p}{,} \PYG{n}{ytilde}\PYG{p}{)} \PYG{n}{trainingerror}\PYG{p}{[}\PYG{n}{polydegree}\PYG{p}{]} \PYG{o}{+}\PYG{o}{=} \PYG{n}{mean\PYGZus{}squared\PYGZus{}error}\PYG{p}{(}\PYG{n}{y\PYGZus{}train}\PYG{p}{,} \PYG{n}{ypred}\PYG{p}{)} \PYG{n}{testerror}\PYG{p}{[}\PYG{n}{polydegree}\PYG{p}{]} \PYG{o}{/}\PYG{o}{=} \PYG{n}{trials} \PYG{n}{trainingerror}\PYG{p}{[}\PYG{n}{polydegree}\PYG{p}{]} \PYG{o}{/}\PYG{o}{=} \PYG{n}{trials} \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Degree of polynomial: }\PYG{l+s+si}{\PYGZpc{}3d}\PYG{l+s+s2}{\PYGZdq{}}\PYG{o}{\PYGZpc{}} \PYG{n}{polynomial}\PYG{p}{[}\PYG{n}{polydegree}\PYG{p}{]}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Mean squared error on training data: }\PYG{l+s+si}{\PYGZpc{}.8f}\PYG{l+s+s2}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{trainingerror}\PYG{p}{[}\PYG{n}{polydegree}\PYG{p}{]}\PYG{p}{)} \PYG{n+nb}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Mean squared error on test data: }\PYG{l+s+si}{\PYGZpc{}.8f}\PYG{l+s+s2}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{testerror}\PYG{p}{[}\PYG{n}{polydegree}\PYG{p}{]}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{polynomial}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{log10}\PYG{p}{(}\PYG{n}{trainingerror}\PYG{p}{)}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Training Error}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{polynomial}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{log10}\PYG{p}{(}\PYG{n}{testerror}\PYG{p}{)}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Test Error}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{xlabel}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Polynomial degree}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{ylabel}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{log10[MSE]}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{legend}\PYG{p}{(}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \end{sphinxVerbatim} \subsection{The same example but now with cross\sphinxhyphen{}validation} \label{\detokenize{chapter4:the-same-example-but-now-with-cross-validation}} \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{c+c1}{\PYGZsh{} Common imports} \PYG{k+kn}{import} \PYG{n+nn}{os} \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{k+kn}{import} \PYG{n+nn}{pandas} \PYG{k}{as} \PYG{n+nn}{pd} \PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n}{LinearRegression}\PYG{p}{,} \PYG{n}{Ridge}\PYG{p}{,} \PYG{n}{Lasso} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{metrics} \PYG{k+kn}{import} \PYG{n}{mean\PYGZus{}squared\PYGZus{}error} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{KFold} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{cross\PYGZus{}val\PYGZus{}score} \PYG{c+c1}{\PYGZsh{} Where to save the figures and data files} \PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Results}\PYG{l+s+s2}{\PYGZdq{}} \PYG{n}{FIGURE\PYGZus{}ID} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Results/FigureFiles}\PYG{l+s+s2}{\PYGZdq{}} \PYG{n}{DATA\PYGZus{}ID} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{DataFiles/}\PYG{l+s+s2}{\PYGZdq{}} \PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR}\PYG{p}{)}\PYG{p}{:} \PYG{n}{os}\PYG{o}{.}\PYG{n}{mkdir}\PYG{p}{(}\PYG{n}{PROJECT\PYGZus{}ROOT\PYGZus{}DIR}\PYG{p}{)} \PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{)}\PYG{p}{:} \PYG{n}{os}\PYG{o}{.}\PYG{n}{makedirs}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{)} \PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{exists}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{)}\PYG{p}{:} \PYG{n}{os}\PYG{o}{.}\PYG{n}{makedirs}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{)} \PYG{k}{def} \PYG{n+nf}{image\PYGZus{}path}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)}\PYG{p}{:} \PYG{k}{return} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{join}\PYG{p}{(}\PYG{n}{FIGURE\PYGZus{}ID}\PYG{p}{,} \PYG{n}{fig\PYGZus{}id}\PYG{p}{)} \PYG{k}{def} \PYG{n+nf}{data\PYGZus{}path}\PYG{p}{(}\PYG{n}{dat\PYGZus{}id}\PYG{p}{)}\PYG{p}{:} \PYG{k}{return} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{join}\PYG{p}{(}\PYG{n}{DATA\PYGZus{}ID}\PYG{p}{,} \PYG{n}{dat\PYGZus{}id}\PYG{p}{)} \PYG{k}{def} \PYG{n+nf}{save\PYGZus{}fig}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)}\PYG{p}{:} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{savefig}\PYG{p}{(}\PYG{n}{image\PYGZus{}path}\PYG{p}{(}\PYG{n}{fig\PYGZus{}id}\PYG{p}{)} \PYG{o}{+} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{.png}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n+nb}{format}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{png}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{infile} \PYG{o}{=} \PYG{n+nb}{open}\PYG{p}{(}\PYG{n}{data\PYGZus{}path}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{EoS.csv}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)}\PYG{p}{,}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{r}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Read the EoS data as csv file and organize the data into two arrays with density and energies} \PYG{n}{EoS} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{read\PYGZus{}csv}\PYG{p}{(}\PYG{n}{infile}\PYG{p}{,} \PYG{n}{names}\PYG{o}{=}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Density}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Energy}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{)} \PYG{n}{EoS}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Energy}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{to\PYGZus{}numeric}\PYG{p}{(}\PYG{n}{EoS}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Energy}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{,} \PYG{n}{errors}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{coerce}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{EoS} \PYG{o}{=} \PYG{n}{EoS}\PYG{o}{.}\PYG{n}{dropna}\PYG{p}{(}\PYG{p}{)} \PYG{n}{Energies} \PYG{o}{=} \PYG{n}{EoS}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Energy}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{n}{Density} \PYG{o}{=} \PYG{n}{EoS}\PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Density}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{c+c1}{\PYGZsh{} The design matrix now as function of various polytrops} \PYG{n}{Maxpolydegree} \PYG{o}{=} \PYG{l+m+mi}{30} \PYG{n}{X} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{Density}\PYG{p}{)}\PYG{p}{,}\PYG{n}{Maxpolydegree}\PYG{p}{)}\PYG{p}{)} \PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{]} \PYG{o}{=} \PYG{l+m+mf}{1.0} \PYG{n}{estimated\PYGZus{}mse\PYGZus{}sklearn} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{Maxpolydegree}\PYG{p}{)} \PYG{n}{polynomial} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{Maxpolydegree}\PYG{p}{)} \PYG{n}{k} \PYG{o}{=}\PYG{l+m+mi}{5} \PYG{n}{kfold} \PYG{o}{=} \PYG{n}{KFold}\PYG{p}{(}\PYG{n}{n\PYGZus{}splits} \PYG{o}{=} \PYG{n}{k}\PYG{p}{)} \PYG{k}{for} \PYG{n}{polydegree} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{Maxpolydegree}\PYG{p}{)}\PYG{p}{:} \PYG{n}{polynomial}\PYG{p}{[}\PYG{n}{polydegree}\PYG{p}{]} \PYG{o}{=} \PYG{n}{polydegree} \PYG{k}{for} \PYG{n}{degree} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{polydegree}\PYG{p}{)}\PYG{p}{:} \PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,}\PYG{n}{degree}\PYG{p}{]} \PYG{o}{=} \PYG{n}{Density}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{n}{degree}\PYG{o}{/}\PYG{l+m+mf}{3.0}\PYG{p}{)} \PYG{n}{OLS} \PYG{o}{=} \PYG{n}{LinearRegression}\PYG{p}{(}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} loop over trials in order to estimate the expectation value of the MSE} \PYG{n}{estimated\PYGZus{}mse\PYGZus{}folds} \PYG{o}{=} \PYG{n}{cross\PYGZus{}val\PYGZus{}score}\PYG{p}{(}\PYG{n}{OLS}\PYG{p}{,} \PYG{n}{X}\PYG{p}{,} \PYG{n}{Energies}\PYG{p}{,} \PYG{n}{scoring}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{neg\PYGZus{}mean\PYGZus{}squared\PYGZus{}error}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{cv}\PYG{o}{=}\PYG{n}{kfold}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{}[:, np.newaxis]} \PYG{n}{estimated\PYGZus{}mse\PYGZus{}sklearn}\PYG{p}{[}\PYG{n}{polydegree}\PYG{p}{]} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{estimated\PYGZus{}mse\PYGZus{}folds}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{polynomial}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{log10}\PYG{p}{(}\PYG{n}{estimated\PYGZus{}mse\PYGZus{}sklearn}\PYG{p}{)}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Test Error}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{xlabel}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Polynomial degree}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{ylabel}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{log10[MSE]}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{legend}\PYG{p}{(}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \end{sphinxVerbatim} \subsection{Cross\sphinxhyphen{}validation with Ridge} \label{\detokenize{chapter4:cross-validation-with-ridge}} \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{KFold} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n}{Ridge} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{cross\PYGZus{}val\PYGZus{}score} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{preprocessing} \PYG{k+kn}{import} \PYG{n}{PolynomialFeatures} \PYG{c+c1}{\PYGZsh{} A seed just to ensure that the random numbers are the same for every run.} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{seed}\PYG{p}{(}\PYG{l+m+mi}{3155}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Generate the data.} \PYG{n}{n} \PYG{o}{=} \PYG{l+m+mi}{100} \PYG{n}{x} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{linspace}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{n}{n}\PYG{p}{)}\PYG{o}{.}\PYG{n}{reshape}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)} \PYG{n}{y} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{exp}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} \PYG{o}{+} \PYG{l+m+mf}{1.5} \PYG{o}{*} \PYG{n}{np}\PYG{o}{.}\PYG{n}{exp}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{p}{(}\PYG{n}{x}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{+} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{normal}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mf}{0.1}\PYG{p}{,} \PYG{n}{x}\PYG{o}{.}\PYG{n}{shape}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Decide degree on polynomial to fit} \PYG{n}{poly} \PYG{o}{=} \PYG{n}{PolynomialFeatures}\PYG{p}{(}\PYG{n}{degree} \PYG{o}{=} \PYG{l+m+mi}{10}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Decide which values of lambda to use} \PYG{n}{nlambdas} \PYG{o}{=} \PYG{l+m+mi}{500} \PYG{n}{lambdas} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{logspace}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{,} \PYG{n}{nlambdas}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Initialize a KFold instance} \PYG{n}{k} \PYG{o}{=} \PYG{l+m+mi}{5} \PYG{n}{kfold} \PYG{o}{=} \PYG{n}{KFold}\PYG{p}{(}\PYG{n}{n\PYGZus{}splits} \PYG{o}{=} \PYG{n}{k}\PYG{p}{)} \PYG{n}{estimated\PYGZus{}mse\PYGZus{}sklearn} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{nlambdas}\PYG{p}{)} \PYG{n}{i} \PYG{o}{=} \PYG{l+m+mi}{0} \PYG{k}{for} \PYG{n}{lmb} \PYG{o+ow}{in} \PYG{n}{lambdas}\PYG{p}{:} \PYG{n}{ridge} \PYG{o}{=} \PYG{n}{Ridge}\PYG{p}{(}\PYG{n}{alpha} \PYG{o}{=} \PYG{n}{lmb}\PYG{p}{)} \PYG{n}{estimated\PYGZus{}mse\PYGZus{}folds} \PYG{o}{=} \PYG{n}{cross\PYGZus{}val\PYGZus{}score}\PYG{p}{(}\PYG{n}{ridge}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{scoring}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{neg\PYGZus{}mean\PYGZus{}squared\PYGZus{}error}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{cv}\PYG{o}{=}\PYG{n}{kfold}\PYG{p}{)} \PYG{n}{estimated\PYGZus{}mse\PYGZus{}sklearn}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{mean}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{estimated\PYGZus{}mse\PYGZus{}folds}\PYG{p}{)} \PYG{n}{i} \PYG{o}{+}\PYG{o}{=} \PYG{l+m+mi}{1} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{figure}\PYG{p}{(}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{log10}\PYG{p}{(}\PYG{n}{lambdas}\PYG{p}{)}\PYG{p}{,} \PYG{n}{estimated\PYGZus{}mse\PYGZus{}sklearn}\PYG{p}{,} \PYG{n}{label} \PYG{o}{=} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{cross\PYGZus{}val\PYGZus{}score}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{xlabel}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{log10(lambda)}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{ylabel}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{MSE}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{legend}\PYG{p}{(}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \end{sphinxVerbatim} \subsection{The Ising model} \label{\detokenize{chapter4:the-ising-model}} The one\sphinxhyphen{}dimensional Ising model with nearest neighbor interaction, no external field and a constant coupling constant \(J\) is given by \begin{equation*} \begin{split} \begin{equation} H = -J \sum_{k}^L s_k s_{k + 1}, \label{_auto2} \tag{2} \end{equation} \end{split} \end{equation*} where \(s_i \in \{-1, 1\}\) and \(s_{N + 1} = s_1\). The number of spins in the system is determined by \(L\). For the one\sphinxhyphen{}dimensional system there is no phase transition. We will look at a system of \(L = 40\) spins with a coupling constant of \(J = 1\). To get enough training data we will generate 10000 states with their respective energies. \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt} \PYG{k+kn}{from} \PYG{n+nn}{mpl\PYGZus{}toolkits}\PYG{n+nn}{.}\PYG{n+nn}{axes\PYGZus{}grid1} \PYG{k+kn}{import} \PYG{n}{make\PYGZus{}axes\PYGZus{}locatable} \PYG{k+kn}{import} \PYG{n+nn}{seaborn} \PYG{k}{as} \PYG{n+nn}{sns} \PYG{k+kn}{import} \PYG{n+nn}{scipy}\PYG{n+nn}{.}\PYG{n+nn}{linalg} \PYG{k}{as} \PYG{n+nn}{scl} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{train\PYGZus{}test\PYGZus{}split} \PYG{k+kn}{import} \PYG{n+nn}{tqdm} \PYG{n}{sns}\PYG{o}{.}\PYG{n}{set}\PYG{p}{(}\PYG{n}{color\PYGZus{}codes}\PYG{o}{=}\PYG{k+kc}{True}\PYG{p}{)} \PYG{n}{cmap\PYGZus{}args}\PYG{o}{=}\PYG{n+nb}{dict}\PYG{p}{(}\PYG{n}{vmin}\PYG{o}{=}\PYG{o}{\PYGZhy{}}\PYG{l+m+mf}{1.}\PYG{p}{,} \PYG{n}{vmax}\PYG{o}{=}\PYG{l+m+mf}{1.}\PYG{p}{,} \PYG{n}{cmap}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{seismic}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{L} \PYG{o}{=} \PYG{l+m+mi}{40} \PYG{n}{n} \PYG{o}{=} \PYG{n+nb}{int}\PYG{p}{(}\PYG{l+m+mf}{1e4}\PYG{p}{)} \PYG{n}{spins} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{choice}\PYG{p}{(}\PYG{p}{[}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{,} \PYG{n}{size}\PYG{o}{=}\PYG{p}{(}\PYG{n}{n}\PYG{p}{,} \PYG{n}{L}\PYG{p}{)}\PYG{p}{)} \PYG{n}{J} \PYG{o}{=} \PYG{l+m+mf}{1.0} \PYG{n}{energies} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)}\PYG{p}{:} \PYG{n}{energies}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{o}{\PYGZhy{}} \PYG{n}{J} \PYG{o}{*} \PYG{n}{np}\PYG{o}{.}\PYG{n}{dot}\PYG{p}{(}\PYG{n}{spins}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{roll}\PYG{p}{(}\PYG{n}{spins}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{)} \end{sphinxVerbatim} Here we use ordinary least squares regression to predict the energy for the nearest neighbor one\sphinxhyphen{}dimensional Ising model on a ring, i.e., the endpoints wrap around. We will use linear regression to fit a value for the coupling constant to achieve this. \subsection{Reformulating the problem to suit regression} \label{\detokenize{chapter4:reformulating-the-problem-to-suit-regression}} A more general form for the one\sphinxhyphen{}dimensional Ising model is \begin{equation*} \begin{split} \begin{equation} H = - \sum_j^L \sum_k^L s_j s_k J_{jk}. \label{_auto3} \tag{3} \end{equation} \end{split} \end{equation*} Here we allow for interactions beyond the nearest neighbors and a state dependent coupling constant. This latter expression can be formulated as a matrix\sphinxhyphen{}product \begin{equation*} \begin{split} \begin{equation} \boldsymbol{H} = \boldsymbol{X} J, \label{_auto4} \tag{4} \end{equation} \end{split} \end{equation*} where \(X_{jk} = s_j s_k\) and \(J\) is a matrix which consists of the elements \(-J_{jk}\). This form of writing the energy fits perfectly with the form utilized in linear regression, that is \begin{equation*} \begin{split} \begin{equation} \boldsymbol{y} = \boldsymbol{X}\boldsymbol{\beta} + \boldsymbol{\epsilon}, \label{_auto5} \tag{5} \end{equation} \end{split} \end{equation*} We split the data in training and test data as discussed in the previous example \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{n}{X} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{p}{(}\PYG{n}{n}\PYG{p}{,} \PYG{n}{L} \PYG{o}{*}\PYG{o}{*} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)}\PYG{p}{:} \PYG{n}{X}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{outer}\PYG{p}{(}\PYG{n}{spins}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{,} \PYG{n}{spins}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{)}\PYG{o}{.}\PYG{n}{ravel}\PYG{p}{(}\PYG{p}{)} \PYG{n}{y} \PYG{o}{=} \PYG{n}{energies} \PYG{n}{X\PYGZus{}train}\PYG{p}{,} \PYG{n}{X\PYGZus{}test}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}test} \PYG{o}{=} \PYG{n}{train\PYGZus{}test\PYGZus{}split}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{test\PYGZus{}size}\PYG{o}{=}\PYG{l+m+mf}{0.2}\PYG{p}{)} \end{sphinxVerbatim} \subsection{Linear regression} \label{\detokenize{chapter4:linear-regression}} In the ordinary least squares method we choose the cost function \begin{equation*} \begin{split} \begin{equation} C(\boldsymbol{X}, \boldsymbol{\beta})= \frac{1}{n}\left\{(\boldsymbol{X}\boldsymbol{\beta} - \boldsymbol{y})^T(\boldsymbol{X}\boldsymbol{\beta} - \boldsymbol{y})\right\}. \label{_auto6} \tag{6} \end{equation} \end{split} \end{equation*} We then find the extremal point of \(C\) by taking the derivative with respect to \(\boldsymbol{\beta}\) as discussed above. This yields the expression for \(\boldsymbol{\beta}\) to be \begin{equation*} \begin{split} \boldsymbol{\beta} = \frac{\boldsymbol{X}^T \boldsymbol{y}}{\boldsymbol{X}^T \boldsymbol{X}}, \end{split} \end{equation*} which immediately imposes some requirements on \(\boldsymbol{X}\) as there must exist an inverse of \(\boldsymbol{X}^T \boldsymbol{X}\). If the expression we are modeling contains an intercept, i.e., a constant term, we must make sure that the first column of \(\boldsymbol{X}\) consists of \(1\). We do this here \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{n}{X\PYGZus{}train\PYGZus{}own} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{concatenate}\PYG{p}{(} \PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{ones}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{p}{)}\PYG{p}{)}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{,} \PYG{n}{X\PYGZus{}train}\PYG{p}{)}\PYG{p}{,} \PYG{n}{axis}\PYG{o}{=}\PYG{l+m+mi}{1} \PYG{p}{)} \PYG{n}{X\PYGZus{}test\PYGZus{}own} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{concatenate}\PYG{p}{(} \PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{ones}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{X\PYGZus{}test}\PYG{p}{)}\PYG{p}{)}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{,} \PYG{n}{X\PYGZus{}test}\PYG{p}{)}\PYG{p}{,} \PYG{n}{axis}\PYG{o}{=}\PYG{l+m+mi}{1} \PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k}{def} \PYG{n+nf}{ols\PYGZus{}inv}\PYG{p}{(}\PYG{n}{x}\PYG{p}{:} \PYG{n}{np}\PYG{o}{.}\PYG{n}{ndarray}\PYG{p}{,} \PYG{n}{y}\PYG{p}{:} \PYG{n}{np}\PYG{o}{.}\PYG{n}{ndarray}\PYG{p}{)} \PYG{o}{\PYGZhy{}}\PYG{o}{\PYGZgt{}} \PYG{n}{np}\PYG{o}{.}\PYG{n}{ndarray}\PYG{p}{:} \PYG{k}{return} \PYG{n}{scl}\PYG{o}{.}\PYG{n}{inv}\PYG{p}{(}\PYG{n}{x}\PYG{o}{.}\PYG{n}{T} \PYG{o}{@} \PYG{n}{x}\PYG{p}{)} \PYG{o}{@} \PYG{p}{(}\PYG{n}{x}\PYG{o}{.}\PYG{n}{T} \PYG{o}{@} \PYG{n}{y}\PYG{p}{)} \PYG{n}{beta} \PYG{o}{=} \PYG{n}{ols\PYGZus{}inv}\PYG{p}{(}\PYG{n}{X\PYGZus{}train\PYGZus{}own}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{)} \end{sphinxVerbatim} \subsection{Singular Value decomposition} \label{\detokenize{chapter4:singular-value-decomposition}} Doing the inversion directly turns out to be a bad idea since the matrix \(\boldsymbol{X}^T\boldsymbol{X}\) is singular. An alternative approach is to use the \sphinxstylestrong{singular value decomposition}. Using the definition of the Moore\sphinxhyphen{}Penrose pseudoinverse we can write the equation for \(\boldsymbol{\beta}\) as \begin{equation*} \begin{split} \boldsymbol{\beta} = \boldsymbol{X}^{+}\boldsymbol{y}, \end{split} \end{equation*} where the pseudoinverse of \(\boldsymbol{X}\) is given by \begin{equation*} \begin{split} \boldsymbol{X}^{+} = \frac{\boldsymbol{X}^T}{\boldsymbol{X}^T\boldsymbol{X}}. \end{split} \end{equation*} Using singular value decomposition we can decompose the matrix \(\boldsymbol{X} = \boldsymbol{U}\boldsymbol{\Sigma} \boldsymbol{V}^T\), where \(\boldsymbol{U}\) and \(\boldsymbol{V}\) are orthogonal(unitary) matrices and \(\boldsymbol{\Sigma}\) contains the singular values (more details below). where \(X^{+} = V\Sigma^{+} U^T\). This reduces the equation for \(\omega\) to \begin{equation*} \begin{split} \begin{equation} \boldsymbol{\beta} = \boldsymbol{V}\boldsymbol{\Sigma}^{+} \boldsymbol{U}^T \boldsymbol{y}. \label{_auto7} \tag{7} \end{equation} \end{split} \end{equation*} Note that solving this equation by actually doing the pseudoinverse (which is what we will do) is not a good idea as this operation scales as \(\mathcal{O}(n^3)\), where \(n\) is the number of elements in a general matrix. Instead, doing \(QR\)\sphinxhyphen{}factorization and solving the linear system as an equation would reduce this down to \(\mathcal{O}(n^2)\) operations. \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k}{def} \PYG{n+nf}{ols\PYGZus{}svd}\PYG{p}{(}\PYG{n}{x}\PYG{p}{:} \PYG{n}{np}\PYG{o}{.}\PYG{n}{ndarray}\PYG{p}{,} \PYG{n}{y}\PYG{p}{:} \PYG{n}{np}\PYG{o}{.}\PYG{n}{ndarray}\PYG{p}{)} \PYG{o}{\PYGZhy{}}\PYG{o}{\PYGZgt{}} \PYG{n}{np}\PYG{o}{.}\PYG{n}{ndarray}\PYG{p}{:} \PYG{n}{u}\PYG{p}{,} \PYG{n}{s}\PYG{p}{,} \PYG{n}{v} \PYG{o}{=} \PYG{n}{scl}\PYG{o}{.}\PYG{n}{svd}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{k}{return} \PYG{n}{v}\PYG{o}{.}\PYG{n}{T} \PYG{o}{@} \PYG{n}{scl}\PYG{o}{.}\PYG{n}{pinv}\PYG{p}{(}\PYG{n}{scl}\PYG{o}{.}\PYG{n}{diagsvd}\PYG{p}{(}\PYG{n}{s}\PYG{p}{,} \PYG{n}{u}\PYG{o}{.}\PYG{n}{shape}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{,} \PYG{n}{v}\PYG{o}{.}\PYG{n}{shape}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)}\PYG{p}{)} \PYG{o}{@} \PYG{n}{u}\PYG{o}{.}\PYG{n}{T} \PYG{o}{@} \PYG{n}{y} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{n}{beta} \PYG{o}{=} \PYG{n}{ols\PYGZus{}svd}\PYG{p}{(}\PYG{n}{X\PYGZus{}train\PYGZus{}own}\PYG{p}{,}\PYG{n}{y\PYGZus{}train}\PYG{p}{)} \end{sphinxVerbatim} When extracting the \(J\)\sphinxhyphen{}matrix we need to make sure that we remove the intercept, as is done here \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{n}{J} \PYG{o}{=} \PYG{n}{beta}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{:}\PYG{p}{]}\PYG{o}{.}\PYG{n}{reshape}\PYG{p}{(}\PYG{n}{L}\PYG{p}{,} \PYG{n}{L}\PYG{p}{)} \end{sphinxVerbatim} A way of looking at the coefficients in \(J\) is to plot the matrices as images. \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{n}{fig} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{figure}\PYG{p}{(}\PYG{n}{figsize}\PYG{o}{=}\PYG{p}{(}\PYG{l+m+mi}{20}\PYG{p}{,} \PYG{l+m+mi}{14}\PYG{p}{)}\PYG{p}{)} \PYG{n}{im} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{imshow}\PYG{p}{(}\PYG{n}{J}\PYG{p}{,} \PYG{o}{*}\PYG{o}{*}\PYG{n}{cmap\PYGZus{}args}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{title}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{OLS}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{xticks}\PYG{p}{(}\PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{yticks}\PYG{p}{(}\PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)} \PYG{n}{cb} \PYG{o}{=} \PYG{n}{fig}\PYG{o}{.}\PYG{n}{colorbar}\PYG{p}{(}\PYG{n}{im}\PYG{p}{)} \PYG{n}{cb}\PYG{o}{.}\PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}yticklabels}\PYG{p}{(}\PYG{n}{cb}\PYG{o}{.}\PYG{n}{ax}\PYG{o}{.}\PYG{n}{get\PYGZus{}yticklabels}\PYG{p}{(}\PYG{p}{)}\PYG{p}{,} \PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \end{sphinxVerbatim} It is interesting to note that OLS considers both \(J_{j, j + 1} = -0.5\) and \(J_{j, j - 1} = -0.5\) as valid matrix elements for \(J\). In our discussion below on hyperparameters and Ridge and Lasso regression we will see that this problem can be removed, partly and only with Lasso regression. In this case our matrix inversion was actually possible. The obvious question now is what is the mathematics behind the SVD? \subsection{The one\sphinxhyphen{}dimensional Ising model} \label{\detokenize{chapter4:the-one-dimensional-ising-model}} Let us bring back the Ising model again, but now with an additional focus on Ridge and Lasso regression as well. We repeat some of the basic parts of the Ising model and the setup of the training and test data. The one\sphinxhyphen{}dimensional Ising model with nearest neighbor interaction, no external field and a constant coupling constant \(J\) is given by \begin{equation*} \begin{split} \begin{equation} H = -J \sum_{k}^L s_k s_{k + 1}, \label{_auto8} \tag{8} \end{equation} \end{split} \end{equation*} where \(s_i \in \{-1, 1\}\) and \(s_{N + 1} = s_1\). The number of spins in the system is determined by \(L\). For the one\sphinxhyphen{}dimensional system there is no phase transition. We will look at a system of \(L = 40\) spins with a coupling constant of \(J = 1\). To get enough training data we will generate 10000 states with their respective energies. \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt} \PYG{k+kn}{from} \PYG{n+nn}{mpl\PYGZus{}toolkits}\PYG{n+nn}{.}\PYG{n+nn}{axes\PYGZus{}grid1} \PYG{k+kn}{import} \PYG{n}{make\PYGZus{}axes\PYGZus{}locatable} \PYG{k+kn}{import} \PYG{n+nn}{seaborn} \PYG{k}{as} \PYG{n+nn}{sns} \PYG{k+kn}{import} \PYG{n+nn}{scipy}\PYG{n+nn}{.}\PYG{n+nn}{linalg} \PYG{k}{as} \PYG{n+nn}{scl} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{train\PYGZus{}test\PYGZus{}split} \PYG{k+kn}{import} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k}{as} \PYG{n+nn}{skl} \PYG{k+kn}{import} \PYG{n+nn}{tqdm} \PYG{n}{sns}\PYG{o}{.}\PYG{n}{set}\PYG{p}{(}\PYG{n}{color\PYGZus{}codes}\PYG{o}{=}\PYG{k+kc}{True}\PYG{p}{)} \PYG{n}{cmap\PYGZus{}args}\PYG{o}{=}\PYG{n+nb}{dict}\PYG{p}{(}\PYG{n}{vmin}\PYG{o}{=}\PYG{o}{\PYGZhy{}}\PYG{l+m+mf}{1.}\PYG{p}{,} \PYG{n}{vmax}\PYG{o}{=}\PYG{l+m+mf}{1.}\PYG{p}{,} \PYG{n}{cmap}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{seismic}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{L} \PYG{o}{=} \PYG{l+m+mi}{40} \PYG{n}{n} \PYG{o}{=} \PYG{n+nb}{int}\PYG{p}{(}\PYG{l+m+mf}{1e4}\PYG{p}{)} \PYG{n}{spins} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{choice}\PYG{p}{(}\PYG{p}{[}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{,} \PYG{n}{size}\PYG{o}{=}\PYG{p}{(}\PYG{n}{n}\PYG{p}{,} \PYG{n}{L}\PYG{p}{)}\PYG{p}{)} \PYG{n}{J} \PYG{o}{=} \PYG{l+m+mf}{1.0} \PYG{n}{energies} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)}\PYG{p}{:} \PYG{n}{energies}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{o}{\PYGZhy{}} \PYG{n}{J} \PYG{o}{*} \PYG{n}{np}\PYG{o}{.}\PYG{n}{dot}\PYG{p}{(}\PYG{n}{spins}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{roll}\PYG{p}{(}\PYG{n}{spins}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{)} \end{sphinxVerbatim} A more general form for the one\sphinxhyphen{}dimensional Ising model is \begin{equation*} \begin{split} \begin{equation} H = - \sum_j^L \sum_k^L s_j s_k J_{jk}. \label{_auto9} \tag{9} \end{equation} \end{split} \end{equation*} Here we allow for interactions beyond the nearest neighbors and a more adaptive coupling matrix. This latter expression can be formulated as a matrix\sphinxhyphen{}product on the form \begin{equation*} \begin{split} \begin{equation} H = X J, \label{_auto10} \tag{10} \end{equation} \end{split} \end{equation*} where \(X_{jk} = s_j s_k\) and \(J\) is the matrix consisting of the elements \(-J_{jk}\). This form of writing the energy fits perfectly with the form utilized in linear regression, viz. \begin{equation*} \begin{split} \begin{equation} \boldsymbol{y} = \boldsymbol{X}\boldsymbol{\beta} + \boldsymbol{\epsilon}. \label{_auto11} \tag{11} \end{equation} \end{split} \end{equation*} We organize the data as we did above \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{n}{X} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{p}{(}\PYG{n}{n}\PYG{p}{,} \PYG{n}{L} \PYG{o}{*}\PYG{o}{*} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)}\PYG{p}{:} \PYG{n}{X}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{outer}\PYG{p}{(}\PYG{n}{spins}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{,} \PYG{n}{spins}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{)}\PYG{o}{.}\PYG{n}{ravel}\PYG{p}{(}\PYG{p}{)} \PYG{n}{y} \PYG{o}{=} \PYG{n}{energies} \PYG{n}{X\PYGZus{}train}\PYG{p}{,} \PYG{n}{X\PYGZus{}test}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}test} \PYG{o}{=} \PYG{n}{train\PYGZus{}test\PYGZus{}split}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{test\PYGZus{}size}\PYG{o}{=}\PYG{l+m+mf}{0.96}\PYG{p}{)} \PYG{n}{X\PYGZus{}train\PYGZus{}own} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{concatenate}\PYG{p}{(} \PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{ones}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{p}{)}\PYG{p}{)}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{,} \PYG{n}{X\PYGZus{}train}\PYG{p}{)}\PYG{p}{,} \PYG{n}{axis}\PYG{o}{=}\PYG{l+m+mi}{1} \PYG{p}{)} \PYG{n}{X\PYGZus{}test\PYGZus{}own} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{concatenate}\PYG{p}{(} \PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{ones}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{X\PYGZus{}test}\PYG{p}{)}\PYG{p}{)}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{newaxis}\PYG{p}{]}\PYG{p}{,} \PYG{n}{X\PYGZus{}test}\PYG{p}{)}\PYG{p}{,} \PYG{n}{axis}\PYG{o}{=}\PYG{l+m+mi}{1} \PYG{p}{)} \end{sphinxVerbatim} We will do all fitting with \sphinxstylestrong{Scikit\sphinxhyphen{}Learn}, \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{n}{clf} \PYG{o}{=} \PYG{n}{skl}\PYG{o}{.}\PYG{n}{LinearRegression}\PYG{p}{(}\PYG{p}{)}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{)} \end{sphinxVerbatim} When extracting the \(J\)\sphinxhyphen{}matrix we make sure to remove the intercept \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{n}{J\PYGZus{}sk} \PYG{o}{=} \PYG{n}{clf}\PYG{o}{.}\PYG{n}{coef\PYGZus{}}\PYG{o}{.}\PYG{n}{reshape}\PYG{p}{(}\PYG{n}{L}\PYG{p}{,} \PYG{n}{L}\PYG{p}{)} \end{sphinxVerbatim} And then we plot the results \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{n}{fig} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{figure}\PYG{p}{(}\PYG{n}{figsize}\PYG{o}{=}\PYG{p}{(}\PYG{l+m+mi}{20}\PYG{p}{,} \PYG{l+m+mi}{14}\PYG{p}{)}\PYG{p}{)} \PYG{n}{im} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{imshow}\PYG{p}{(}\PYG{n}{J\PYGZus{}sk}\PYG{p}{,} \PYG{o}{*}\PYG{o}{*}\PYG{n}{cmap\PYGZus{}args}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{title}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{LinearRegression from Scikit\PYGZhy{}learn}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{xticks}\PYG{p}{(}\PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{yticks}\PYG{p}{(}\PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)} \PYG{n}{cb} \PYG{o}{=} \PYG{n}{fig}\PYG{o}{.}\PYG{n}{colorbar}\PYG{p}{(}\PYG{n}{im}\PYG{p}{)} \PYG{n}{cb}\PYG{o}{.}\PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}yticklabels}\PYG{p}{(}\PYG{n}{cb}\PYG{o}{.}\PYG{n}{ax}\PYG{o}{.}\PYG{n}{get\PYGZus{}yticklabels}\PYG{p}{(}\PYG{p}{)}\PYG{p}{,} \PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \end{sphinxVerbatim} The results perfectly with our previous discussion where we used our own code. \subsection{Ridge regression} \label{\detokenize{chapter4:ridge-regression}} Having explored the ordinary least squares we move on to ridge regression. In ridge regression we include a \sphinxstylestrong{regularizer}. This involves a new cost function which leads to a new estimate for the weights \(\boldsymbol{\beta}\). This results in a penalized regression problem. The cost function is given by 1 3 6 \textless{} \textless{} \textless{} ! ! M A T H \_ B L O C K \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{n}{\PYGZus{}lambda} \PYG{o}{=} \PYG{l+m+mf}{0.1} \PYG{n}{clf\PYGZus{}ridge} \PYG{o}{=} \PYG{n}{skl}\PYG{o}{.}\PYG{n}{Ridge}\PYG{p}{(}\PYG{n}{alpha}\PYG{o}{=}\PYG{n}{\PYGZus{}lambda}\PYG{p}{)}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{)} \PYG{n}{J\PYGZus{}ridge\PYGZus{}sk} \PYG{o}{=} \PYG{n}{clf\PYGZus{}ridge}\PYG{o}{.}\PYG{n}{coef\PYGZus{}}\PYG{o}{.}\PYG{n}{reshape}\PYG{p}{(}\PYG{n}{L}\PYG{p}{,} \PYG{n}{L}\PYG{p}{)} \PYG{n}{fig} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{figure}\PYG{p}{(}\PYG{n}{figsize}\PYG{o}{=}\PYG{p}{(}\PYG{l+m+mi}{20}\PYG{p}{,} \PYG{l+m+mi}{14}\PYG{p}{)}\PYG{p}{)} \PYG{n}{im} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{imshow}\PYG{p}{(}\PYG{n}{J\PYGZus{}ridge\PYGZus{}sk}\PYG{p}{,} \PYG{o}{*}\PYG{o}{*}\PYG{n}{cmap\PYGZus{}args}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{title}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Ridge from Scikit\PYGZhy{}learn}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{xticks}\PYG{p}{(}\PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{yticks}\PYG{p}{(}\PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)} \PYG{n}{cb} \PYG{o}{=} \PYG{n}{fig}\PYG{o}{.}\PYG{n}{colorbar}\PYG{p}{(}\PYG{n}{im}\PYG{p}{)} \PYG{n}{cb}\PYG{o}{.}\PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}yticklabels}\PYG{p}{(}\PYG{n}{cb}\PYG{o}{.}\PYG{n}{ax}\PYG{o}{.}\PYG{n}{get\PYGZus{}yticklabels}\PYG{p}{(}\PYG{p}{)}\PYG{p}{,} \PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \end{sphinxVerbatim} \subsection{LASSO regression} \label{\detokenize{chapter4:lasso-regression}} In the \sphinxstylestrong{Least Absolute Shrinkage and Selection Operator} (LASSO)\sphinxhyphen{}method we get a third cost function. \begin{equation*} \begin{split} \begin{equation} C(\boldsymbol{X}, \boldsymbol{\beta}; \lambda) = (\boldsymbol{X}\boldsymbol{\beta} - \boldsymbol{y})^T(\boldsymbol{X}\boldsymbol{\beta} - \boldsymbol{y}) + \lambda \sqrt{\boldsymbol{\beta}^T\boldsymbol{\beta}}. \label{_auto13} \tag{13} \end{equation} \end{split} \end{equation*} Finding the extremal point of this cost function is not so straight\sphinxhyphen{}forward as in least squares and ridge. We will therefore rely solely on the function \sphinxcode{\sphinxupquote{Lasso}} from \sphinxstylestrong{Scikit\sphinxhyphen{}Learn}. \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{n}{clf\PYGZus{}lasso} \PYG{o}{=} \PYG{n}{skl}\PYG{o}{.}\PYG{n}{Lasso}\PYG{p}{(}\PYG{n}{alpha}\PYG{o}{=}\PYG{n}{\PYGZus{}lambda}\PYG{p}{)}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{)} \PYG{n}{J\PYGZus{}lasso\PYGZus{}sk} \PYG{o}{=} \PYG{n}{clf\PYGZus{}lasso}\PYG{o}{.}\PYG{n}{coef\PYGZus{}}\PYG{o}{.}\PYG{n}{reshape}\PYG{p}{(}\PYG{n}{L}\PYG{p}{,} \PYG{n}{L}\PYG{p}{)} \PYG{n}{fig} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{figure}\PYG{p}{(}\PYG{n}{figsize}\PYG{o}{=}\PYG{p}{(}\PYG{l+m+mi}{20}\PYG{p}{,} \PYG{l+m+mi}{14}\PYG{p}{)}\PYG{p}{)} \PYG{n}{im} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{imshow}\PYG{p}{(}\PYG{n}{J\PYGZus{}lasso\PYGZus{}sk}\PYG{p}{,} \PYG{o}{*}\PYG{o}{*}\PYG{n}{cmap\PYGZus{}args}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{title}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Lasso from Scikit\PYGZhy{}learn}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{xticks}\PYG{p}{(}\PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{yticks}\PYG{p}{(}\PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)} \PYG{n}{cb} \PYG{o}{=} \PYG{n}{fig}\PYG{o}{.}\PYG{n}{colorbar}\PYG{p}{(}\PYG{n}{im}\PYG{p}{)} \PYG{n}{cb}\PYG{o}{.}\PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}yticklabels}\PYG{p}{(}\PYG{n}{cb}\PYG{o}{.}\PYG{n}{ax}\PYG{o}{.}\PYG{n}{get\PYGZus{}yticklabels}\PYG{p}{(}\PYG{p}{)}\PYG{p}{,} \PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \end{sphinxVerbatim} It is quite striking how LASSO breaks the symmetry of the coupling constant as opposed to ridge and OLS. We get a sparse solution with \(J_{j, j + 1} = -1\). \subsection{Performance as function of the regularization parameter} \label{\detokenize{chapter4:performance-as-function-of-the-regularization-parameter}} We see how the different models perform for a different set of values for \(\lambda\). \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{n}{lambdas} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{logspace}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{,} \PYG{l+m+mi}{10}\PYG{p}{)} \PYG{n}{train\PYGZus{}errors} \PYG{o}{=} \PYG{p}{\PYGZob{}} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{ols\PYGZus{}sk}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{lambdas}\PYG{o}{.}\PYG{n}{size}\PYG{p}{)}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{ridge\PYGZus{}sk}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{lambdas}\PYG{o}{.}\PYG{n}{size}\PYG{p}{)}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{lasso\PYGZus{}sk}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{lambdas}\PYG{o}{.}\PYG{n}{size}\PYG{p}{)} \PYG{p}{\PYGZcb{}} \PYG{n}{test\PYGZus{}errors} \PYG{o}{=} \PYG{p}{\PYGZob{}} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{ols\PYGZus{}sk}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{lambdas}\PYG{o}{.}\PYG{n}{size}\PYG{p}{)}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{ridge\PYGZus{}sk}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{lambdas}\PYG{o}{.}\PYG{n}{size}\PYG{p}{)}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{lasso\PYGZus{}sk}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:} \PYG{n}{np}\PYG{o}{.}\PYG{n}{zeros}\PYG{p}{(}\PYG{n}{lambdas}\PYG{o}{.}\PYG{n}{size}\PYG{p}{)} \PYG{p}{\PYGZcb{}} \PYG{n}{plot\PYGZus{}counter} \PYG{o}{=} \PYG{l+m+mi}{1} \PYG{n}{fig} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{figure}\PYG{p}{(}\PYG{n}{figsize}\PYG{o}{=}\PYG{p}{(}\PYG{l+m+mi}{32}\PYG{p}{,} \PYG{l+m+mi}{54}\PYG{p}{)}\PYG{p}{)} \PYG{k}{for} \PYG{n}{i}\PYG{p}{,} \PYG{n}{\PYGZus{}lambda} \PYG{o+ow}{in} \PYG{n+nb}{enumerate}\PYG{p}{(}\PYG{n}{tqdm}\PYG{o}{.}\PYG{n}{tqdm}\PYG{p}{(}\PYG{n}{lambdas}\PYG{p}{)}\PYG{p}{)}\PYG{p}{:} \PYG{k}{for} \PYG{n}{key}\PYG{p}{,} \PYG{n}{method} \PYG{o+ow}{in} \PYG{n+nb}{zip}\PYG{p}{(} \PYG{p}{[}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{ols\PYGZus{}sk}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{ridge\PYGZus{}sk}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{lasso\PYGZus{}sk}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{n}{skl}\PYG{o}{.}\PYG{n}{LinearRegression}\PYG{p}{(}\PYG{p}{)}\PYG{p}{,} \PYG{n}{skl}\PYG{o}{.}\PYG{n}{Ridge}\PYG{p}{(}\PYG{n}{alpha}\PYG{o}{=}\PYG{n}{\PYGZus{}lambda}\PYG{p}{)}\PYG{p}{,} \PYG{n}{skl}\PYG{o}{.}\PYG{n}{Lasso}\PYG{p}{(}\PYG{n}{alpha}\PYG{o}{=}\PYG{n}{\PYGZus{}lambda}\PYG{p}{)}\PYG{p}{]} \PYG{p}{)}\PYG{p}{:} \PYG{n}{method} \PYG{o}{=} \PYG{n}{method}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{)} \PYG{n}{train\PYGZus{}errors}\PYG{p}{[}\PYG{n}{key}\PYG{p}{]}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{n}{method}\PYG{o}{.}\PYG{n}{score}\PYG{p}{(}\PYG{n}{X\PYGZus{}train}\PYG{p}{,} \PYG{n}{y\PYGZus{}train}\PYG{p}{)} \PYG{n}{test\PYGZus{}errors}\PYG{p}{[}\PYG{n}{key}\PYG{p}{]}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} \PYG{o}{=} \PYG{n}{method}\PYG{o}{.}\PYG{n}{score}\PYG{p}{(}\PYG{n}{X\PYGZus{}test}\PYG{p}{,} \PYG{n}{y\PYGZus{}test}\PYG{p}{)} \PYG{n}{omega} \PYG{o}{=} \PYG{n}{method}\PYG{o}{.}\PYG{n}{coef\PYGZus{}}\PYG{o}{.}\PYG{n}{reshape}\PYG{p}{(}\PYG{n}{L}\PYG{p}{,} \PYG{n}{L}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{subplot}\PYG{p}{(}\PYG{l+m+mi}{10}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{,} \PYG{n}{plot\PYGZus{}counter}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{imshow}\PYG{p}{(}\PYG{n}{omega}\PYG{p}{,} \PYG{o}{*}\PYG{o}{*}\PYG{n}{cmap\PYGZus{}args}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{title}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+s2}{, \PYGZdl{}}\PYG{l+s+s2}{\PYGZbs{}}\PYG{l+s+s2}{lambda = }\PYG{l+s+si}{\PYGZpc{}.4f}\PYG{l+s+s2}{\PYGZdl{}}\PYG{l+s+s2}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{key}\PYG{p}{,} \PYG{n}{\PYGZus{}lambda}\PYG{p}{)}\PYG{p}{)} \PYG{n}{plot\PYGZus{}counter} \PYG{o}{+}\PYG{o}{=} \PYG{l+m+mi}{1} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \end{sphinxVerbatim} We see that LASSO reaches a good solution for low values of \(\lambda\), but will “wither” when we increase \(\lambda\) too much. Ridge is more stable over a larger range of values for \(\lambda\), but eventually also fades away. \subsection{Finding the optimal value of \protect\(\lambda\protect\)} \label{\detokenize{chapter4:finding-the-optimal-value-of-lambda}} To determine which value of \(\lambda\) is best we plot the accuracy of the models when predicting the training and the testing set. We expect the accuracy of the training set to be quite good, but if the accuracy of the testing set is much lower this tells us that we might be subject to an overfit model. The ideal scenario is an accuracy on the testing set that is close to the accuracy of the training set. \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{n}{fig} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{figure}\PYG{p}{(}\PYG{n}{figsize}\PYG{o}{=}\PYG{p}{(}\PYG{l+m+mi}{20}\PYG{p}{,} \PYG{l+m+mi}{14}\PYG{p}{)}\PYG{p}{)} \PYG{n}{colors} \PYG{o}{=} \PYG{p}{\PYGZob{}} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{ols\PYGZus{}sk}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{r}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{ridge\PYGZus{}sk}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{y}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{lasso\PYGZus{}sk}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{c}\PYG{l+s+s2}{\PYGZdq{}} \PYG{p}{\PYGZcb{}} \PYG{k}{for} \PYG{n}{key} \PYG{o+ow}{in} \PYG{n}{train\PYGZus{}errors}\PYG{p}{:} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{semilogx}\PYG{p}{(} \PYG{n}{lambdas}\PYG{p}{,} \PYG{n}{train\PYGZus{}errors}\PYG{p}{[}\PYG{n}{key}\PYG{p}{]}\PYG{p}{,} \PYG{n}{colors}\PYG{p}{[}\PYG{n}{key}\PYG{p}{]}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Train }\PYG{l+s+si}{\PYGZob{}0\PYGZcb{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{o}{.}\PYG{n}{format}\PYG{p}{(}\PYG{n}{key}\PYG{p}{)}\PYG{p}{,} \PYG{n}{linewidth}\PYG{o}{=}\PYG{l+m+mf}{4.0} \PYG{p}{)} \PYG{k}{for} \PYG{n}{key} \PYG{o+ow}{in} \PYG{n}{test\PYGZus{}errors}\PYG{p}{:} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{semilogx}\PYG{p}{(} \PYG{n}{lambdas}\PYG{p}{,} \PYG{n}{test\PYGZus{}errors}\PYG{p}{[}\PYG{n}{key}\PYG{p}{]}\PYG{p}{,} \PYG{n}{colors}\PYG{p}{[}\PYG{n}{key}\PYG{p}{]} \PYG{o}{+} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{\PYGZhy{}\PYGZhy{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{label}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Test }\PYG{l+s+si}{\PYGZob{}0\PYGZcb{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{o}{.}\PYG{n}{format}\PYG{p}{(}\PYG{n}{key}\PYG{p}{)}\PYG{p}{,} \PYG{n}{linewidth}\PYG{o}{=}\PYG{l+m+mf}{4.0} \PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{legend}\PYG{p}{(}\PYG{n}{loc}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{best}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{xlabel}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{\PYGZdl{}}\PYG{l+s+s2}{\PYGZbs{}}\PYG{l+s+s2}{lambda\PYGZdl{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{ylabel}\PYG{p}{(}\PYG{l+s+sa}{r}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{\PYGZdl{}R\PYGZca{}2\PYGZdl{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{fontsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{tick\PYGZus{}params}\PYG{p}{(}\PYG{n}{labelsize}\PYG{o}{=}\PYG{l+m+mi}{18}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \end{sphinxVerbatim} From the above figure we can see that LASSO with \(\lambda = 10^{-2}\) achieves a very good accuracy on the test set. This by far surpasses the other models for all values of \(\lambda\). \section{Data Analysis and Machine Learning: Logistic Regression} \label{\detokenize{chapter5:data-analysis-and-machine-learning-logistic-regression}}\label{\detokenize{chapter5::doc}} \sphinxstylestrong{Morten Hjorth\sphinxhyphen{}Jensen}, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University Date: \sphinxstylestrong{Oct 17, 2019} Copyright 1999\sphinxhyphen{}2019, Morten Hjorth\sphinxhyphen{}Jensen. Released under CC Attribution\sphinxhyphen{}NonCommercial 4.0 license \subsection{Logistic Regression} \label{\detokenize{chapter5:logistic-regression}} In linear regression our main interest was centered on learning the coefficients of a functional fit (say a polynomial) in order to be able to predict the response of a continuous variable on some unseen data. The fit to the continuous variable \(y_i\) is based on some independent variables \(\hat{x}_i\). Linear regression resulted in analytical expressions for standard ordinary Least Squares or Ridge regression (in terms of matrices to invert) for several quantities, ranging from the variance and thereby the confidence intervals of the parameters \(\hat{\beta}\) to the mean squared error. If we can invert the product of the design matrices, linear regression gives then a simple recipe for fitting our data. Classification problems, however, are concerned with outcomes taking the form of discrete variables (i.e. categories). We may for example, on the basis of DNA sequencing for a number of patients, like to find out which mutations are important for a certain disease; or based on scans of various patients’ brains, figure out if there is a tumor or not; or given a specific physical system, we’d like to identify its state, say whether it is an ordered or disordered system (typical situation in solid state physics); or classify the status of a patient, whether she/he has a stroke or not and many other similar situations. The most common situation we encounter when we apply logistic regression is that of two possible outcomes, normally denoted as a binary outcome, true or false, positive or negative, success or failure etc. \subsection{Optimization and Deep learning} \label{\detokenize{chapter5:optimization-and-deep-learning}} Logistic regression will also serve as our stepping stone towards neural network algorithms and supervised deep learning. For logistic learning, the minimization of the cost function leads to a non\sphinxhyphen{}linear equation in the parameters \(\hat{\beta}\). The optimization of the problem calls therefore for minimization algorithms. This forms the bottle neck of all machine learning algorithms, namely how to find reliable minima of a multi\sphinxhyphen{}variable function. This leads us to the family of gradient descent methods. The latter are the working horses of basically all modern machine learning algorithms. We note also that many of the topics discussed here on logistic regression are also commonly used in modern supervised Deep Learning models, as we will see later. \subsection{Basics} \label{\detokenize{chapter5:basics}} We consider the case where the dependent variables, also called the responses or the outcomes, \(y_i\) are discrete and only take values from \(k=0,\dots,K-1\) (i.e. \(K\) classes). The goal is to predict the output classes from the design matrix \(\hat{X}\in\mathbb{R}^{n\times p}\) made of \(n\) samples, each of which carries \(p\) features or predictors. The primary goal is to identify the classes to which new unseen samples belong. Let us specialize to the case of two classes only, with outputs \(y_i=0\) and \(y_i=1\). Our outcomes could represent the status of a credit card user that could default or not on her/his credit card debt. That is \begin{equation*} \begin{split} y_i = \begin{bmatrix} 0 & \mathrm{no}\\ 1 & \mathrm{yes} \end{bmatrix}. \end{split} \end{equation*} \subsection{Linear classifier} \label{\detokenize{chapter5:linear-classifier}} Before moving to the logistic model, let us try to use our linear regression model to classify these two outcomes. We could for example fit a linear model to the default case if \(y_i > 0.5\) and the no default case \(y_i \leq 0.5\). We would then have our weighted linear combination, namely \begin{equation*} \begin{split} \begin{equation} \hat{y} = \hat{X}^T\hat{\beta} + \hat{\epsilon}, \label{_auto1} \tag{1} \end{equation} \end{split} \end{equation*} where \(\hat{y}\) is a vector representing the possible outcomes, \(\hat{X}\) is our \(n\times p\) design matrix and \(\hat{\beta}\) represents our estimators/predictors. \subsection{Some selected properties} \label{\detokenize{chapter5:some-selected-properties}} The main problem with our function is that it takes values on the entire real axis. In the case of logistic regression, however, the labels \(y_i\) are discrete variables. A typical example is the credit card data discussed below here, where we can set the state of defaulting the debt to \(y_i=1\) and not to \(y_i=0\) for one the persons in the data set (see the full example below). One simple way to get a discrete output is to have sign functions that map the output of a linear regressor to values \(\{0,1\}\), \(f(s_i)=sign(s_i)=1\) if \(s_i\ge 0\) and 0 if otherwise. We will encounter this model in our first demonstration of neural networks. Historically it is called the “perceptron” model in the machine learning literature. This model is extremely simple. However, in many cases it is more favorable to use a \textasciigrave{}\textasciigrave{}soft” classifier that outputs the probability of a given category. This leads us to the logistic function. \subsection{The logistic function} \label{\detokenize{chapter5:the-logistic-function}} The perceptron is an example of a \textasciigrave{}\textasciigrave{}hard classification” model. We will encounter this model when we discuss neural networks as well. Each datapoint is deterministically assigned to a category (i.e \(y_i=0\) or \(y_i=1\)). In many cases, it is favorable to have a “soft” classifier that outputs the probability of a given category rather than a single value. For example, given \(x_i\), the classifier outputs the probability of being in a category \(k\). Logistic regression is the most common example of a so\sphinxhyphen{}called soft classifier. In logistic regression, the probability that a data point \(x_i\) belongs to a category \(y_i=\{0,1\}\) is given by the so\sphinxhyphen{}called logit function (or Sigmoid) which is meant to represent the likelihood for a given event, \begin{equation*} \begin{split} p(t) = \frac{1}{1+\mathrm \exp{-t}}=\frac{\exp{t}}{1+\mathrm \exp{t}}. \end{split} \end{equation*} Note that \(1-p(t)= p(-t)\). \subsection{Examples of likelihood functions used in logistic regression and nueral networks} \label{\detokenize{chapter5:examples-of-likelihood-functions-used-in-logistic-regression-and-nueral-networks}} The following code plots the logistic function, the step function and other functions we will encounter from here and on. \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{o}{\PYGZpc{}}\PYG{k}{matplotlib} inline \PYG{l+s+sd}{\PYGZdq{}\PYGZdq{}\PYGZdq{}The sigmoid function (or the logistic curve) is a} \PYG{l+s+sd}{function that takes any real number, z, and outputs a number (0,1).} \PYG{l+s+sd}{It is useful in neural networks for assigning weights on a relative scale.} \PYG{l+s+sd}{The value z is the weighted sum of parameters involved in the learning algorithm.\PYGZdq{}\PYGZdq{}\PYGZdq{}} \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt} \PYG{k+kn}{import} \PYG{n+nn}{math} \PYG{k}{as} \PYG{n+nn}{mt} \PYG{n}{z} \PYG{o}{=} \PYG{n}{numpy}\PYG{o}{.}\PYG{n}{arange}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{5}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{,} \PYG{o}{.}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{n}{sigma\PYGZus{}fn} \PYG{o}{=} \PYG{n}{numpy}\PYG{o}{.}\PYG{n}{vectorize}\PYG{p}{(}\PYG{k}{lambda} \PYG{n}{z}\PYG{p}{:} \PYG{l+m+mi}{1}\PYG{o}{/}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{o}{+}\PYG{n}{numpy}\PYG{o}{.}\PYG{n}{exp}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{z}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)} \PYG{n}{sigma} \PYG{o}{=} \PYG{n}{sigma\PYGZus{}fn}\PYG{p}{(}\PYG{n}{z}\PYG{p}{)} \PYG{n}{fig} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{figure}\PYG{p}{(}\PYG{p}{)} \PYG{n}{ax} \PYG{o}{=} \PYG{n}{fig}\PYG{o}{.}\PYG{n}{add\PYGZus{}subplot}\PYG{p}{(}\PYG{l+m+mi}{111}\PYG{p}{)} \PYG{n}{ax}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{z}\PYG{p}{,} \PYG{n}{sigma}\PYG{p}{)} \PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}ylim}\PYG{p}{(}\PYG{p}{[}\PYG{o}{\PYGZhy{}}\PYG{l+m+mf}{0.1}\PYG{p}{,} \PYG{l+m+mf}{1.1}\PYG{p}{]}\PYG{p}{)} \PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}xlim}\PYG{p}{(}\PYG{p}{[}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{5}\PYG{p}{,}\PYG{l+m+mi}{5}\PYG{p}{]}\PYG{p}{)} \PYG{n}{ax}\PYG{o}{.}\PYG{n}{grid}\PYG{p}{(}\PYG{k+kc}{True}\PYG{p}{)} \PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}xlabel}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{z}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}title}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{sigmoid function}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \PYG{l+s+sd}{\PYGZdq{}\PYGZdq{}\PYGZdq{}Step Function\PYGZdq{}\PYGZdq{}\PYGZdq{}} \PYG{n}{z} \PYG{o}{=} \PYG{n}{numpy}\PYG{o}{.}\PYG{n}{arange}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{5}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{,} \PYG{o}{.}\PYG{l+m+mi}{02}\PYG{p}{)} \PYG{n}{step\PYGZus{}fn} \PYG{o}{=} \PYG{n}{numpy}\PYG{o}{.}\PYG{n}{vectorize}\PYG{p}{(}\PYG{k}{lambda} \PYG{n}{z}\PYG{p}{:} \PYG{l+m+mf}{1.0} \PYG{k}{if} \PYG{n}{z} \PYG{o}{\PYGZgt{}}\PYG{o}{=} \PYG{l+m+mf}{0.0} \PYG{k}{else} \PYG{l+m+mf}{0.0}\PYG{p}{)} \PYG{n}{step} \PYG{o}{=} \PYG{n}{step\PYGZus{}fn}\PYG{p}{(}\PYG{n}{z}\PYG{p}{)} \PYG{n}{fig} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{figure}\PYG{p}{(}\PYG{p}{)} \PYG{n}{ax} \PYG{o}{=} \PYG{n}{fig}\PYG{o}{.}\PYG{n}{add\PYGZus{}subplot}\PYG{p}{(}\PYG{l+m+mi}{111}\PYG{p}{)} \PYG{n}{ax}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{z}\PYG{p}{,} \PYG{n}{step}\PYG{p}{)} \PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}ylim}\PYG{p}{(}\PYG{p}{[}\PYG{o}{\PYGZhy{}}\PYG{l+m+mf}{0.5}\PYG{p}{,} \PYG{l+m+mf}{1.5}\PYG{p}{]}\PYG{p}{)} \PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}xlim}\PYG{p}{(}\PYG{p}{[}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{5}\PYG{p}{,}\PYG{l+m+mi}{5}\PYG{p}{]}\PYG{p}{)} \PYG{n}{ax}\PYG{o}{.}\PYG{n}{grid}\PYG{p}{(}\PYG{k+kc}{True}\PYG{p}{)} \PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}xlabel}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{z}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}title}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{step function}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \PYG{l+s+sd}{\PYGZdq{}\PYGZdq{}\PYGZdq{}tanh Function\PYGZdq{}\PYGZdq{}\PYGZdq{}} \PYG{n}{z} \PYG{o}{=} \PYG{n}{numpy}\PYG{o}{.}\PYG{n}{arange}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{mt}\PYG{o}{.}\PYG{n}{pi}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{mt}\PYG{o}{.}\PYG{n}{pi}\PYG{p}{,} \PYG{l+m+mf}{0.1}\PYG{p}{)} \PYG{n}{t} \PYG{o}{=} \PYG{n}{numpy}\PYG{o}{.}\PYG{n}{tanh}\PYG{p}{(}\PYG{n}{z}\PYG{p}{)} \PYG{n}{fig} \PYG{o}{=} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{figure}\PYG{p}{(}\PYG{p}{)} \PYG{n}{ax} \PYG{o}{=} \PYG{n}{fig}\PYG{o}{.}\PYG{n}{add\PYGZus{}subplot}\PYG{p}{(}\PYG{l+m+mi}{111}\PYG{p}{)} \PYG{n}{ax}\PYG{o}{.}\PYG{n}{plot}\PYG{p}{(}\PYG{n}{z}\PYG{p}{,} \PYG{n}{t}\PYG{p}{)} \PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}ylim}\PYG{p}{(}\PYG{p}{[}\PYG{o}{\PYGZhy{}}\PYG{l+m+mf}{1.0}\PYG{p}{,} \PYG{l+m+mf}{1.0}\PYG{p}{]}\PYG{p}{)} \PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}xlim}\PYG{p}{(}\PYG{p}{[}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{mt}\PYG{o}{.}\PYG{n}{pi}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{mt}\PYG{o}{.}\PYG{n}{pi}\PYG{p}{]}\PYG{p}{)} \PYG{n}{ax}\PYG{o}{.}\PYG{n}{grid}\PYG{p}{(}\PYG{k+kc}{True}\PYG{p}{)} \PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}xlabel}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{z}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{ax}\PYG{o}{.}\PYG{n}{set\PYGZus{}title}\PYG{p}{(}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{tanh function}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \end{sphinxVerbatim} \noindent\sphinxincludegraphics{{chapter5_7_0}.png} \noindent\sphinxincludegraphics{{chapter5_7_1}.png} \noindent\sphinxincludegraphics{{chapter5_7_2}.png} \subsection{Two parameters} \label{\detokenize{chapter5:two-parameters}} We assume now that we have two classes with \(y_i\) either \(0\) or \(1\). Furthermore we assume also that we have only two parameters \(\beta\) in our fitting of the Sigmoid function, that is we define probabilities \begin{equation*} \begin{split} \begin{align*} p(y_i=1|x_i,\hat{\beta}) &= \frac{\exp{(\beta_0+\beta_1x_i)}}{1+\exp{(\beta_0+\beta_1x_i)}},\nonumber\\ p(y_i=0|x_i,\hat{\beta}) &= 1 - p(y_i=1|x_i,\hat{\beta}), \end{align*} \end{split} \end{equation*} where \(\hat{\beta}\) are the weights we wish to extract from data, in our case \(\beta_0\) and \(\beta_1\). Note that we used \begin{equation*} \begin{split} p(y_i=0\vert x_i, \hat{\beta}) = 1-p(y_i=1\vert x_i, \hat{\beta}). \end{split} \end{equation*} \subsection{Maximum likelihood} \label{\detokenize{chapter5:maximum-likelihood}} In order to define the total likelihood for all possible outcomes from adataset \(\mathcal{D}=\{(y_i,x_i)\}\), with the binary labels \(y_i\in\{0,1\}\) and where the data points are drawn independently, we use the so\sphinxhyphen{}called \sphinxhref{https://en.wikipedia.org/wiki/Maximum\_likelihood\_estimation}{Maximum Likelihood Estimation} (MLE) principle. We aim thus at maximizing the probability of seeing the observed data. We can then approximate the likelihood in terms of the product of the individual probabilities of a specific outcome \(y_i\), that is \begin{equation*} \begin{split} \begin{align*} P(\mathcal{D}|\hat{\beta})& = \prod_{i=1}^n \left[p(y_i=1|x_i,\hat{\beta})\right]^{y_i}\left[1-p(y_i=1|x_i,\hat{\beta}))\right]^{1-y_i}\nonumber \\ \end{align*} \end{split} \end{equation*} from which we obtain the log\sphinxhyphen{}likelihood and our \sphinxstylestrong{cost/loss} function \begin{equation*} \begin{split} \mathcal{C}(\hat{\beta}) = \sum_{i=1}^n \left( y_i\log{p(y_i=1|x_i,\hat{\beta})} + (1-y_i)\log\left[1-p(y_i=1|x_i,\hat{\beta}))\right]\right). \end{split} \end{equation*} \subsection{The cost function rewritten} \label{\detokenize{chapter5:the-cost-function-rewritten}} Reordering the logarithms, we can rewrite the \sphinxstylestrong{cost/loss} function as \begin{equation*} \begin{split} \mathcal{C}(\hat{\beta}) = \sum_{i=1}^n \left(y_i(\beta_0+\beta_1x_i) -\log{(1+\exp{(\beta_0+\beta_1x_i)})}\right). \end{split} \end{equation*} The maximum likelihood estimator is defined as the set of parameters that maximize the log\sphinxhyphen{}likelihood where we maximize with respect to \(\beta\). Since the cost (error) function is just the negative log\sphinxhyphen{}likelihood, for logistic regression we have that \begin{equation*} \begin{split} \mathcal{C}(\hat{\beta})=-\sum_{i=1}^n \left(y_i(\beta_0+\beta_1x_i) -\log{(1+\exp{(\beta_0+\beta_1x_i)})}\right). \end{split} \end{equation*} This equation is known in statistics as the \sphinxstylestrong{cross entropy}. Finally, we note that just as in linear regression, in practice we often supplement the cross\sphinxhyphen{}entropy with additional regularization terms, usually \(L_1\) and \(L_2\) regularization as we did for Ridge and Lasso regression. \subsection{Minimizing the cross entropy} \label{\detokenize{chapter5:minimizing-the-cross-entropy}} The cross entropy is a convex function of the weights \(\hat{\beta}\) and, therefore, any local minimizer is a global minimizer. Minimizing this cost function with respect to the two parameters \(\beta_0\) and \(\beta_1\) we obtain \begin{equation*} \begin{split} \frac{\partial \mathcal{C}(\hat{\beta})}{\partial \beta_0} = -\sum_{i=1}^n \left(y_i -\frac{\exp{(\beta_0+\beta_1x_i)}}{1+\exp{(\beta_0+\beta_1x_i)}}\right), \end{split} \end{equation*} and \begin{equation*} \begin{split} \frac{\partial \mathcal{C}(\hat{\beta})}{\partial \beta_1} = -\sum_{i=1}^n \left(y_ix_i -x_i\frac{\exp{(\beta_0+\beta_1x_i)}}{1+\exp{(\beta_0+\beta_1x_i)}}\right). \end{split} \end{equation*} \subsection{A more compact expression} \label{\detokenize{chapter5:a-more-compact-expression}} Let us now define a vector \(\hat{y}\) with \(n\) elements \(y_i\), an \(n\times p\) matrix \(\hat{X}\) which contains the \(x_i\) values and a vector \(\hat{p}\) of fitted probabilities \(p(y_i\vert x_i,\hat{\beta})\). We can rewrite in a more compact form the first derivative of cost function as \begin{equation*} \begin{split} \frac{\partial \mathcal{C}(\hat{\beta})}{\partial \hat{\beta}} = -\hat{X}^T\left(\hat{y}-\hat{p}\right). \end{split} \end{equation*} If we in addition define a diagonal matrix \(\hat{W}\) with elements \(p(y_i\vert x_i,\hat{\beta})(1-p(y_i\vert x_i,\hat{\beta})\), we can obtain a compact expression of the second derivative as \begin{equation*} \begin{split} \frac{\partial^2 \mathcal{C}(\hat{\beta})}{\partial \hat{\beta}\partial \hat{\beta}^T} = \hat{X}^T\hat{W}\hat{X}. \end{split} \end{equation*} \subsection{Extending to more predictors} \label{\detokenize{chapter5:extending-to-more-predictors}} Within a binary classification problem, we can easily expand our model to include multiple predictors. Our ratio between likelihoods is then with \(p\) predictors \begin{equation*} \begin{split} \log{ \frac{p(\hat{\beta}\hat{x})}{1-p(\hat{\beta}\hat{x})}} = \beta_0+\beta_1x_1+\beta_2x_2+\dots+\beta_px_p. \end{split} \end{equation*} Here we defined \(\hat{x}=[1,x_1,x_2,\dots,x_p]\) and \(\hat{\beta}=[\beta_0, \beta_1, \dots, \beta_p]\) leading to \begin{equation*} \begin{split} p(\hat{\beta}\hat{x})=\frac{ \exp{(\beta_0+\beta_1x_1+\beta_2x_2+\dots+\beta_px_p)}}{1+\exp{(\beta_0+\beta_1x_1+\beta_2x_2+\dots+\beta_px_p)}}. \end{split} \end{equation*} \subsection{Including more classes} \label{\detokenize{chapter5:including-more-classes}} Till now we have mainly focused on two classes, the so\sphinxhyphen{}called binary system. Suppose we wish to extend to \(K\) classes. Let us for the sake of simplicity assume we have only two predictors. We have then following model 1 5 \textless{} \textless{} \textless{} ! ! M A T H \_ B L O C K \begin{equation*} \begin{split} \log{\frac{p(C=2\vert x)}{p(K\vert x)}} = \beta_{20}+\beta_{21}x_1, \end{split} \end{equation*} and so on till the class \(C=K-1\) class \begin{equation*} \begin{split} \log{\frac{p(C=K-1\vert x)}{p(K\vert x)}} = \beta_{(K-1)0}+\beta_{(K-1)1}x_1, \end{split} \end{equation*} and the model is specified in term of \(K-1\) so\sphinxhyphen{}called log\sphinxhyphen{}odds or \sphinxstylestrong{logit} transformations. \subsection{More classes} \label{\detokenize{chapter5:more-classes}} In our discussion of neural networks we will encounter the above again in terms of a slightly modified function, the so\sphinxhyphen{}called \sphinxstylestrong{Softmax} function. The softmax function is used in various multiclass classification methods, such as multinomial logistic regression (also known as softmax regression), multiclass linear discriminant analysis, naive Bayes classifiers, and artificial neural networks. Specifically, in multinomial logistic regression and linear discriminant analysis, the input to the function is the result of \(K\) distinct linear functions, and the predicted probability for the \(k\)\sphinxhyphen{}th class given a sample vector \(\hat{x}\) and a weighting vector \(\hat{\beta}\) is (with two predictors): \begin{equation*} \begin{split} p(C=k\vert \mathbf {x} )=\frac{\exp{(\beta_{k0}+\beta_{k1}x_1)}}{1+\sum_{l=1}^{K-1}\exp{(\beta_{l0}+\beta_{l1}x_1)}}. \end{split} \end{equation*} It is easy to extend to more predictors. The final class is \begin{equation*} \begin{split} p(C=K\vert \mathbf {x} )=\frac{1}{1+\sum_{l=1}^{K-1}\exp{(\beta_{l0}+\beta_{l1}x_1)}}, \end{split} \end{equation*} and they sum to one. Our earlier discussions were all specialized to the case with two classes only. It is easy to see from the above that what we derived earlier is compatible with these equations. To find the optimal parameters we would typically use a gradient descent method. Newton’s method and gradient descent methods are discussed in the material on \sphinxhref{https://compphysics.github.io/MachineLearning/doc/pub/Splines/html/Splines-bs.html}{optimization methods}. \subsection{A simple classification problem} \label{\detokenize{chapter5:a-simple-classification-problem}} \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{k+kn}{from} \PYG{n+nn}{sklearn} \PYG{k+kn}{import} \PYG{n}{datasets}\PYG{p}{,} \PYG{n}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n+nn}{matplotlib}\PYG{n+nn}{.}\PYG{n+nn}{pyplot} \PYG{k}{as} \PYG{n+nn}{plt} \PYG{k}{def} \PYG{n+nf}{generate\PYGZus{}data}\PYG{p}{(}\PYG{p}{)}\PYG{p}{:} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{seed}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{)} \PYG{n}{X}\PYG{p}{,} \PYG{n}{y} \PYG{o}{=} \PYG{n}{datasets}\PYG{o}{.}\PYG{n}{make\PYGZus{}moons}\PYG{p}{(}\PYG{l+m+mi}{200}\PYG{p}{,} \PYG{n}{noise}\PYG{o}{=}\PYG{l+m+mf}{0.20}\PYG{p}{)} \PYG{k}{return} \PYG{n}{X}\PYG{p}{,} \PYG{n}{y} \PYG{k}{def} \PYG{n+nf}{visualize}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{clf}\PYG{p}{)}\PYG{p}{:} \PYG{n}{plot\PYGZus{}decision\PYGZus{}boundary}\PYG{p}{(}\PYG{k}{lambda} \PYG{n}{x}\PYG{p}{:} \PYG{n}{clf}\PYG{o}{.}\PYG{n}{predict}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{X}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)} \PYG{k}{def} \PYG{n+nf}{plot\PYGZus{}decision\PYGZus{}boundary}\PYG{p}{(}\PYG{n}{pred\PYGZus{}func}\PYG{p}{,} \PYG{n}{X}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)}\PYG{p}{:} \PYG{c+c1}{\PYGZsh{} Set min and max values and give it some padding} \PYG{n}{x\PYGZus{}min}\PYG{p}{,} \PYG{n}{x\PYGZus{}max} \PYG{o}{=} \PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{]}\PYG{o}{.}\PYG{n}{min}\PYG{p}{(}\PYG{p}{)} \PYG{o}{\PYGZhy{}} \PYG{o}{.}\PYG{l+m+mi}{5}\PYG{p}{,} \PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{]}\PYG{o}{.}\PYG{n}{max}\PYG{p}{(}\PYG{p}{)} \PYG{o}{+} \PYG{o}{.}\PYG{l+m+mi}{5} \PYG{n}{y\PYGZus{}min}\PYG{p}{,} \PYG{n}{y\PYGZus{}max} \PYG{o}{=} \PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{o}{.}\PYG{n}{min}\PYG{p}{(}\PYG{p}{)} \PYG{o}{\PYGZhy{}} \PYG{o}{.}\PYG{l+m+mi}{5}\PYG{p}{,} \PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{o}{.}\PYG{n}{max}\PYG{p}{(}\PYG{p}{)} \PYG{o}{+} \PYG{o}{.}\PYG{l+m+mi}{5} \PYG{n}{h} \PYG{o}{=} \PYG{l+m+mf}{0.01} \PYG{c+c1}{\PYGZsh{} Generate a grid of points with distance h between them} \PYG{n}{xx}\PYG{p}{,} \PYG{n}{yy} \PYG{o}{=} \PYG{n}{np}\PYG{o}{.}\PYG{n}{meshgrid}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{arange}\PYG{p}{(}\PYG{n}{x\PYGZus{}min}\PYG{p}{,} \PYG{n}{x\PYGZus{}max}\PYG{p}{,} \PYG{n}{h}\PYG{p}{)}\PYG{p}{,} \PYG{n}{np}\PYG{o}{.}\PYG{n}{arange}\PYG{p}{(}\PYG{n}{y\PYGZus{}min}\PYG{p}{,} \PYG{n}{y\PYGZus{}max}\PYG{p}{,} \PYG{n}{h}\PYG{p}{)}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Predict the function value for the whole gid} \PYG{n}{Z} \PYG{o}{=} \PYG{n}{pred\PYGZus{}func}\PYG{p}{(}\PYG{n}{np}\PYG{o}{.}\PYG{n}{c\PYGZus{}}\PYG{p}{[}\PYG{n}{xx}\PYG{o}{.}\PYG{n}{ravel}\PYG{p}{(}\PYG{p}{)}\PYG{p}{,} \PYG{n}{yy}\PYG{o}{.}\PYG{n}{ravel}\PYG{p}{(}\PYG{p}{)}\PYG{p}{]}\PYG{p}{)} \PYG{n}{Z} \PYG{o}{=} \PYG{n}{Z}\PYG{o}{.}\PYG{n}{reshape}\PYG{p}{(}\PYG{n}{xx}\PYG{o}{.}\PYG{n}{shape}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Plot the contour and training examples} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{contourf}\PYG{p}{(}\PYG{n}{xx}\PYG{p}{,} \PYG{n}{yy}\PYG{p}{,} \PYG{n}{Z}\PYG{p}{,} \PYG{n}{cmap}\PYG{o}{=}\PYG{n}{plt}\PYG{o}{.}\PYG{n}{cm}\PYG{o}{.}\PYG{n}{Spectral}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{scatter}\PYG{p}{(}\PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{,} \PYG{n}{X}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{,} \PYG{n}{c}\PYG{o}{=}\PYG{n}{y}\PYG{p}{,} \PYG{n}{cmap}\PYG{o}{=}\PYG{n}{plt}\PYG{o}{.}\PYG{n}{cm}\PYG{o}{.}\PYG{n}{Spectral}\PYG{p}{)} \PYG{n}{plt}\PYG{o}{.}\PYG{n}{show}\PYG{p}{(}\PYG{p}{)} \PYG{k}{def} \PYG{n+nf}{classify}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)}\PYG{p}{:} \PYG{n}{clf} \PYG{o}{=} \PYG{n}{linear\PYGZus{}model}\PYG{o}{.}\PYG{n}{LogisticRegressionCV}\PYG{p}{(}\PYG{p}{)} \PYG{n}{clf}\PYG{o}{.}\PYG{n}{fit}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)} \PYG{k}{return} \PYG{n}{clf} \PYG{k}{def} \PYG{n+nf}{main}\PYG{p}{(}\PYG{p}{)}\PYG{p}{:} \PYG{n}{X}\PYG{p}{,} \PYG{n}{y} \PYG{o}{=} \PYG{n}{generate\PYGZus{}data}\PYG{p}{(}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} visualize(X, y)} \PYG{n}{clf} \PYG{o}{=} \PYG{n}{classify}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)} \PYG{n}{visualize}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{clf}\PYG{p}{)} \PYG{k}{if} \PYG{n+nv+vm}{\PYGZus{}\PYGZus{}name\PYGZus{}\PYGZus{}} \PYG{o}{==} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{\PYGZus{}\PYGZus{}main\PYGZus{}\PYGZus{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:} \PYG{n}{main}\PYG{p}{(}\PYG{p}{)} \end{sphinxVerbatim} \noindent\sphinxincludegraphics{{chapter5_42_0}.png} \subsection{The Credit Card example} \label{\detokenize{chapter5:the-credit-card-example}} Here we use the the \sphinxhref{https://archive.ics.uci.edu/ml/datasets/default+of+credit+card+clients}{credit card data}. The data are from an extensive database from Taiwan and include more than ten predictors. For categorical data \sphinxhyphen{}Scikit\sphinxhyphen{}Learn\sphinxhyphen{} provides a so\sphinxhyphen{}called \sphinxstylestrong{one\sphinxhyphen{}hot encoder}. This is called one\sphinxhyphen{}hot encoding, because only one attribute will be equal to 1 (hot), while the others will be 0 (cold). \sphinxstylestrong{Scikit\sphinxhyphen{}Learn} provides a OneHotEncoder encoder to convert integer categorical values into one\sphinxhyphen{}hot \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{preprocessing} \PYG{k+kn}{import} \PYG{n}{OneHotEncoder} \PYG{n}{encoder} \PYG{o}{=} \PYG{n}{OneHotEncoder}\PYG{p}{(}\PYG{p}{)} \end{sphinxVerbatim} \subsection{How to read the Credit Card data} \label{\detokenize{chapter5:how-to-read-the-credit-card-data}} \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{k+kn}{import} \PYG{n+nn}{pandas} \PYG{k}{as} \PYG{n+nn}{pd} \PYG{k+kn}{import} \PYG{n+nn}{os} \PYG{k+kn}{import} \PYG{n+nn}{numpy} \PYG{k}{as} \PYG{n+nn}{np} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{train\PYGZus{}test\PYGZus{}split} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{preprocessing} \PYG{k+kn}{import} \PYG{n}{OneHotEncoder} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{compose} \PYG{k+kn}{import} \PYG{n}{ColumnTransformer} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{preprocessing} \PYG{k+kn}{import} \PYG{n}{StandardScaler}\PYG{p}{,} \PYG{n}{OneHotEncoder} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{metrics} \PYG{k+kn}{import} \PYG{n}{confusion\PYGZus{}matrix}\PYG{p}{,} \PYG{n}{accuracy\PYGZus{}score}\PYG{p}{,} \PYG{n}{roc\PYGZus{}auc\PYGZus{}score} \PYG{c+c1}{\PYGZsh{} Trying to set the seed} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{seed}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{)} \PYG{k+kn}{import} \PYG{n+nn}{random} \PYG{n}{random}\PYG{o}{.}\PYG{n}{seed}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Reading file into data frame} \PYG{n}{cwd} \PYG{o}{=} \PYG{n}{os}\PYG{o}{.}\PYG{n}{getcwd}\PYG{p}{(}\PYG{p}{)} \PYG{n}{filename} \PYG{o}{=} \PYG{n}{cwd} \PYG{o}{+} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{/default of credit card clients.xls}\PYG{l+s+s1}{\PYGZsq{}} \PYG{n}{nanDict} \PYG{o}{=} \PYG{p}{\PYGZob{}}\PYG{p}{\PYGZcb{}} \PYG{n}{df} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{read\PYGZus{}excel}\PYG{p}{(}\PYG{n}{filename}\PYG{p}{,} \PYG{n}{header}\PYG{o}{=}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{skiprows}\PYG{o}{=}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{n}{index\PYGZus{}col}\PYG{o}{=}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{n}{na\PYGZus{}values}\PYG{o}{=}\PYG{n}{nanDict}\PYG{p}{)} \PYG{n}{df}\PYG{o}{.}\PYG{n}{rename}\PYG{p}{(}\PYG{n}{index}\PYG{o}{=}\PYG{n+nb}{str}\PYG{p}{,} \PYG{n}{columns}\PYG{o}{=}\PYG{p}{\PYGZob{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{default payment next month}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{defaultPaymentNextMonth}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{\PYGZcb{}}\PYG{p}{,} \PYG{n}{inplace}\PYG{o}{=}\PYG{k+kc}{True}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Features and targets } \PYG{n}{X} \PYG{o}{=} \PYG{n}{df}\PYG{o}{.}\PYG{n}{loc}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{df}\PYG{o}{.}\PYG{n}{columns} \PYG{o}{!=} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{defaultPaymentNextMonth}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{o}{.}\PYG{n}{values} \PYG{n}{y} \PYG{o}{=} \PYG{n}{df}\PYG{o}{.}\PYG{n}{loc}\PYG{p}{[}\PYG{p}{:}\PYG{p}{,} \PYG{n}{df}\PYG{o}{.}\PYG{n}{columns} \PYG{o}{==} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{defaultPaymentNextMonth}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{o}{.}\PYG{n}{values} \PYG{c+c1}{\PYGZsh{} Categorical variables to one\PYGZhy{}hot\PYGZsq{}s} \PYG{n}{onehotencoder} \PYG{o}{=} \PYG{n}{OneHotEncoder}\PYG{p}{(}\PYG{n}{categories}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{auto}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)} \PYG{n}{X} \PYG{o}{=} \PYG{n}{ColumnTransformer}\PYG{p}{(} \PYG{p}{[}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{onehotencoder}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{)}\PYG{p}{,}\PYG{p}{]}\PYG{p}{,} \PYG{n}{remainder}\PYG{o}{=}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{passthrough}\PYG{l+s+s2}{\PYGZdq{}} \PYG{p}{)}\PYG{o}{.}\PYG{n}{fit\PYGZus{}transform}\PYG{p}{(}\PYG{n}{X}\PYG{p}{)} \PYG{n}{y}\PYG{o}{.}\PYG{n}{shape} \PYG{c+c1}{\PYGZsh{} Train\PYGZhy{}test split} \PYG{n}{trainingShare} \PYG{o}{=} \PYG{l+m+mf}{0.5} \PYG{n}{seed} \PYG{o}{=} \PYG{l+m+mi}{1} \PYG{n}{XTrain}\PYG{p}{,} \PYG{n}{XTest}\PYG{p}{,} \PYG{n}{yTrain}\PYG{p}{,} \PYG{n}{yTest}\PYG{o}{=}\PYG{n}{train\PYGZus{}test\PYGZus{}split}\PYG{p}{(}\PYG{n}{X}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{train\PYGZus{}size}\PYG{o}{=}\PYG{n}{trainingShare}\PYG{p}{,} \PYGZbs{} \PYG{n}{test\PYGZus{}size} \PYG{o}{=} \PYG{l+m+mi}{1}\PYG{o}{\PYGZhy{}}\PYG{n}{trainingShare}\PYG{p}{,} \PYG{n}{random\PYGZus{}state}\PYG{o}{=}\PYG{n}{seed}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Input Scaling} \PYG{n}{sc} \PYG{o}{=} \PYG{n}{StandardScaler}\PYG{p}{(}\PYG{p}{)} \PYG{n}{XTrain} \PYG{o}{=} \PYG{n}{sc}\PYG{o}{.}\PYG{n}{fit\PYGZus{}transform}\PYG{p}{(}\PYG{n}{XTrain}\PYG{p}{)} \PYG{n}{XTest} \PYG{o}{=} \PYG{n}{sc}\PYG{o}{.}\PYG{n}{transform}\PYG{p}{(}\PYG{n}{XTest}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} One\PYGZhy{}hot\PYGZsq{}s of the target vector} \PYG{n}{Y\PYGZus{}train\PYGZus{}onehot}\PYG{p}{,} \PYG{n}{Y\PYGZus{}test\PYGZus{}onehot} \PYG{o}{=} \PYG{n}{onehotencoder}\PYG{o}{.}\PYG{n}{fit\PYGZus{}transform}\PYG{p}{(}\PYG{n}{yTrain}\PYG{p}{)}\PYG{p}{,} \PYG{n}{onehotencoder}\PYG{o}{.}\PYG{n}{fit\PYGZus{}transform}\PYG{p}{(}\PYG{n}{yTest}\PYG{p}{)} \PYG{c+c1}{\PYGZsh{} Remove instances with zeros only for past bill statements or paid amounts} \PYG{l+s+sd}{\PYGZsq{}\PYGZsq{}\PYGZsq{}} \PYG{l+s+sd}{df = df.drop(df[(df.BILL\PYGZus{}AMT1 == 0) \PYGZam{}} \PYG{l+s+sd}{ (df.BILL\PYGZus{}AMT2 == 0) \PYGZam{}} \PYG{l+s+sd}{ (df.BILL\PYGZus{}AMT3 == 0) \PYGZam{}} \PYG{l+s+sd}{ (df.BILL\PYGZus{}AMT4 == 0) \PYGZam{}} \PYG{l+s+sd}{ (df.BILL\PYGZus{}AMT5 == 0) \PYGZam{}} \PYG{l+s+sd}{ (df.BILL\PYGZus{}AMT6 == 0) \PYGZam{}} \PYG{l+s+sd}{ (df.PAY\PYGZus{}AMT1 == 0) \PYGZam{}} \PYG{l+s+sd}{ (df.PAY\PYGZus{}AMT2 == 0) \PYGZam{}} \PYG{l+s+sd}{ (df.PAY\PYGZus{}AMT3 == 0) \PYGZam{}} \PYG{l+s+sd}{ (df.PAY\PYGZus{}AMT4 == 0) \PYGZam{}} \PYG{l+s+sd}{ (df.PAY\PYGZus{}AMT5 == 0) \PYGZam{}} \PYG{l+s+sd}{ (df.PAY\PYGZus{}AMT6 == 0)].index)} \PYG{l+s+sd}{\PYGZsq{}\PYGZsq{}\PYGZsq{}} \PYG{n}{df} \PYG{o}{=} \PYG{n}{df}\PYG{o}{.}\PYG{n}{drop}\PYG{p}{(}\PYG{n}{df}\PYG{p}{[}\PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{BILL\PYGZus{}AMT1} \PYG{o}{==} \PYG{l+m+mi}{0}\PYG{p}{)} \PYG{o}{\PYGZam{}} \PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{BILL\PYGZus{}AMT2} \PYG{o}{==} \PYG{l+m+mi}{0}\PYG{p}{)} \PYG{o}{\PYGZam{}} \PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{BILL\PYGZus{}AMT3} \PYG{o}{==} \PYG{l+m+mi}{0}\PYG{p}{)} \PYG{o}{\PYGZam{}} \PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{BILL\PYGZus{}AMT4} \PYG{o}{==} \PYG{l+m+mi}{0}\PYG{p}{)} \PYG{o}{\PYGZam{}} \PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{BILL\PYGZus{}AMT5} \PYG{o}{==} \PYG{l+m+mi}{0}\PYG{p}{)} \PYG{o}{\PYGZam{}} \PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{BILL\PYGZus{}AMT6} \PYG{o}{==} \PYG{l+m+mi}{0}\PYG{p}{)}\PYG{p}{]}\PYG{o}{.}\PYG{n}{index}\PYG{p}{)} \PYG{n}{df} \PYG{o}{=} \PYG{n}{df}\PYG{o}{.}\PYG{n}{drop}\PYG{p}{(}\PYG{n}{df}\PYG{p}{[}\PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{PAY\PYGZus{}AMT1} \PYG{o}{==} \PYG{l+m+mi}{0}\PYG{p}{)} \PYG{o}{\PYGZam{}} \PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{PAY\PYGZus{}AMT2} \PYG{o}{==} \PYG{l+m+mi}{0}\PYG{p}{)} \PYG{o}{\PYGZam{}} \PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{PAY\PYGZus{}AMT3} \PYG{o}{==} \PYG{l+m+mi}{0}\PYG{p}{)} \PYG{o}{\PYGZam{}} \PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{PAY\PYGZus{}AMT4} \PYG{o}{==} \PYG{l+m+mi}{0}\PYG{p}{)} \PYG{o}{\PYGZam{}} \PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{PAY\PYGZus{}AMT5} \PYG{o}{==} \PYG{l+m+mi}{0}\PYG{p}{)} \PYG{o}{\PYGZam{}} \PYG{p}{(}\PYG{n}{df}\PYG{o}{.}\PYG{n}{PAY\PYGZus{}AMT6} \PYG{o}{==} \PYG{l+m+mi}{0}\PYG{p}{)}\PYG{p}{]}\PYG{o}{.}\PYG{n}{index}\PYG{p}{)} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{linear\PYGZus{}model} \PYG{k+kn}{import} \PYG{n}{LogisticRegression} \PYG{k+kn}{from} \PYG{n+nn}{sklearn}\PYG{n+nn}{.}\PYG{n+nn}{model\PYGZus{}selection} \PYG{k+kn}{import} \PYG{n}{GridSearchCV} \PYG{n}{lambdas}\PYG{o}{=}\PYG{n}{np}\PYG{o}{.}\PYG{n}{logspace}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{5}\PYG{p}{,}\PYG{l+m+mi}{7}\PYG{p}{,}\PYG{l+m+mi}{13}\PYG{p}{)} \PYG{n}{parameters} \PYG{o}{=} \PYG{p}{[}\PYG{p}{\PYGZob{}}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{C}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{:} \PYG{l+m+mf}{1.}\PYG{o}{/}\PYG{n}{lambdas}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{solver}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:}\PYG{p}{[}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{lbfgs}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{]}\PYG{p}{\PYGZcb{}}\PYG{p}{]}\PYG{c+c1}{\PYGZsh{}*len(parameters)\PYGZcb{}]} \PYG{n}{scoring} \PYG{o}{=} \PYG{p}{[}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{accuracy}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{roc\PYGZus{}auc}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{]} \PYG{n}{logReg} \PYG{o}{=} \PYG{n}{LogisticRegression}\PYG{p}{(}\PYG{p}{)} \PYG{n}{gridSearch} \PYG{o}{=} \PYG{n}{GridSearchCV}\PYG{p}{(}\PYG{n}{logReg}\PYG{p}{,} \PYG{n}{parameters}\PYG{p}{,} \PYG{n}{cv}\PYG{o}{=}\PYG{l+m+mi}{5}\PYG{p}{,} \PYG{n}{scoring}\PYG{o}{=}\PYG{n}{scoring}\PYG{p}{,} \PYG{n}{refit}\PYG{o}{=}\PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{roc\PYGZus{}auc}\PYG{l+s+s1}{\PYGZsq{}}\PYG{p}{)} \end{sphinxVerbatim} \begin{sphinxVerbatim}[commandchars=\\\{\}] \PYG{g+gt}{\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}} \PYG{n+ne}{FileNotFoundError}\PYG{g+gWhitespace}{ }Traceback (most recent call last) \PYG{o}{\PYGZlt{}}\PYG{n}{ipython}\PYG{o}{\PYGZhy{}}\PYG{n+nb}{input}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{4}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{4}\PYG{n}{edad39a9543}\PYG{o}{\PYGZgt{}} \PYG{o+ow}{in} \PYG{o}{\PYGZlt{}}\PYG{n}{module}\PYG{o}{\PYGZgt{}} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{19} \PYG{n}{filename} \PYG{o}{=} \PYG{n}{cwd} \PYG{o}{+} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{/default of credit card clients.xls}\PYG{l+s+s1}{\PYGZsq{}} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{20} \PYG{n}{nanDict} \PYG{o}{=} \PYG{p}{\PYGZob{}}\PYG{p}{\PYGZcb{}} \PYG{n+ne}{\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{21} \PYG{n}{df} \PYG{o}{=} \PYG{n}{pd}\PYG{o}{.}\PYG{n}{read\PYGZus{}excel}\PYG{p}{(}\PYG{n}{filename}\PYG{p}{,} \PYG{n}{header}\PYG{o}{=}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{skiprows}\PYG{o}{=}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{n}{index\PYGZus{}col}\PYG{o}{=}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{n}{na\PYGZus{}values}\PYG{o}{=}\PYG{n}{nanDict}\PYG{p}{)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{22} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{23} \PYG{n}{df}\PYG{o}{.}\PYG{n}{rename}\PYG{p}{(}\PYG{n}{index}\PYG{o}{=}\PYG{n+nb}{str}\PYG{p}{,} \PYG{n}{columns}\PYG{o}{=}\PYG{p}{\PYGZob{}}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{default payment next month}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{defaultPaymentNextMonth}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{\PYGZcb{}}\PYG{p}{,} \PYG{n}{inplace}\PYG{o}{=}\PYG{k+kc}{True}\PYG{p}{)} \PYG{n+nn}{\PYGZti{}/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/pandas/io/excel/\PYGZus{}base.py} in \PYG{n+ni}{read\PYGZus{}excel}\PYG{n+nt}{(io, sheet\PYGZus{}name, header, names, index\PYGZus{}col, usecols, squeeze, dtype, engine, converters, true\PYGZus{}values, false\PYGZus{}values, skiprows, nrows, na\PYGZus{}values, keep\PYGZus{}default\PYGZus{}na, verbose, parse\PYGZus{}dates, date\PYGZus{}parser, thousands, comment, skipfooter, convert\PYGZus{}float, mangle\PYGZus{}dupe\PYGZus{}cols, **kwds)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{302} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{303} \PYG{k}{if} \PYG{o+ow}{not} \PYG{n+nb}{isinstance}\PYG{p}{(}\PYG{n}{io}\PYG{p}{,} \PYG{n}{ExcelFile}\PYG{p}{)}\PYG{p}{:} \PYG{n+ne}{\PYGZhy{}\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{304} \PYG{n}{io} \PYG{o}{=} \PYG{n}{ExcelFile}\PYG{p}{(}\PYG{n}{io}\PYG{p}{,} \PYG{n}{engine}\PYG{o}{=}\PYG{n}{engine}\PYG{p}{)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{305} \PYG{k}{elif} \PYG{n}{engine} \PYG{o+ow}{and} \PYG{n}{engine} \PYG{o}{!=} \PYG{n}{io}\PYG{o}{.}\PYG{n}{engine}\PYG{p}{:} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{306} \PYG{k}{raise} \PYG{n+ne}{ValueError}\PYG{p}{(} \PYG{n+nn}{\PYGZti{}/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/pandas/io/excel/\PYGZus{}base.py} in \PYG{n+ni}{\PYGZus{}\PYGZus{}init\PYGZus{}\PYGZus{}}\PYG{n+nt}{(self, io, engine)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{822} \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{\PYGZus{}io} \PYG{o}{=} \PYG{n}{stringify\PYGZus{}path}\PYG{p}{(}\PYG{n}{io}\PYG{p}{)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{823} \PYG{n+ne}{\PYGZhy{}\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{824} \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{\PYGZus{}reader} \PYG{o}{=} \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{\PYGZus{}engines}\PYG{p}{[}\PYG{n}{engine}\PYG{p}{]}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{\PYGZus{}io}\PYG{p}{)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{825} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{826} \PYG{k}{def} \PYG{n+nf}{\PYGZus{}\PYGZus{}fspath\PYGZus{}\PYGZus{}}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{)}\PYG{p}{:} \PYG{n+nn}{\PYGZti{}/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/pandas/io/excel/\PYGZus{}xlrd.py} in \PYG{n+ni}{\PYGZus{}\PYGZus{}init\PYGZus{}\PYGZus{}}\PYG{n+nt}{(self, filepath\PYGZus{}or\PYGZus{}buffer)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{19} \PYG{n}{err\PYGZus{}msg} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Install xlrd \PYGZgt{}= 1.0.0 for Excel support}\PYG{l+s+s2}{\PYGZdq{}} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{20} \PYG{n}{import\PYGZus{}optional\PYGZus{}dependency}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{xlrd}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{extra}\PYG{o}{=}\PYG{n}{err\PYGZus{}msg}\PYG{p}{)} \PYG{n+ne}{\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{21} \PYG{n+nb}{super}\PYG{p}{(}\PYG{p}{)}\PYG{o}{.}\PYG{n+nf+fm}{\PYGZus{}\PYGZus{}init\PYGZus{}\PYGZus{}}\PYG{p}{(}\PYG{n}{filepath\PYGZus{}or\PYGZus{}buffer}\PYG{p}{)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{22} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{23} \PYG{n+nd}{@property} \PYG{n+nn}{\PYGZti{}/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/pandas/io/excel/\PYGZus{}base.py} in \PYG{n+ni}{\PYGZus{}\PYGZus{}init\PYGZus{}\PYGZus{}}\PYG{n+nt}{(self, filepath\PYGZus{}or\PYGZus{}buffer)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{351} \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{book} \PYG{o}{=} \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{load\PYGZus{}workbook}\PYG{p}{(}\PYG{n}{filepath\PYGZus{}or\PYGZus{}buffer}\PYG{p}{)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{352} \PYG{k}{elif} \PYG{n+nb}{isinstance}\PYG{p}{(}\PYG{n}{filepath\PYGZus{}or\PYGZus{}buffer}\PYG{p}{,} \PYG{n+nb}{str}\PYG{p}{)}\PYG{p}{:} \PYG{n+ne}{\PYGZhy{}\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{353} \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{book} \PYG{o}{=} \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{load\PYGZus{}workbook}\PYG{p}{(}\PYG{n}{filepath\PYGZus{}or\PYGZus{}buffer}\PYG{p}{)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{354} \PYG{k}{elif} \PYG{n+nb}{isinstance}\PYG{p}{(}\PYG{n}{filepath\PYGZus{}or\PYGZus{}buffer}\PYG{p}{,} \PYG{n+nb}{bytes}\PYG{p}{)}\PYG{p}{:} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{355} \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{book} \PYG{o}{=} \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{load\PYGZus{}workbook}\PYG{p}{(}\PYG{n}{BytesIO}\PYG{p}{(}\PYG{n}{filepath\PYGZus{}or\PYGZus{}buffer}\PYG{p}{)}\PYG{p}{)} \PYG{n+nn}{\PYGZti{}/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/pandas/io/excel/\PYGZus{}xlrd.py} in \PYG{n+ni}{load\PYGZus{}workbook}\PYG{n+nt}{(self, filepath\PYGZus{}or\PYGZus{}buffer)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{34} \PYG{k}{return} \PYG{n}{open\PYGZus{}workbook}\PYG{p}{(}\PYG{n}{file\PYGZus{}contents}\PYG{o}{=}\PYG{n}{data}\PYG{p}{)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{35} \PYG{k}{else}\PYG{p}{:} \PYG{n+ne}{\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{36} \PYG{k}{return} \PYG{n}{open\PYGZus{}workbook}\PYG{p}{(}\PYG{n}{filepath\PYGZus{}or\PYGZus{}buffer}\PYG{p}{)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{37} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{38} \PYG{n+nd}{@property} \PYG{n+nn}{\PYGZti{}/opt/anaconda3/lib/python3.8/site\PYGZhy{}packages/xlrd/\PYGZus{}\PYGZus{}init\PYGZus{}\PYGZus{}.py} in \PYG{n+ni}{open\PYGZus{}workbook}\PYG{n+nt}{(filename, logfile, verbosity, use\PYGZus{}mmap, file\PYGZus{}contents, encoding\PYGZus{}override, formatting\PYGZus{}info, on\PYGZus{}demand, ragged\PYGZus{}rows)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{109} \PYG{k}{else}\PYG{p}{:} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{110} \PYG{n}{filename} \PYG{o}{=} \PYG{n}{os}\PYG{o}{.}\PYG{n}{path}\PYG{o}{.}\PYG{n}{expanduser}\PYG{p}{(}\PYG{n}{filename}\PYG{p}{)} \PYG{n+ne}{\PYGZhy{}\PYGZhy{}\PYGZgt{} }\PYG{l+m+mi}{111} \PYG{k}{with} \PYG{n+nb}{open}\PYG{p}{(}\PYG{n}{filename}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{rb}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{)} \PYG{k}{as} \PYG{n}{f}\PYG{p}{:} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{112} \PYG{n}{peek} \PYG{o}{=} \PYG{n}{f}\PYG{o}{.}\PYG{n}{read}\PYG{p}{(}\PYG{n}{peeksz}\PYG{p}{)} \PYG{g+gWhitespace}{ }\PYG{l+m+mi}{113} \PYG{k}{if} \PYG{n}{peek} \PYG{o}{==} \PYG{l+s+sa}{b}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{PK}\PYG{l+s+se}{\PYGZbs{}x03}\PYG{l+s+se}{\PYGZbs{}x04}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{:} \PYG{c+c1}{\PYGZsh{} a ZIP file} \PYG{n+ne}{FileNotFoundError}: [Errno 2] No such file or directory: \PYGZsq{}/Users/hjensen/Teaching/FYS\PYGZhy{}STK4150/doc/src/LectureNotes/default of credit card clients.xls\PYGZsq{} \end{sphinxVerbatim} \renewcommand{\indexname}{Index} \printindex \end{document}