```python
import numpy as np
import sympy as sp
import pandas as pd
import math
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split


data=pd.read_csv('C:\\Users\\Utsav\\Desktop\\pulsar_prediction\\pulsar_stars.csv', sep=',',header=0)


pulsarData=data.values
pulsarData=np.array(pulsarData)
```


```python
#split the data

train, test = train_test_split(data, test_size=0.2)
pulsar=np.array(train)
train=np.array(train)
test=np.array(test)

print(len(test))
print(len(train))
#print(train)
```

    3580
    14318



```python
#copy labels of test & train data elsewhere
y_train=np.zeros(len(train))
y_test=np.zeros(len(test))
for i in range(len(test)):
    y_test[i]=test[i][8]
    test[i][8]=-1
#replace all test and train labels by -1
for i in range(len(train)):
    y_train[i]=train[i][8]
    train[i][8]=-1
    
```


```python
from sklearn.naive_bayes import GaussianNB
gnb = GaussianNB()
naiveBayes = gnb.fit(train, y_train)
y_pred = naiveBayes.predict(test)

```


```python
# calculate score

score=0
for i in range(len(test)):
    if y_pred[i]==y_test[i]:
        score=score+1
score=score*100/len(test)

print("Accuracy = " + "%.2f" % score + "%")


#calculate no of flase negatives
score=0

for i in range(len(test)):
    if y_pred[i]==0 and y_test[i]==1:
        score=score+1

        
#score=score*100/len(test)
percent = score/len(test)


print("No of false negatives = %d" % score)
print("No of positives in test data = %d" % sum(y_test))
print("No of positives predicted : %d" % sum(y_pred))
```

    Accuracy = 94.55%
    No of false negatives = 50
    No of positives in test data = 314
    No of positives predicted : 409


Own Implementation


```python
train=np.array(train)
NegativeCount=0
PositiveCount=0
for i in range(14318):
    if pulsar[i][8]==0:
        NegativeCount=NegativeCount+1
    if pulsar[i][8]==1:
        PositiveCount=PositiveCount+1
print(NegativeCount)
print(PositiveCount)
print(PositiveCount+NegativeCount)

positives=np.zeros((PositiveCount,9))
negatives=np.zeros((NegativeCount,9))
    
j=0
k=0
    
for i in range(14318):

    if(pulsar[i,8] == 1):
        positives[j,:] = pulsar[i,:]
        j=j+1
    if(pulsar[i,8] == 0):
        negatives[k,:] = pulsar[i,:]
        k=k+1

meanMatrixP = np.zeros(8)

for i in range(8):
    meanMatrixP[i] = np.mean(positives[:,i])

varMatrixP = np.zeros(8)

for i in range(8):
    varMatrixP[i] = np.var(positives[:,i])
    
    

meanMatrixNP = np.zeros(8)

for i in range(8):
    meanMatrixNP[i] = np.mean(negatives[:,i])

varMatrixNP = np.zeros(8)

for i in range(8):
    varMatrixNP[i] = np.var(negatives[:,i])


```

    12993
    1325
    14318



```python
print(meanMatrixP)
print(varMatrixP)
print(meanMatrixNP)
print(varMatrixNP)
```

    [56.46443396 38.68962562  3.15917187 15.78174757 50.15509686 56.67265959
      2.72705344 17.43102427]
    [ 902.9684607    65.5988982     3.56288583  201.32374846 2045.3054129
      390.33028281    9.28765632 2112.73912716]
    [116.59501991  47.34497034   0.21020221   0.38037675   8.87940773
      23.24627826   8.85398831 113.23625212]
    [3.06486622e+02 3.81434314e+01 1.12921431e-01 1.08675296e+00
     6.02750879e+02 2.74519094e+02 1.77832335e+01 1.11451932e+04]



```python
#make predictions:

y_pred1 = np.zeros(len(test))
print(sum(y_pred))

p_prob = PositiveCount/(PositiveCount+NegativeCount)
np_prob = NegativeCount/(PositiveCount+NegativeCount)
print(p_prob)
print(np_prob)

for i in range(len(test)):
    prod_pulsar=1
    for p in range(8):
        prod_pulsar = prod_pulsar*np.exp((-(test[i,p]-meanMatrixP[p])**2)/(2*varMatrixP[p]))/(np.sqrt(2*3.14*varMatrixP[p]))
    pulsar_prob = p_prob * (prod_pulsar)
    

    prod_nonpulsar=1
    for nonp in range(8):
        prod_nonpulsar = prod_nonpulsar*np.exp((-(test[i,nonp]-meanMatrixNP[nonp])**2)/(2*varMatrixNP[nonp]))/(np.sqrt(2*3.14*varMatrixNP[nonp]))
    nonpulsar_prob = np_prob * (prod_nonpulsar)
    
    if (pulsar_prob>=nonpulsar_prob):
        y_pred1[i] = 1
    else:
        y_pred1[i] = 0
    

```

    409.0
    0.09254085766168459
    0.9074591423383154



```python
# calculate score

score=0
for i in range(len(test)):
    if y_pred1[i]==y_test[i]:
        score=score+1
score=score*100/len(test)

print("Accuracy = " + "%.2f" % score + "%")


#calculate no of flase negatives
score=0

for i in range(len(test)):
    if y_pred1[i]==0 and y_test[i]==1:
        score=score+1

        
#score=score*100/len(test)
percent = score/len(test)


print("No of false negatives = %d" % score)
print("No of positives in test data = %d" % sum(y_test))
print("No of positives predicted : %d" % sum(y_pred1))
```

    Accuracy = 94.55%
    No of false negatives = 50
    No of positives in test data = 314
    No of positives predicted : 409



```python

```


```python

```

72adeb13afe629d436c58989ebc668ad0cec5113
ipynb
Jupyter Notebook
Naive Bayes/NaiveBayes.ipynb
UtsavMurarka/Pulsar-identification
521740ca6c16ef5d125e9c410fd1b6c46c0da1f9
MIT
2021-01-12T19:44:44.000Z
2021-01-12T19:44:44.000Z
Naive Bayes/NaiveBayes.ipynb
UtsavMurarka/Pulsar-identification
521740ca6c16ef5d125e9c410fd1b6c46c0da1f9
MIT
Naive Bayes/NaiveBayes.ipynb
UtsavMurarka/Pulsar-identification
521740ca6c16ef5d125e9c410fd1b6c46c0da1f9
MIT
2020-04-29T08:57:53.000Z
2020-04-29T08:57:53.000Z
Qwen/Qwen-72B
1. YES
2. YES