more update

This commit is contained in:
mhjensen
2018-05-25 11:00:43 -04:00
parent 3f3955e45b
commit ccccc6ed1b
+20 -3
View File
@@ -140,8 +140,24 @@ formats, ipython notebooks, latex files, pdf files etc with minimal edits.
!split
===== Simple linear regression model using _scikit-learn_ =====
We start with perhaps our simplest possible example, using _scikit-learn_ to perform linear regression analysis on a data set produced by us.
What follows is a simple Python code where we have defined function $y$ in terms of the variable $x$. Both are defined as vectors of dimension $1\times 100$. The entries to 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).
The Numpy functions are imported used the
What follows is a simple Python code where we have defined function $y$ in terms of the variable $x$. Both are defined as vectors of dimension $1\times 100$. The entries to 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 _import numpy as np_ statement and the
random number generator for the uniform distribution is called using the function
_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.
Using the Numpy function _randn()_ we can compute random numbers with the normal distribution (mean value equal to zero and variance set to one) and produce the values of $y$
assuming a linear dependence as function of $x$
!bt
\[
y = 2*x+N(0,1),
\]
!et
where $N(0,1)$ represents random numbers generated by the normal distribution.
From _scikit_learn_ we import then the _LinearRegression_ functionality and make a prediction $\tilde{y} = \alpha + \beta x$ using the function _fit(x,y)_
We make also a prediction
!bc pycod
# Importing various packages
import numpy as np
@@ -149,7 +165,7 @@ import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
x = np.random.rand(100,1)
y = 4+3*x+np.random.randn(100,1)
y = 2*x+np.random.randn(100,1)
linreg = LinearRegression()
linreg.fit(x,y)
xnew = np.array([[0],[2]])
@@ -164,6 +180,7 @@ plt.title(r'Random numbers ')
plt.show()
!ec
Add paly around with various forms, also relative error, plot that and other ways to estimate by the eye the qaulity of the fit
!split
===== Introduction to Jupyter notebook and available tools =====