<jupyter_start><jupyter_text>Logistic Regression<jupyter_code>def logit(prediction):
    return 1.0 / (1 + np.exp(-prediction))
def CalculateLossAcc(X, y, w, b):
    prediction = logit(X.dot(w.T) + b)
    loss = 0
    for i in range(len(y)):
        if y[i] == 0:
            loss -= np.log(1-prediction[i])
        else:
            loss -= np.log(prediction[i])
    loss = 1.0 * loss / len(X)
    prediction[prediction > 0.5] = 1
    prediction[prediction <= 0.5] = 0
    acc = 1.0 * np.sum([a == b for (a, b) in zip(prediction, y)]) / len(y)
    return loss, acc
from sklearn import linear_model
losslogistic = np.zeros(3000)
acclogistic = np.zeros(3000)
for i in range(3000):
    logistic = linear_model.LogisticRegression(C = 1e10, max_iter = i, solver = 'sag')
    logistic.fit(data,labels)
    weight = logistic.coef_
    bias = logistic.intercept_
    iteration = logistic.n_iter_
    loss, acc = CalculateLossAcc(data, labels, weight, bias)
    losslogistic[i] = loss[0]
    acclogistic[i] = acc
import matplotlib
import matplotlib.pyplot as plt

x = [i for i in range(len(losslogistic))]
plt.figure(figsize = (8,6))
plt.plot(x, losslogistic)
plt.xlabel('# of iteration')
plt.ylabel('loss')
plt.title('Loss of Logistic Regression')
plt.show()
x = [i for i in range(len(acclogistic))]
plt.figure(figsize = (8,6))
plt.plot(x, acclogistic)
plt.xlabel('# of iteration')
plt.ylabel('Accuracy')
plt.title('Accuracy of Logistic Regression')
plt.show()<jupyter_output><empty_output><jupyter_text>My Coordinate Descent Method<jupyter_code>import numpy as np
from sklearn import linear_model
class LogisticRegressionWithGoordinateDescent():
    def __init__(self, X, y, lr = 1e-3, maxIter = 1000, random = False):
        self.X = self.addBias(X)
        self.y = y
        self.lr = lr
        self.w = np.zeros((len(self.X[0]),1))
        self.maxIter = maxIter
        self.random = random
        self.loss = []
        self.acc = []
        
    def addBias(self, X):
        return np.insert(X, 0, 1, axis = 1)
    
    def logit(self,prediction):
        return 1.0 / (1 + np.exp(-prediction))
    
    def findIndex(self, derE):
        if self.random:
            return np.random.randint(0, 13)
        else:
            return np.argmax(np.abs(derE))
    
    def CalculateLossAcc(self):
        prediction = self.logit(self.X.dot(self.w))
        loss = 0
        for i in range(len(self.y)):
            if self.y[i] == 0:
                loss -= np.log(1 - prediction[i])
            else:
                loss -= np.log(prediction[i])
        loss = 1.0 * loss / len(self.y)
        prediction[prediction > 0.5] = 1
        prediction[prediction <= 0.5] = 0
        acc = 1.0 * np.sum([a == b for (a, b) in zip(prediction, self.y)]) / len(self.y)
        return loss, acc
    
    def fit(self):
        it = 0
        while it < self.maxIter:
            prediction = self.logit(self.X.dot(self.w))
            derE = np.sum((prediction - self.y) * self.X, axis=0) / len(self.X)
            wIndex = self.findIndex(derE)
            self.w[wIndex] = self.w[wIndex] - self.lr * derE[wIndex]
            loss, acc = self.CalculateLossAcc()       
            print 'loss = %8.6f' % loss
            print 'acc = %5.2f' % acc
            self.loss.append(loss)
            self.acc.append(acc)       
            it += 1
cd = LogisticRegressionWithGoordinateDescent(data, labels, lr = 1e-5, maxIter = 3000)
cd.fit()
print (len(cd.loss))
print max(cd.acc)
x = [i for i in range(len(cd.loss))]
plt.figure(figsize = (8,6))
plt.plot(x, cd.loss)
plt.xlabel('# of iteration')
plt.ylabel('loss')
plt.title('Loss of Coordinate Descent')
plt.show()
x = [i for i in range(len(cd.acc))]
plt.figure(figsize = (8,6))
plt.plot(x, cd.acc)
plt.xlabel('# of iteration')
plt.ylabel('Accuracy')
plt.title('Accuracy of Coordinate Descent')
plt.show()<jupyter_output><empty_output><jupyter_text>Uniformly Random Coordinate Descent<jupyter_code>cd1 = LogisticRegressionWithGoordinateDescent(data, labels, lr = 1e-4, maxIter = 3000, random = True)
cd1.fit()
x = [i for i in range(len(cd1.loss))]
plt.figure(figsize = (8,6))
plt.plot(x, cd1.loss)
plt.xlabel('# of iteration')
plt.ylabel('loss')
plt.title('Loss of Random Select')
plt.show()
x = [i for i in range(len(cd1.acc))]
plt.figure(figsize = (8,6))
plt.plot(x, cd1.acc)
plt.xlabel('# of iteration')
plt.ylabel('Accuracy')
plt.title('Accuracy of Random Select')
plt.show()
x = [i for i in range(len(cd1.loss))]
plt.figure(figsize = (8,6))
rsoss, = plt.plot(x, cd1.loss)
cdloss, = plt.plot(x, cd.loss)
lrloss, = plt.plot(x, losslogistic)
plt.legend([logloss, cdloss, lrloss], ['Random Select', 'Coordinate Descent', 'Logistic Regression'])
plt.grid(True)
plt.xlabel('# of iteration')
plt.ylabel('loss')
plt.title('Loss of three Algorithm')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15550487248600456, 'fraction_numerical': 0.02135600248807796, 'mean_word_length': 2.9476268412438626, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '522093', 'license': 'MIT', 'n_tokens_mistral': 1900, 'n_tokens_neox': 1788, 'n_words': 410, 'path': 'PA2-Coordinate descent/.ipynb_checkpoints/CSE250B PA2 Pin Tian-checkpoint.ipynb', 'repo_name': 'yrbszhsh/CSE250B-UCSD', 'size': '4823'}