<jupyter_start><jupyter_text>Logistic Regression<jupyter_code># logistic regression
def sigmoid(x):
    return .5 * (1 + np.tanh(.5 * x))

X_train = X_train.T
X_test = X_test.T

x1 = X_train[0,:]
x2 = X_train[1,:]
x3 = X_train[2,:]
x4 = X_train[3,:]
x5 = X_train[4,:]
x6 = X_train[5,:]
x7 = X_train[6,:]
x8 = X_train[7,:]
x9 = X_train[8,:]
x10 = X_train[9,:]
x11 = X_train[10,:]
x12 = X_train[11,:]

z1 = X_test[0,:]
z2 = X_test[1,:]
z3 = X_test[2,:]
z4 = X_test[3,:]
z5 = X_test[4,:]
z6 = X_test[5,:]
z7 = X_test[6,:]
z8 = X_test[7,:]
z9 = X_test[8,:]
z10 = X_test[9,:]
z11 = X_test[10,:]
z12 =X_test[11,:]

# initialize
eta = 0.001
maxIter = 1000

w = np.random.randn(13)
a_predict = (w[0]+w[1]*x1+w[2]*x2+w[3]*x3 + w[4]*x4+ w[5]*x5 + w[6]*x6+w[7]*x7+ w[8]*x8+ w[9]*x9+ w[10]*x10+ w[11]*x11+ w[12]*x12)
y_predict = sigmoid(a_predict)

misclass = np.where(y_predict!= t_train)
misclass = np.random.permutation(misclass[0])

cost = np.zeros(maxIter)
cost[:] = np.nan

# implement (training)
for iter in range(0,maxIter):
    misclass = np.random.permutation(misclass)

    w = w - eta*np.sum((y_predict - t_train)*np.vstack([np.ones((1,N_train)), x1[misclass].reshape(1,-1),x2[misclass].reshape(1,-1),x3[misclass].reshape(1,-1),\
                                                        x4[misclass].reshape(1,-1),x5[misclass].reshape(1,-1),x6[misclass].reshape(1,-1),x7[misclass].reshape(1,-1),\
                                                        x9[misclass].reshape(1,-1),x8[misclass].reshape(1,-1),x10[misclass].reshape(1,-1),x11[misclass].reshape(1,-1),\
                                                        x12[misclass].reshape(1,-1)]), axis = 1)

    a_predict = (w[0]+w[1]*x1+w[2]*x2+w[3]*x3+w[4]*x4+w[5]*x5+w[6]*x6+w[7]*x7+w[8]*x8+w[9]*x9+w[10]*x10+w[11]*x11+w[12]*x12)
    y_predict = sigmoid(a_predict)
    
    misclass = np.where(y_predict!= t_train)[0]    

    cost[iter] = -np.sum(t_train*np.log(y_predict[misclass]) + (1 - t_train)*np.log(1 - y_predict[misclass]))

    if misclass.size == 0:
        break
        
        
# test 
test_predict = (w[0]+w[1]*z1+w[2]*z2+w[3]*z3+w[4]*z4+w[5]*z5+w[6]*z6+w[7]*z7+w[8]*z8+w[9]*z9+w[10]*z10+w[11]*z11+w[12]*z12)
test_y = sigmoid(test_predict)

# accuracy
y_predict[np.where(y_predict >= 0.5)] = 1
y_predict[np.where(y_predict <= 0.5)] = 0

test_y[np.where(test_y >= 0.5)] = 1
test_y[np.where(test_y <= 0.5)] = 0

accuracy[2,0] = np.sum(t_train == y_predict) / N_train # numpy-logistic, training
accuracy[2,1] = np.sum(t_test == test_y) / N_test # numpy-logistic, test
print(accuracy)

plt.figure()
plt.plot(cost)<jupyter_output>[[0.99634404 0.99384615]
 [0.9876852  0.98615385]
 [0.75370406 0.75461538]
 [0.         0.        ]]<jupyter_text>Perceptron<jupyter_code># numpy-perceptron
X_train = X_train.T
X_test = X_test.T

w = np.random.randn(13) # 12개의 hyperparameter
y_predict = (w[0]+w[1]*x1+w[2]*x2+w[3]*x3 + w[4]*x4+ w[5]*x5 + w[6]*x6+w[7]*x7+ w[8]*x8+ w[9]*x9+ w[10]*x10+ w[11]*x11+ w[12]*x12)

id_misclass = np.where(y_predict*tp_train < 0)
id_misclass = np.random.permutation(id_misclass[0])

eta = 0.01
maxIter = 100

cost = np.zeros(maxIter)
cost[:] = np.nan

for iter in range(0,maxIter):
    id_misclass = np.random.permutation(id_misclass)
    w = w + eta*np.array([1, x1[id_misclass[0]], x2[id_misclass[0]], x3[id_misclass[0]], x4[id_misclass[0]],x5[id_misclass[0]],\
                          x6[id_misclass[0]], x7[id_misclass[0]], x8[id_misclass[0]], x9[id_misclass[0]], x10[id_misclass[0]],\
                          x11[id_misclass[0]],x12[id_misclass[0]]])*tp_train[id_misclass[0]]
    y_predict = (w[0]+w[1]*x1+w[2]*x2+w[3]*x3 + w[4]*x4+ w[5]*x5 + w[6]*x6+w[7]*x7+ w[8]*x8+ w[9]*x9+ w[10]*x10+ w[11]*x11+ w[12]*x12)
    
    id_misclass = np.where(y_predict*tp_train < 0)[0]    
    cost[iter] = -np.sum(y_predict[id_misclass]*tp_train[id_misclass])    
    
    if id_misclass.size == 0:
        break

        
# test 
test_predict = (w[0]+w[1]*z1+w[2]*z2+w[3]*z3+w[4]*z4+w[5]*z5+w[6]*z6+w[7]*z7+w[8]*z8+w[9]*z9+w[10]*z10+w[11]*z11+w[12]*z12)

c1_train = np.where(y_predict >= 0)
c0_train = np.where(y_predict <= 0)

# accuracy
y_predict[np.where(y_predict <= 0)] = -1
y_predict[np.where(y_predict >= 0)] = 1

test_predict[np.where(test_predict <= 0)] = -1
test_predict[np.where(test_predict >= 0)] = 1

accuracy[3,0] = np.sum(tp_train == y_predict) / N_train  # numpy-perceptron, training
accuracy[3,1] = np.sum(tp_test == test_predict) / N_test # numpy-perceptron, test
print(accuracy)

plt.figure()
plt.plot(cost)
# bar plot
xloc = np.arange(4)
width = 0.2

plt.figure()
plt.bar(xloc - 0.5*width, accuracy[:,0], width)
plt.bar(xloc + 0.5*width, accuracy[:,1], width)
plt.xticks(xloc,['sklearn-SVM','sklearn-logistic','numpy-logistic','numpy-perceptron'])
plt.ylabel('accuracy [%]')
plt.legend(['training','test'])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2214418508572609, 'fraction_numerical': 0.09192315637265028, 'mean_word_length': 4.129237288135593, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '170362', 'license': 'MIT', 'n_tokens_mistral': 2525, 'n_tokens_neox': 2319, 'n_words': 291, 'path': 'Homework03.ipynb', 'repo_name': 'shimdol22/DGU-2021-02-Datascience-ML', 'size': '4841'}