|
1 | 1 | {
|
2 | 2 | "cells": [
|
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## Naive Bayes Classifier Implementation using numpy\n", |
| 8 | + "\n", |
| 9 | + "Naive Bayes is anoother supervised machine laerning algorithm for classification problem. It makes a strong assumption about the data that **each feature is independent of the value of any other feature**. For example, a fruit may be considered to be an apple if it is red, round, and about 10 cm in diameter. A naive Bayes classifier considers each of these features to contribute independently to the probability that this fruit is an apple, regardless of any possible correlations between the color, roundness, and diameter features.\n", |
| 10 | + "\n", |
| 11 | + "In Naive bayes classifier what we are trying to find is the probability that a given data point belogs to a specific class, we are going to have prediction for all the class in our target.\n", |
| 12 | + "\n", |
| 13 | + "\n", |
| 14 | + "\n", |
| 15 | + "\n", |
| 16 | + "This is bernolli naive bayes impementation, which we expecting the features to be true or false (1 or 0)." |
| 17 | + ] |
| 18 | + }, |
3 | 19 | {
|
4 | 20 | "cell_type": "code",
|
5 |
| - "execution_count": 4, |
| 21 | + "execution_count": 72, |
6 | 22 | "metadata": {},
|
7 | 23 | "outputs": [],
|
8 | 24 | "source": [
|
|
21 | 37 | " return np.apply_along_axis(lambda x: self.compute_probs(x), 1, X)\n",
|
22 | 38 | " \n",
|
23 | 39 | " def compute_probs(self, x):\n",
|
24 |
| - " probs = np.array([self.compute_prob(x, y) for y in range(len(self.y_classes))])\n", |
| 40 | + " probs = [self.compute_prob(x, y) for y in range(len(self.y_classes))]\n", |
25 | 41 | " return self.y_classes[np.argmax(probs)]\n",
|
26 | 42 | " \n",
|
27 | 43 | " def compute_prob(self, x, y):\n",
|
|
31 | 47 | " res *= (Pxy**x[j])*((1-Pxy)**(1-x[j])) # p(xj=0|y)\n",
|
32 | 48 | " return res * self.phi_y[y]\n",
|
33 | 49 | " \n",
|
34 |
| - " def evaluate(self, X, y):\n", |
| 50 | + " def score(self, X, y):\n", |
35 | 51 | " return (self.predict(X) == y).mean()"
|
36 | 52 | ]
|
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "code", |
| 56 | + "execution_count": null, |
| 57 | + "metadata": {}, |
| 58 | + "outputs": [], |
| 59 | + "source": [] |
37 | 60 | }
|
38 | 61 | ],
|
39 | 62 | "metadata": {
|
|
0 commit comments