<jupyter_start><jupyter_text>Standard Deviation-Normalization and Data Process<jupyter_code>std = np.std(X, ddof=1)
mean = np.mean(X)
X = (X - mean) / std

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state=None)
#print(X)<jupyter_output><empty_output><jupyter_text>sigmoid function<jupyter_code>def sigmoid(s):
    sigma = 1/(1+np.exp(-s))
    return sigma
def model(w,b,X):
    s = w*X+b
    return sigmoid(s)<jupyter_output><empty_output><jupyter_text>cost function(cross entropy) ![image.png](attachment:image.png)<jupyter_code>def cost_function(w,b, X, y):
    y_hat = model(w,b, X)
    loss = 0
    for i in range(y.size):
        loss = loss - (y[i]*np.log(y_hat[i])+(1-y[i])*np.log(1-y_hat[i]))
    return loss<jupyter_output><empty_output><jupyter_text>optimizerreferences:   [1]https://zhuanlan.zhihu.com/p/35709485  [2]https://www.cnblogs.com/xiaoma927/p/9716018.html   ![image.png](attachment:image.png) ![image.png](attachment:image.png)<jupyter_code>def optimizer(w,b,y,X,alpha):
    dw=0
    db=0
    for i in range(X.size):
        dw=dw+(sigmoid(w*X[i]+b)-y[i])*X[i]
        db=db+(sigmoid(w*X[i]+b)-y[i])
    dw=dw/X.size
    db=db/X.size
    
    return w-alpha*dw,b-alpha*db<jupyter_output><empty_output><jupyter_text>iterator<jupyter_code>def iterater(w,b,X,y,times,alpha):
    costs = []
    accs = []
    for i in range(times):
        costs.append(cost_function(w,b, X, y))
        accs.append(accuracy(w,b, X, y))
        w,b = optimizer(w,b,y,X,alpha)
    return w,b, costs, accs<jupyter_output><empty_output><jupyter_text>assessment<jupyter_code>def predict_proba(w,b, X):
    y_hat=model(w,b, X)
    return y_hat

def predict(X, w,b):
    y_hat=predict_proba(w,b,X)
    y_hard=(y_hat > 0.5) * 1
    return y_hard

def accuracy(w,b, X, y):
    y_hard=predict(X, w,b)
    count_right = 0
    for i in range(y_hard.size):
        count_right=count_right+(y_hard[i]==y[i])
    return count_right*1.0/y.size<jupyter_output><empty_output><jupyter_text>get the party started<jupyter_code>w = 1
b = 1
times = 150
w,b, costs, accs = iterater(w,b,X_train,y_train,times,0.1)
print(w)
print(b)<jupyter_output>0.09948224884956461
-1.7252261397322346<jupyter_text>costs descend graph<jupyter_code>print(costs[-1], accs[-1])

import matplotlib.pyplot as plt
plt.plot(range(times),costs)
plt.show()<jupyter_output>101.14226110672884 0.8857142857142857<jupyter_text>score<jupyter_code>accuracy(w,b, X_test, y_test)<jupyter_output><empty_output><jupyter_text>compared with sklearn<jupyter_code>from sklearn.linear_model import LogisticRegression
lr = LogisticRegression()
X_train=X_train.reshape(-1, 1)
y_train=y_train.reshape(-1, 1)
lr.fit(X_train, y_train.astype('int'))
lr.score(X_train, y_train.astype('int'))
lr.get_params()
test = np.array(range(50))
test=test.reshape(-1,1)
lr.predict(test)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16423103688239388, 'fraction_numerical': 0.04244954766875435, 'mean_word_length': 5.403118040089087, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 40, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '633986', 'license': 'MIT', 'n_tokens_mistral': 1341, 'n_tokens_neox': 1266, 'n_words': 168, 'path': 'cross_entropy.ipynb', 'repo_name': 'henry-bugfree/logistic_regression_with_cross_entropy', 'size': '2874'}