naive_bayes¶
ai.naive_bayes
¶
Naive Bayes methods are a set of supervised learning algorithms based on
applying Bayes’ theorem with the “naive” assumption of conditional
independence between every pair of features given the value of the class
variable.
ai.naive_bayes implements the following naive bayes algorithms:
ai.naive_bayes.naive_bayes.GaussianNB
GaussianNB
¶
Gaussian Naive Bayes (GaussianNB).
Naive Bayes methods are a set of supervised learning algorithms based on
applying Bayes’ theorem with the “naive” assumption of conditional
independence between every pair of features given the value of the class
variable. Bayes theorem states the following relationship, given class
variable y and dependent feature vector \(x_{1}\) through \(x_{n}\):
Using the naive conditional independence assumption that:
for all \(i\), this relationship is simplified to:
Since \(P(x_{1}, ..., x_{n})\) is constant given the input, we can use the following classification rule:
Note:
We never multiply probabilities in computer science since, the number can multiply to \(0\) upto the machine precision. It's better to use the monotonic function \(\mathrm{log}\) and add the log of the probabilities.
and we can use Maximum A Posteriori (MAP) estimation to estimate \(P(y)\) and \(P(x_{i} | y)\); the former is then the relative frequency of class \(y\) in the training set.
GaussianNB implements the Gaussian Naive Bayes algorithm for classification.
The likelihood of the features is assumed to be Gaussian:
The parameters \(\sigma_{y}\) and \(\mu_{y}\) are estimated using maximum likelihood.
Source code in ai/naive_bayes/naive_bayes.py
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | |
__init__(*, priors=None)
¶
Initializes model's parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
priors
|
Union[list, ndarray]
|
Prior probabilities of the classes. If specified, the priors are not adjusted according to the data. |
None
|
Source code in ai/naive_bayes/naive_bayes.py
fit(X, y)
¶
Fit Gaussian Naive Bayes according to X, y.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Training vectors, where |
required |
y
|
ndarray
|
Target values. |
required |
Returns:
| Type | Description |
|---|---|
GaussianNB
|
Returns the instance itself. |
Source code in ai/naive_bayes/naive_bayes.py
predict(X)
¶
Predict for X using the previously calculated priors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Testing vectors, where |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Predictions made for the given testing vector |
Source code in ai/naive_bayes/naive_bayes.py
naive_bayes
¶
GaussianNB
¶
Gaussian Naive Bayes (GaussianNB).
Naive Bayes methods are a set of supervised learning algorithms based on
applying Bayes’ theorem with the “naive” assumption of conditional
independence between every pair of features given the value of the class
variable. Bayes theorem states the following relationship, given class
variable y and dependent feature vector \(x_{1}\) through \(x_{n}\):
Using the naive conditional independence assumption that:
for all \(i\), this relationship is simplified to:
Since \(P(x_{1}, ..., x_{n})\) is constant given the input, we can use the following classification rule:
Note:
We never multiply probabilities in computer science since, the number can multiply to \(0\) upto the machine precision. It's better to use the monotonic function \(\mathrm{log}\) and add the log of the probabilities.
and we can use Maximum A Posteriori (MAP) estimation to estimate \(P(y)\) and \(P(x_{i} | y)\); the former is then the relative frequency of class \(y\) in the training set.
GaussianNB implements the Gaussian Naive Bayes algorithm for classification.
The likelihood of the features is assumed to be Gaussian:
The parameters \(\sigma_{y}\) and \(\mu_{y}\) are estimated using maximum likelihood.
Source code in ai/naive_bayes/naive_bayes.py
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | |
__init__(*, priors=None)
¶
Initializes model's parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
priors
|
Union[list, ndarray]
|
Prior probabilities of the classes. If specified, the priors are not adjusted according to the data. |
None
|
Source code in ai/naive_bayes/naive_bayes.py
fit(X, y)
¶
Fit Gaussian Naive Bayes according to X, y.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Training vectors, where |
required |
y
|
ndarray
|
Target values. |
required |
Returns:
| Type | Description |
|---|---|
GaussianNB
|
Returns the instance itself. |
Source code in ai/naive_bayes/naive_bayes.py
predict(X)
¶
Predict for X using the previously calculated priors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Testing vectors, where |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Predictions made for the given testing vector |