<jupyter_start><jupyter_text>Logistic 回归 (Logistic Regression)* 目标：实现 Logistic 回归，* 背景：你要卖房，想要知道怎样定价比较好。方案：收集最近的售房信息，做一个房价模型。* 数据：ex1data2.txt 文件。第一列： 房子面积，第二列：房间数，第三列：房价。<jupyter_code>%matplotlib

import numpy as np
import matplotlib.pyplot as plt<jupyter_output>Using matplotlib backend: MacOSX<jupyter_text>1 Logistic 回归* 目标：实现 Logistic 回归模型，用于预测学生是被大学录取* 背景：你是学校部门的管理，想要通过申请人的两个测试的结果来决定他是否有机会被录取。你拥有之前申请人的历史数据，可以当作 Logistic 回归的训练集。方案：构建一个分类模型，通过申请人两次测试的分数来评估一个他被录取的概率。* 数据：ex2data1.txt 文件。第一列：第一次测试的分数，第二列：第二次测试的分数，第三列：是否被录取。<jupyter_code># 该函数用于将数据集从CSV数据文件中读入
def loadcsv(filename):
    f = open(filename, 'rb')
    return np.loadtxt(f, delimiter=',')
# ex2data1.txt - Logistic 回归的数据集, csv, (100, 3)
data = loadcsv('ex2data1.txt')
X = data[:,0:2]
y = data[:,2:3]<jupyter_output><empty_output><jupyter_text>1.1 可视化数据绘制数据散点图，该数据有三个属性需要绘制(测试一成绩、测试二成绩和是否录取)<jupyter_code># 该函数用于描绘该训练数据集的散点图
def plotData(X, y):
    pos = (y == 1).flatten('F')
    neg = (y == 0).flatten('F')
    plt.plot(X[pos, 0:1], X[pos, 1:2], 'k+', linewidth=2,
             markersize=7, label='Admitted')
    plt.plot(X[neg, 0:1], X[neg, 1:2], 'ko', markerfacecolor='y',
             markersize=7, label='Not admitted')
    plt.xlabel('Exam 1 score')
    plt.ylabel('Exam 2 score')
    plt.legend()

plt.figure(1)
plotData(X, y)
plt.title('Figure 1: Scatter plot of training data')
plt.show()
# plt.pause(0)<jupyter_output><empty_output><jupyter_text>1.2 实现 1.2.1 sigmoid 函数Logistic 回归 hypothesis 定义：$$h(x; \theta) = g(\theta^Tx)$$sigmoid 函数定义：$$g(z) = \frac{1}{1+e^{-z}}$$<jupyter_code># sigmoid 函数实现
def sigmoid(z):
    return 1 / (1 + np.exp(-z))
sigmoid(0)<jupyter_output><empty_output><jupyter_text>1.2.2 损失函数和梯度Logistic 回归的目标函数：$$J(\theta)=\frac{1}{m}\sum_{i=1}^mL_{nll}(\sigma(\theta^Tx^{(i)}),y^{(i)})$$负对数似然函数：$$L_{nll}(g,a) = -a\log g - (1-a)\log(1-g))$$梯度：$$\frac{\partial{J(\theta)}}{\partial{\theta_j}} = \frac{1}{m}\sum_{i=1}^m(\sigma(\theta^Tx^{(i)})-y^{(i)})x_j^{(i)}$$<jupyter_code>m, n = X.shape

X_aug = np.concatenate((np.ones((m, 1)), X), 1)

initial_theta = np.zeros((n + 1, 1))<jupyter_output><empty_output><jupyter_text>计算梯度<jupyter_code># 同时计算损失和梯度
def costFunction(theta, X, y):
    h = sigmoid(X @ theta)
    J = np.sum((-y) * np.log(h) - (1-y) * np.log(1 - h)) / m
    grad = X.T @ (h - y) / m
    return J, grad
cost, grad = costFunction(initial_theta, X_aug, y)
print('Cost at initial theta (zeros): %f' % cost)
np.disp('Gradient at initial theta (zeros:)')
np.disp(grad)

test_theta = np.array([[-24], [0.2], [0.2]])
cost, grad = costFunction(test_theta, X_aug, y)
print('Cost at non-zero test theta: %f' % cost)
np.disp('Gradient at non-zero theta:')
np.disp(grad)<jupyter_output>Gradient at non-zero theta:
[[0.04290299]
 [2.56623412]
 [2.64679737]]
{'fraction_non_alphanumeric': 0.20239390642002175, 'fraction_numerical': 0.034457743924555674, 'mean_word_length': 5.661835748792271, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '541337', 'license': 'MulanPSL-1.0', 'n_tokens_mistral': 1487, 'n_tokens_neox': 1532, 'n_words': 205, 'path': 'ml_notes/stanford/ex2/ex2.ipynb', 'repo_name': 'ZaynChen/notes', 'size': '2757'}