```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
#replace all test and train labels by 1 (for intercept term)


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

for i in range(len(train)):
    y_train[i]=train[i][8]
    train[i][8]=1
    
```


```python
%%time
#logistic regression using sklearn
from sklearn.linear_model import LogisticRegression
from sklearn import metrics


logreg = LogisticRegression()
logreg.fit(train, y_train)

y_pred = logreg.predict(test)

```

    Wall time: 186 ms



```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 = 97.99%
    No of false negatives = 57
    No of positives in test data = 332
    No of positives predicted : 290



```python
#using maxabs scaler



from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
train_scaled=scaler.fit_transform(train)
test_scaled=scaler.fit_transform(test)


logreg = LogisticRegression()
logreg.fit(train_scaled, y_train)

y_pred = logreg.predict(test_scaled)

#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 = 97.35%
    No of false negatives = 88
    No of positives in test data = 332
    No of positives predicted : 251


Own Implementation


```python
from scipy.optimize import fmin_tnc

theta = np.zeros((X.shape[1], 1))

def sigmoid(z):
    sigma = 1 / (1 + np.exp(-z)) 
    return sigma

def probability(theta, x):
    return sigmoid(np.dot(x, theta))

def cost_function(theta, x, y):
    cost = -np.sum(y*np.log(probability(theta, x)) + (1-y)*np.log(1 - probability(theta, x)))
    return cost

def gradient (theta, x, y): 
    return np.dot(x.T, probability(theta , x) - y)


def fit (x, y, theta):
    param = fmin_tnc(func=cost_function, x0=theta, fprime=gradient,args=(x, y.flatten()))
    return param[0]

parameters = fit(X, y, theta)
print (parameters)

def predict(test, y_test, theta):
    for i in range(len(test)):
        p = probability(parameters, test[i,:])
        if p>0.5:
            y_test[i] = 1
        else:
            y_test[i] = 0

```

    [ 3.65133667e-02 -3.80217159e-02  6.78890765e+00 -6.33306875e-01
     -2.47733463e-02  4.29645538e-02 -5.28755145e-03 -3.77353568e-03
     -9.14482752e+00]



```python
#predict
predict(test, y_pred, parameters)
```


```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 = 98.02%
    No of false negatives = 57
    No of positives in test data = 332
    No of positives predicted : 289


However we can decrease the number of false negatives if we predict 1 at p>0.4 instead of 0.5


```python
from scipy.optimize import fmin_tnc

theta = np.zeros((X.shape[1], 1))

def sigmoid(z):
    sigma = 1 / (1 + np.exp(-z)) 
    return sigma

def probability(theta, x):
    return sigmoid(np.dot(x, theta))

def cost_function(theta, x, y):
    cost = -np.sum(y*np.log(probability(theta, x)) + (1-y)*np.log(1 - probability(theta, x)))
    return cost

def gradient (theta, x, y): 
    return np.dot(x.T, probability(theta , x) - y)


def fit (x, y, theta):
    param = fmin_tnc(func=cost_function, x0=theta, fprime=gradient,args=(x, y.flatten()))
    return param[0]

parameters = fit(X, y, theta)
print (parameters)

def predict(test, y_test, theta):
    for i in range(len(test)):
        p = probability(parameters, test[i,:])
        if p>0.4:
            y_test[i] = 1
        else:
            y_test[i] = 0

            
```

    [ 3.65133667e-02 -3.80217159e-02  6.78890765e+00 -6.33306875e-01
     -2.47733463e-02  4.29645538e-02 -5.28755145e-03 -3.77353568e-03
     -9.14482752e+00]



```python
#predict
predict(test, y_pred, parameters)
```


```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 = 98.10%
    No of false negatives = 49
    No of positives in test data = 332
    No of positives predicted : 302



```python

```


```python

```

1cca9324419f95bd6561c519a646e1e01ef267de
ipynb
Jupyter Notebook
Logistic Regression/pulsar_Logistic.ipynb
UtsavMurarka/Pulsar-identification
521740ca6c16ef5d125e9c410fd1b6c46c0da1f9
MIT
2021-01-12T19:44:44.000Z
2021-01-12T19:44:44.000Z
Logistic Regression/pulsar_Logistic.ipynb
UtsavMurarka/Pulsar-identification
521740ca6c16ef5d125e9c410fd1b6c46c0da1f9
MIT
Logistic Regression/pulsar_Logistic.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