linear_model¶
ai.linear_model
¶
The term linear model implies that the model is specified as a linear combination of features. Based on training data, the learning process computes one weight for each feature to form a model that can predict or estimate the target value.
ai.linear_model module implements a variety of linear models:
ai.linear_model.linear.LinearRegressionai.linear_model.logistic.LogisticRegressionai.linear_model.perceptron.Perceptron
LinearRegression
¶
LinearRegression fits a linear model with coefficients w = (w1, ..., wp)
to minimize the residual sum of squares between the observed targets in the
dataset, and the targets predicted by the linear approximation.
Source code in ai/linear_model/linear.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
__init__(*, alpha=0.01, n_iters=1000)
¶
Initializes model's learning rate and number of iterations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float16
|
Model's learning rate. High value might over shoot the minimum loss, while low values might make the model to take forever to learn. |
0.01
|
n_iters
|
int
|
Maximum number of updations to make over the weights and bias in order to reach to a effecient prediction that minimizes the loss. |
1000
|
Source code in ai/linear_model/linear.py
fit(X, y)
¶
Fit the linear model on X given y.
Hypothesis function for our LinearRegression \(\hat y = b + wX\),
where b is the model's intercept and w is the coefficient of X.
The cost function or the loss function that we use is the Mean Squared Error
(MSE) between the predicted value and the true value. The cost function
(J) can be written as:
To achieve the best-fit regression line, the model aims to predict the
target value \(\hat Y\) such that the error difference between the
predicted value \(\hat Y\) and the true value \(Y\) is minimum. So,
it is very important to update the b and w values, to reach the best
value that minimizes the error between the predicted y value and the true
y value.
A linear regression model can be trained using the optimization algorithm
gradient descent by iteratively modifying the model’s parameters to reduce
the mean squared error (MSE) of the model on a training dataset. To update
b and w values in order to reduce the Cost function (minimizing RMSE
value) and achieve the best-fit line the model uses Gradient Descent. The
idea is to start with random b and w values and then iteratively update
the values, reaching minimum cost.
On differentiating cost function J with respect to b:
On differentiating cost function J with respect to w:
The above derivative functions are used for updating weights and bias in
each iteration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Training vectors, where |
required |
y
|
ndarray
|
Target vector. |
required |
Source code in ai/linear_model/linear.py
predict(X)
¶
Predict for X using the previously calculated weights and bias.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature vector. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Target vector. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
ValueError
|
If shape of the given |
Source code in ai/linear_model/linear.py
LogisticRegression
¶
Logistic Regression (aka logit) classifier.
The logistic regression model transforms the linear regression function continuous value output into categorical value output using a sigmoid function, which maps any real-valued set of independent variables input into a value between 0 and 1. This function is known as the logistic function.
Now we use the sigmoid function where the input will be z and we find the probability between 0 and 1. i.e predicted y.
Source code in ai/linear_model/logistic.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
__init__(alpha=0.01, n_iters=1000)
¶
Initializes model's learning rate and number of iterations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float16
|
Model's learning rate. High value might over shoot the minimum loss, while low values might make the model to take forever to learn. |
0.01
|
n_iters
|
int64
|
Maximum number of updations to make over the weights and bias in order to reach to a effecient prediction that minimizes the loss. |
1000
|
Source code in ai/linear_model/logistic.py
fit(X, y)
¶
Fit Logistic Regression according to X, y.
Hypothesis function for our LogisticRegression is the same as for the
LinearRegression \(\hat y = b + wX\), where b is the model's
intercept and w is the coefficient of X.
The cost function or the loss function that we use is the Mean Squared Error
(MSE) between the predicted value and the true value. The cost function
(J) can be written as:
To achieve the best-fit regression line, the model aims to predict the
target value \(\hat Y\) such that the error difference between the
predicted value \(\hat Y\) and the true value \(Y\) is minimum. So,
it is very important to update the b and w values, to reach the best
value that minimizes the error between the predicted y value and the true
y value.
A logistic regression model can be trained using the optimization algorithm
gradient descent by iteratively modifying the model’s parameters to reduce
the mean squared error (MSE) of the model on a training dataset. To update
b and w values in order to reduce the Cost function (minimizing RMSE
value) and achieve the best-fit line the model uses Gradient Descent. The
idea is to start with random b and w values and then iteratively update
the values, reaching minimum cost.
On differentiating cost function J with respect to b:
On differentiating cost function J with respect to w:
The above derivative functions are used for updating weights and bias in
each iteration.
The sigmoid function is then used for mapping the predictions between 0 and 1.
where \(z\) can be replaced with our hypothesis function \(\hat y = b + wX\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Training vectors, where |
required |
y
|
ndarray
|
Target values. |
required |
Returns:
| Type | Description |
|---|---|
LogisticRegression
|
Returns the instance itself. |
Source code in ai/linear_model/logistic.py
predict(X)
¶
Predict for X using the previously calculated weights and bias.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature vector. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Target vector. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
ValueError
|
If shape of the given |
Source code in ai/linear_model/logistic.py
Perceptron
¶
Perceptron classifier.
The perceptron is a simple supervised machine learning algorithm used to classify data into binary outcomes. It is a type of linear classifier, i.e. a classification algorithm that makes its predictions based on a linear predictor function combining a set of weights with the feature vector.
Source code in ai/linear_model/perceptron.py
__init__(*, alpha=np.float16(0.01), n_iters=np.int64(1000), random_state=1)
¶
Initializes model's learning rate and number of iterations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float16
|
Model's learning rate. High value might over shoot the minimum loss, while low values might make the model to take forever to learn. |
float16(0.01)
|
n_iters
|
int64
|
Maximum number of updations to make over the weights and bias in order to reach to a effecient prediction that minimizes the loss. |
int64(1000)
|
random_state
|
int
|
Seed to generate random weights and bias. |
1
|
Source code in ai/linear_model/perceptron.py
fit(X, y)
¶
Fit training data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Training vectors, where n_samples is the number of samples and n_features is the number of features. |
required |
y
|
ndarray
|
Target values. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
self |
Perceptron
|
An instance of self. |
Source code in ai/linear_model/perceptron.py
net_input(X)
¶
Calculate net input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Training vectors, where n_samples is the number of samples and n_features is the number of features. |
required |
Returns:
| Type | Description |
|---|---|
|
The dot product of |
Source code in ai/linear_model/perceptron.py
predict(X)
¶
Return class label after unit step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Training vectors, where n_samples is the number of samples and n_features is the number of features. |
required |
Returns:
| Type | Description |
|---|---|
|
The class label after unit step. |
Source code in ai/linear_model/perceptron.py
linear
¶
A numpy-compatible linear regression implementation.
Example:
```python
import numpy as np
from sklearn import datasets
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from ai.linear_model import LinearRegression
X, y = datasets.make_regression(
n_samples=100, n_features=1, noise=20, random_state=4
)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
model = LinearRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
print(accuracy_score(y_pred, y_test))
```
LinearRegression
¶
LinearRegression fits a linear model with coefficients w = (w1, ..., wp)
to minimize the residual sum of squares between the observed targets in the
dataset, and the targets predicted by the linear approximation.
Source code in ai/linear_model/linear.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
__init__(*, alpha=0.01, n_iters=1000)
¶
Initializes model's learning rate and number of iterations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float16
|
Model's learning rate. High value might over shoot the minimum loss, while low values might make the model to take forever to learn. |
0.01
|
n_iters
|
int
|
Maximum number of updations to make over the weights and bias in order to reach to a effecient prediction that minimizes the loss. |
1000
|
Source code in ai/linear_model/linear.py
fit(X, y)
¶
Fit the linear model on X given y.
Hypothesis function for our LinearRegression \(\hat y = b + wX\),
where b is the model's intercept and w is the coefficient of X.
The cost function or the loss function that we use is the Mean Squared Error
(MSE) between the predicted value and the true value. The cost function
(J) can be written as:
To achieve the best-fit regression line, the model aims to predict the
target value \(\hat Y\) such that the error difference between the
predicted value \(\hat Y\) and the true value \(Y\) is minimum. So,
it is very important to update the b and w values, to reach the best
value that minimizes the error between the predicted y value and the true
y value.
A linear regression model can be trained using the optimization algorithm
gradient descent by iteratively modifying the model’s parameters to reduce
the mean squared error (MSE) of the model on a training dataset. To update
b and w values in order to reduce the Cost function (minimizing RMSE
value) and achieve the best-fit line the model uses Gradient Descent. The
idea is to start with random b and w values and then iteratively update
the values, reaching minimum cost.
On differentiating cost function J with respect to b:
On differentiating cost function J with respect to w:
The above derivative functions are used for updating weights and bias in
each iteration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Training vectors, where |
required |
y
|
ndarray
|
Target vector. |
required |
Source code in ai/linear_model/linear.py
predict(X)
¶
Predict for X using the previously calculated weights and bias.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature vector. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Target vector. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
ValueError
|
If shape of the given |
Source code in ai/linear_model/linear.py
logistic
¶
LogisticRegression
¶
Logistic Regression (aka logit) classifier.
The logistic regression model transforms the linear regression function continuous value output into categorical value output using a sigmoid function, which maps any real-valued set of independent variables input into a value between 0 and 1. This function is known as the logistic function.
Now we use the sigmoid function where the input will be z and we find the probability between 0 and 1. i.e predicted y.
Source code in ai/linear_model/logistic.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
__init__(alpha=0.01, n_iters=1000)
¶
Initializes model's learning rate and number of iterations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float16
|
Model's learning rate. High value might over shoot the minimum loss, while low values might make the model to take forever to learn. |
0.01
|
n_iters
|
int64
|
Maximum number of updations to make over the weights and bias in order to reach to a effecient prediction that minimizes the loss. |
1000
|
Source code in ai/linear_model/logistic.py
fit(X, y)
¶
Fit Logistic Regression according to X, y.
Hypothesis function for our LogisticRegression is the same as for the
LinearRegression \(\hat y = b + wX\), where b is the model's
intercept and w is the coefficient of X.
The cost function or the loss function that we use is the Mean Squared Error
(MSE) between the predicted value and the true value. The cost function
(J) can be written as:
To achieve the best-fit regression line, the model aims to predict the
target value \(\hat Y\) such that the error difference between the
predicted value \(\hat Y\) and the true value \(Y\) is minimum. So,
it is very important to update the b and w values, to reach the best
value that minimizes the error between the predicted y value and the true
y value.
A logistic regression model can be trained using the optimization algorithm
gradient descent by iteratively modifying the model’s parameters to reduce
the mean squared error (MSE) of the model on a training dataset. To update
b and w values in order to reduce the Cost function (minimizing RMSE
value) and achieve the best-fit line the model uses Gradient Descent. The
idea is to start with random b and w values and then iteratively update
the values, reaching minimum cost.
On differentiating cost function J with respect to b:
On differentiating cost function J with respect to w:
The above derivative functions are used for updating weights and bias in
each iteration.
The sigmoid function is then used for mapping the predictions between 0 and 1.
where \(z\) can be replaced with our hypothesis function \(\hat y = b + wX\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Training vectors, where |
required |
y
|
ndarray
|
Target values. |
required |
Returns:
| Type | Description |
|---|---|
LogisticRegression
|
Returns the instance itself. |
Source code in ai/linear_model/logistic.py
predict(X)
¶
Predict for X using the previously calculated weights and bias.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Feature vector. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Target vector. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
ValueError
|
If shape of the given |
Source code in ai/linear_model/logistic.py
perceptron
¶
Perceptron
¶
Perceptron classifier.
The perceptron is a simple supervised machine learning algorithm used to classify data into binary outcomes. It is a type of linear classifier, i.e. a classification algorithm that makes its predictions based on a linear predictor function combining a set of weights with the feature vector.
Source code in ai/linear_model/perceptron.py
__init__(*, alpha=np.float16(0.01), n_iters=np.int64(1000), random_state=1)
¶
Initializes model's learning rate and number of iterations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float16
|
Model's learning rate. High value might over shoot the minimum loss, while low values might make the model to take forever to learn. |
float16(0.01)
|
n_iters
|
int64
|
Maximum number of updations to make over the weights and bias in order to reach to a effecient prediction that minimizes the loss. |
int64(1000)
|
random_state
|
int
|
Seed to generate random weights and bias. |
1
|
Source code in ai/linear_model/perceptron.py
fit(X, y)
¶
Fit training data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Training vectors, where n_samples is the number of samples and n_features is the number of features. |
required |
y
|
ndarray
|
Target values. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
self |
Perceptron
|
An instance of self. |
Source code in ai/linear_model/perceptron.py
net_input(X)
¶
Calculate net input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Training vectors, where n_samples is the number of samples and n_features is the number of features. |
required |
Returns:
| Type | Description |
|---|---|
|
The dot product of |
Source code in ai/linear_model/perceptron.py
predict(X)
¶
Return class label after unit step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Training vectors, where n_samples is the number of samples and n_features is the number of features. |
required |
Returns:
| Type | Description |
|---|---|
|
The class label after unit step. |