逻辑回归基本知识点:分类
Cost function(代价函数)
梯度下降
题目
线性可分
散点图
#线性可分
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data=pd.read_csv("E:/学习/研究生阶段/python-learning/吴恩达机器学习课后作业/code/ex2-logistic regression/ex2data1.txt",names=["Exam1","Exam2","Accepted"])
# print(data)
data.insert(0,"ones",1)
x=data.iloc[:,0:-1]#取数据的0-倒数第二列,取所有行
y=data.iloc[:,-1:]
x=x.values
y=y.values
# print(x,y)
fig,ax=plt.subplots()
ax.scatter(data[data["Accepted"]==0]["Exam1"],data[data["Accepted"]==0]["Exam2"],c="r",marker="x",label="y=0")
ax.scatter(data[data["Accepted"]==1]["Exam1"],data[data["Accepted"]==1]["Exam2"],c="g",marker="o",label="y=1")
ax.legend()
ax.set(xlabel="Exam1",ylabel="Exam2")
plt.show()
决策边界
#线性可分
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
"""
原始数据处理函数
"""
def get_x_y(data):
data.insert(0, "ones", 1)
x = data.iloc[:, 0:-1] # 取数据的0-倒数第二列,取所有行
y = data.iloc[:, -1:]
x = x.values
y = y.values
return x,y
def sigmoid(z):
return 1/(1+np.exp(-z))
def cost_function(x,y,theta):
y_=sigmoid(x @ theta)
return np.sum(-(y*np.log(y_)+(1-y)*np.log(1-y_))/len(x))
"""
定义梯度下降函数
alpha:学习速率
inters:迭代次数
"""
def gradientDescent(x,y,theta,alpha,inters):
costs = []
for i in range(inters):
theta=theta-alpha*x.T @ (sigmoid(x @ theta)-y)/len(x)
cost=cost_function(x,y,theta)
costs.append(cost)
if i%1000==0:
print(cost)
return theta,costs
def predict(X,theta):
prob = sigmoid(X@theta)
return [1 if x >= 0.5 else 0 for x in prob]
data=pd.read_csv("E:/学习/研究生阶段/python-learning/吴恩达机器学习课后作业/code/ex2-logistic regression/ex2data1.txt",names=["Exam1","Exam2","Accepted"])
# print(data)
x,y=get_x_y(data)
print(x)
theta=np.zeros((x.shape[1],1))#theta初始值x.shape[1] x 1零矩阵
alpha=0.004#学习速率初始化
inters=200000#迭代次数初始化
print(x.shape[1])
"""
预测
"""
theta,costs=gradientDescent(x,y,theta,alpha,inters)
print(theta)
y_ = np.array(predict(x,theta))
y_pre = y_.reshape(len(y_),1)
acc = np.mean(y_pre==y)#acc准确率
print(acc)
fig,ax=plt.subplots()
ax.scatter(data[data["Accepted"]==0]["Exam1"],data[data["Accepted"]==0]["Exam2"],c="r",marker="x",label="y=0")
ax.scatter(data[data["Accepted"]==1]["Exam1"],data[data["Accepted"]==1]["Exam2"],c="g",marker="o",label="y=1")
x_x1=x[:,1].reshape(len(x),1)
y_x2=-(theta[0,0]/theta[2,0])-(theta[1,0]/theta[2,0])*x_x1
ax.plot(x_x1,y_x2,"r",label="jiexian")
ax.legend()
ax.set(xlabel="Exam1",ylabel="Exam2")
plt.show()
线性不可分
特征映射
题目
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data=pd.read_csv("E:/学习/研究生阶段/python-learning/吴恩达机器学习课后作业/code/ex2-logistic regression/ex2data2.txt",names=["Test1","Test2","Accepted"])
fig,ax=plt.subplots()
ax.scatter(data[data["Accepted"]==0]["Test1"],data[data["Accepted"]==0]["Test2"],c="r",marker="x",label="y=0")
ax.scatter(data[data["Accepted"]==1]["Test1"],data[data["Accepted"]==1]["Test2"],c="g",marker="o",label="y=1")
ax.legend()
ax.set(xlabel="Test1",ylabel="Test2")
plt.show()
特征映射
def feature_mapping(x1,x2,power):
data_dirc={}
for i in np.arange(power + 1):
for j in np.arange(i + 1):
data_dirc['F{}{}'.format(i-j, j)] = np.power(x1, i - j) * np.power(x2, j)
return pd.DataFrame(data_dirc)
代码解释
这段代码定义了一个名为 feature_mapping 的函数,它接收三个参数:x1 和 x2(这两个参数通常是数值型数据,比如两个特征变量),以及 power(一个整数,表示幂次的上限)。函数的目的是根据输入的 x1 和 x2,以及指定的 power,生成一个新的特征映射(feature mapping)数据集,并以 pandas.DataFrame 的形式返回。
在特征映射过程中,函数通过嵌套循环遍历所有可能的幂次组合,其中 i 的范围从 0 到 power(包含),而 j 的范围从 0 到 i(包含)。对于每一对 (i, j),它计算 x1 的 (i-j) 次幂与 x2 的 j 次幂的乘积,并将这个乘积作为新特征的值。新特征的名称由 'F{}{}' 格式化字符串生成,其中第一个 {} 填入 (i-j),第二个 {} 填入 j。
然而,代码中存在一个潜在的问题:它没有导入 numpy 和 pandas 库,这两个库在代码中被使用。为了使代码正常工作,你需要在代码的开始处添加以下导入语句:
python
import numpy as np
import pandas as pd
完整的函数代码应该如下所示:
python
import numpy as np
import pandas as pd
def feature_mapping(x1, x2, power):
data_dirc = {}
for i in np.arange(power + 1):
for j in np.arange(i + 1):
data_dirc['F{}{}'.format(i-j, j)] = np.power(x1, i - j) * np.power(x2, j)
return pd.DataFrame(data_dirc)
# 示例用法
x1 = 2
x2 = 3
power = 2
result = feature_mapping(x1, x2, power)
print(result)
这个示例中,x1 设置为 2,x2 设置为 3,power 设置为 2。因此,函数会生成包含以下特征的数据框:
F00(x1^0 * x2^0)
F10(x1^1 * x2^0)
F11(x1^1 * x2^1)
F20(x1^2 * x2^0)
F21(x1^2 * x2^1)
这些特征对应于 x1 和 x2 的不同幂次组合,可用于机器学习模型中以增加模型的非线性。
代价函数(损失函数)
def sigmoid(z):
return 1/(1+np.exp(-z))
def cost_function(x,y,theta,lr):
y_=sigmoid(x @ theta)
reg=np.sum(np.power(theta[1:]),2)*(lr/(2*len(x)))#正则化
return np.sum(-(y*np.log(y_)+(1-y)*np.log(1-y_))/len(x))+reg
梯度下降
"""
定义梯度下降函数
alpha:学习速率
inters:迭代次数
lamda
"""
def gradientDescent(x,y,theta,alpha,inters,lamda):
costs = []
for i in range(inters):
reg = theta[1:] * (lamda / len(x)) #
reg = np.insert(reg, 0, values=0, axis=0) #在第一个元素前插入0,为了与后面维数匹配
theta=theta-alpha*x.T @ (sigmoid(x @ theta)-y)/len(x)
cost=cost_function(x,y,theta,lamda)
costs.append(cost)
# if i%1000==0:
# print(cost)
return theta,costs
"""
准确率预测
"""
def predict(X,theta):
prob = sigmoid(X@theta)
return [1 if x >= 0.5 else 0 for x in prob]
结果
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
"""
原始数据处理函数
"""
def get_x_y(data):
data.insert(0, "ones", 1)
x = data.iloc[:, 0:-1] # 取数据的0-倒数第二列,取所有行
y = data.iloc[:, -1:]
x = x.values
y = y.values
return x,y
"""
特征映射
"""
def feature_mapping(x1,x2,power):
data_dirc={}
for i in np.arange(power + 1):
for j in np.arange(i + 1):
data_dirc['F{}{}'.format(i-j, j)] = np.power(x1, i - j) * np.power(x2, j)
return pd.DataFrame(data_dirc)
"""
代价函数
"""
def sigmoid(z):
return 1/(1+np.exp(-z))
def cost_function(x,y,theta,lamda):
y_=sigmoid(x @ theta)
reg=np.sum(np.power(theta[1:],2))*(lamda/(2*len(x)))#正则化
return np.sum(-(y*np.log(y_)+(1-y)*np.log(1-y_))/len(x))+reg
"""
定义梯度下降函数
alpha:学习速率
inters:迭代次数
lamda
"""
def gradientDescent(x,y,theta,alpha,inters,lamda):
costs = []
for i in range(inters):
reg = theta[1:] * (lamda / len(x)) #
reg = np.insert(reg, 0, values=0, axis=0) #在第一个元素前插入0,为了与后面维数匹配
theta=theta-alpha*x.T @ (sigmoid(x @ theta)-y)/len(x)
cost=cost_function(x,y,theta,lamda)
costs.append(cost)
# if i%1000==0:
# print(cost)
return theta,costs
"""
准确率预测
"""
def predict(X,theta):
prob = sigmoid(X@theta)
return [1 if x >= 0.5 else 0 for x in prob]
data=pd.read_csv("E:/学习/研究生阶段/python-learning/吴恩达机器学习课后作业/code/ex2-logistic regression/ex2data2.txt",names=["Test1","Test2","Accepted"])
# # fig,ax=plt.subplots()
# # ax.scatter(data[data["Accepted"]==0]["Test1"],data[data["Accepted"]==0]["Test2"],c="r",marker="x",label="y=0")
# # ax.scatter(data[data["Accepted"]==1]["Test1"],data[data["Accepted"]==1]["Test2"],c="g",marker="o",label="y=1")
# # ax.legend()
# # ax.set(xlabel="Test1",ylabel="Test2")
# # plt.show()
x1=data["Test1"]
x2=data["Test2"]
power=6
data_=feature_mapping(x1,x2,power)
x=data_.values
y = data.iloc[:, -1:].values
theta=np.zeros((x.shape [1],1))#theta初始值x.shape[1] x 1零矩阵
lamda=0.001
alpha=0.001#学习速率初始化
inters=200000#迭代次数初始化
# cost_init=cost_function(x,y,theta, lamda)
# print(cost_init)
theta,costs=gradientDescent(x,y,theta,alpha,inters,lamda)
print(theta)
y_ = np.array(predict(x,theta))
y_pre = y_.reshape(len(y_),1)
acc = np.mean(y_pre==y)#acc准确率
print(acc)
fig,ax=plt.subplots()
ax.scatter(data[data["Accepted"]==0]["Test1"],data[data["Accepted"]==0]["Test2"],c="r",marker="x",label="y=0")
ax.scatter(data[data["Accepted"]==1]["Test1"],data[data["Accepted"]==1]["Test2"],c="g",marker="o",label="y=1")
ax.legend()
ax.set(xlabel="Test1",ylabel="Test2")
x_plot= np.linspace(-1.2,1.2,200)
xx,yy = np.meshgrid(x_plot,x_plot)
z = feature_mapping(xx.ravel(),yy.ravel(),power).values
zz=z @ theta
zz=zz.reshape(xx.shape)
plt.contour(xx,yy,zz,0)
plt.show()