Skip to content

Instantly share code, notes, and snippets.

View sunilkumardash9's full-sized avatar
⚔️

Sunil Kumar Dash sunilkumardash9

⚔️
View GitHub Profile
import numpy as np
from numpy import log,dot,exp,shape
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
X,y = make_classification(n_featues=4)
from sklearn.model_selection import train_test_split
X_tr,X_te,y_tr,y_te = train_test_split(X,y,test_size=0.1
def standardize(X_tr):
for i in range(shape(X_tr)[1]):
def F1_score(y,y_hat):
tp,tn,fp,fn = 0,0,0,0
for i in range(len(y)):
if y[i] == 1 and y_hat[i] == 1:
tp += 1
elif y[i] == 1 and y_hat[i] == 0:
fn += 1
elif y[i] == 0 and y_hat[i] == 1:
fp += 1
elif y[i] == 0 and y_hat[i] == 0:
def predict(self,X):
z = dot(self.initialize(X)[1],self.weights)
lis = []
for i in self.sigmoid(z):
if i>0.5:
lis.append(1)
else:
lis.append(0)
return lis
def fit(self,X,y,alpha=0.001,iter=400):
weights,X = self.initialize(X)
cost_list = np.zeros(iter,)
for i in range(iter):
weights = weights-alpha*dot(X.T,self.sigmoid(dot(X,weights))-np.reshape(y,(len(y),1)))
cost_list[i] = cost(weights)
self.weights = weights
return cost_list
def cost_func(self,theta):
z = dot(X,theta)
cost0 = y.T.dot(log(self.sigmoid(z)))
cost1 = (1-y).T.dot(log(1-self.sigmoid(z)))
cost = -(sum(cost1 + cost0))/len(y)
return cos
def sigmoid(self,X,weights):
z = np.dot(X,weights)
sig = 1/(1+np.e**(-z))
return sig
fig,ax = plt.subplots(figsize=(12,8))
ax.set_ylabel('J(cost)')
ax.set_xlabel('iterations')
x = ax.plot(range(400),a,'b.')
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import f1_score
model = LogisticRegression().fit(X_tr,y_tr)
y_pred = model.predict(X_te)
print(f1_score(y_te,y_pred))
from sklearn.datasets import make_classification
X,y = make_classification(n_features=4)
#spliting train,test data
from sklearn.model_selection import train_test_split
X_tr,X_te,y_tr,y_te = train_test_split(X,y,test_size=0.15)
def initialize(self,X):
weights = np.zeros((shape(X)[1]+1,1))
X = np.c_[np.ones((shape(X)[0],1)),X]
return weights,X