<jupyter_start><jupyter_text>一、逻辑回归（二分类）---  读取数据并可视化<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv('ex2data1.txt',names = ['exam 1 score','exam 2 score','admitted?'])
data.head()
df = data.copy()
col = df.shape[1]
x = df.iloc[:,0:col - 1]
y = df.iloc[:,col - 1 :col]
X = x.copy()
X.insert(0,'one',1)
x = np.array(x)
y = np.array(y)
X = np.array(X)
x_1 = x[:,0]
x_2 = x[:,1]
x,y,X,x_1,x_2,x_1,y.shape
"""将原数据分组"""
pos = []
neg = []
for i in range(x.shape[0]):#选出正类和负类的索引
    if y[i] == 1:
        pos.append(i)
    else:
        neg.append(i)
pos_x_1 = x_1[pos]#一维矩阵不能用于scatter中，要转换为一维数组或列表
pos_x_2 = x_2[pos]
neg_x_1 = x_1[neg]
neg_x_2 = x_2[neg]
fig,ax = plt.subplots()
ax.scatter(pos_x_1,pos_x_2,label = 'admitted',marker = 'o')
ax.scatter(neg_x_1,neg_x_2,label = 'not admitted',marker= 'x')
ax.legend(loc = 1)<jupyter_output><empty_output><jupyter_text>sigmoid函数<jupyter_code>def sigmoid(z):
    g = 1/(1+np.exp(-z))
    return g
n = np.arange(-10.,10.,0.1)
sigmoid(n)
fig = plt.figure()
plt.plot(n,sigmoid(n))
theta = np.zeros([3,1])<jupyter_output><empty_output><jupyter_text>代价函数<jupyter_code>theta = np.zeros([3,1])
def costfunction(X,y,theta):
    cost_pos = -y * np.log(sigmoid(X @ theta))
    cost_neg = -(1-y) * np.log(sigmoid(X @ theta))
    cost = 1/len(X)*(cost_pos + cost_neg)
    return np.sum(cost)
costfunction(X,y,theta)
y.shape,X.shape<jupyter_output><empty_output><jupyter_text>梯度下降<jupyter_code>"""一个梯度步长"""
def gradient(theta,X,y):
    h = X @ theta
    erro = sigmoid(h) - y
    grad = (erro.T @ X)/len(X)
    return grad.T
gradient(theta,X,y)
"""迭代10000次"""
costs = [costfunction(X,y,theta)]
for i in range(10000):
    temp = theta - 0.01*gradient(theta,X,y)
    theta = temp
    costs.append(costfunction(X,y,theta))
costs
fig  =plt.figure()
plt.plot(costs)
theta
##在alpha=0.01 迭代10000次后，代价来回跳动，依然很难收敛
fig = plt.figure()
plt.plot(sigmoid(X @ theta))
# import scipy.optimize as opt
# result = opt.fmin_tnc(func=costfunction, x0=theta, fprime=gradient, args=(X,y))
"""用sklearn中的逻辑回归"""
import sklearn.linear_model as lm
classfiser =lm.LogisticRegression(solver='liblinear', C=100)
y = y.ravel()
classfiser.fit(x,y)
x_min,x_max = min(X[:,1]) - 1.0, max(X[:,1]) + 1.0
y_min,y_max = min(X[:,2]) - 1.0, max(X[:,2]) + 1.0
x_values,y_values = np.meshgrid(np.arange(x_min,x_max,0.01),np.arange(y_min,y_max,0.1))
mesh_output = classfiser.predict(np.c_[x_values.ravel(),y_values.ravel()])
mesh_output = mesh_output.reshape(x_values.shape)
plt.figure()
plt.pcolormesh(x_values,y_values,mesh_output,cmap=plt.cm.gray)
plt.scatter(pos_x_1,pos_x_2,label = 'admitted',marker = 'o')
plt.scatter(neg_x_1,neg_x_2,label = 'not admitted',marker= 'x')
plt.show()<jupyter_output><empty_output><jupyter_text>二、逻辑回归正则化<jupyter_code>data2 = pd.read_csv("ex2data2.txt",names = ["text1","text2","quality"])
data2.head()
pos = data2[data2['quality'].isin([1])]
neg = data2[data2['quality'].isin([0])]

fig, ax = plt.subplots()
ax.scatter(pos['text1'], pos['text2'], s=50, c='b', marker='o', label='y = 1')
ax.scatter(neg['text1'], neg['text2'], s=50, c='r', marker='x', label='y = 0')
ax.legend()
ax.set_xlabel('text1')
ax.set_ylabel('text2')
plt.show()<jupyter_output><empty_output><jupyter_text>特征映射<jupyter_code>df2 = data2.copy()
x1 = df2["text1"]
x2 = df2["text2"]
x = np.array(df2.iloc[:,0:2])
def feature_map(x1,x2,power): 
    for i in np.arange(power+1):
        for j in np.arange(power+1):
            df2['F' + str(i) + str(j)] = np.power(x1, i) * np.power(x2, j)
feature_map(x1,x2,power = 6)
df2.drop("text1",axis = 1,inplace = True)
df2.drop("text2",axis = 1,inplace = True)
df2.head()<jupyter_output><empty_output><jupyter_text>正则化代价函数<jupyter_code>col = df2.shape[1]
X = df2.iloc[:,1:col]
X.insert(0,'one',1)
X = np.array(X)
y = np.array(df2.iloc[:,0:1])
theta = np.zeros(X.shape[1]).reshape(X.shape[1],1)
X.shape,y.shape,theta.shape
x
def costfunctionreg(X,y,theta,c=1):
    reg = c/len(X)*np.sum(theta[1:,:]**2)
    return costfunction(X,y,theta)+reg
costfunction(X,y,theta)<jupyter_output><empty_output><jupyter_text>正则化剃度下降<jupyter_code>"""正则化梯度步长"""
def gradientreg(theta,X,y,c=1):
    reg = np.zeros((X.shape[1],1))
    for i in range(X.shape[1]):
        if i == 0:
            reg[i] = 0
        else:
            reg[i] = c/len(X)*theta[i,:]
    return gradient(theta,X,y) + reg
gradientreg(theta,X,y,c=1)
# """迭代10000次"""
# costs = [costfunction(X,y,theta)]
# for i in range(10000):
#     temp = theta - 0.01*gradient(theta,X,y)
#     theta = temp
#     costs.append(costfunction(X,y,theta))
# costs
import sklearn.linear_model as lm
classfiser =lm.LogisticRegression(solver='liblinear', C=100)
y = y.ravel()
classfiser.fit(x,y)
x_min,x_max = min(x1 - 1.0), max(x1 + 1.0)
y_min,y_max = min(x2 - 1.0), max(x2 + 1.0)
x_values,y_values = np.meshgrid(np.arange(x_min,x_max,0.01),np.arange(y_min,y_max,0.1))
mesh_output = classfiser.predict(np.c_[x_values.ravel(),y_values.ravel()])
mesh_output = mesh_output.reshape(x_values.shape)
plt.figure()
plt.pcolormesh(x_values,y_values,mesh_output,cmap=plt.cm.gray)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20452771272443404, 'fraction_numerical': 0.038836846213895396, 'mean_word_length': 5.470959595959596, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '78960', 'license': 'MIT', 'n_tokens_mistral': 2511, 'n_tokens_neox': 2463, 'n_words': 336, 'path': 'code-homework/ML/ex2_Logistic Regression/.ipynb_checkpoints/ex2_logistic regressionV1-checkpoint.ipynb', 'repo_name': 'phww/Andrew.Ng-ML-Study', 'size': '5124'}