简介
逻辑回归: 使用了逻辑回归函数对数据进行了拟合就叫逻辑回归??
\[P(x)=\frac{1}{1+e^{-x}}(sigmoid function)
\]
\[y= \begin{cases}1, & P(x) \geq 0.5 \\ \hline 0, & P(x)<0.5\end{cases}
\]
其中y为分类结果,P为概率分布,x为特征值。
分类问题的核心就是寻找决策边界。
损失函数
\[J_{i}=\left\{\begin{array}{l}
-\log \left(P\left(x_{i}\right)\right), \text { if } y_{i}=1 \\
-\log \left(1-P\left(x_{i}\right)\right), \text { if } y_{i}=0
\end{array}\right.
\]
联合表示
\[J=\frac{1}{m} \sum_{i=1}^{m} J_{i}=-\frac{1}{m}\left[\sum_{i=1}^{m}\left(\left(y_{i} \log \left(P\left(x_{i}\right)\right)\right.+\left(1-y_{i}\right) \log \left(1-P\left(x_{i}\right)\right)\right)\right]
\]
重复计算直至收敛
\[\left\{\begin{aligned}
t e m p_{\theta_{j}} &=\theta_{j}-\alpha \frac{\partial}{\partial \theta_{j}} J(\theta) \\
\theta_{j} &=t e m p_{\theta_{j}}
\end{aligned}\right\}
\]
评估模型表现
使用准确率
\[\text { Accuracy }=\frac{\text { 正确预测样本数量 }}{\text { 总样本数量 }}
\]
参考链接
https://blog.csdn.net/weixin_46344368/article/details/105904589?spm=1001.2014.3001.5502
https://blog.csdn.net/weixin_46344368/article/details/105909429?spm=1001.2014.3001.5502
code
学生考试成绩已知两门成绩判定第三门成绩是否会合格
import pandas as pd
import numpy as np
data = pd.read_csv('examdata.csv')
data.head()
#visalize the data
from matplotlib import pyplot as plt
fig1=plt.figure()
plt.scatter(data.loc[:,'Exam1'],data.loc[:,'Exam2'])
plt.title('Exam1-Exam2')
plt.xlabel('Exam1')
plt.ylabel('Exam2')
plt.show()
# add label mask
mask=data.loc[:,'Pass']==1
print(mask) # print(~mask)
fig2=plt.figure()
passed=plt.scatter(data.loc[:,'Exam1'][mask],data.loc[:,'Exam2'][mask])
failed=plt.scatter(data.loc[:,'Exam1'][~mask],data.loc[:,'Exam2'][~mask])
plt.title('Exam1-Exam2')
plt.xlabel('Exam1')
plt.ylabel('Exam2')
plt.legend((passed,failed),('passed','failed'))
plt.show()
#define X,y
X = data.drop(['Pass'],axis=1)
y = data.loc[:,'Pass']
#X.head()
X1 = data.loc[:,'Exam1']
X2 = data.loc[:, 'Exam2']
y.head()
# eatablish the model and train it
from sklearn.linear_model import LogisticRegression
LR = LogisticRegression()
LR.fit(X,y)
# show the predicted result and its accuracy
y_predict = LR.predict(X)
print(y_predict)
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y,y_predict)
print(accuracy)
#exam1 = 70, exam2=65
y_test = LR.predict([[70,65]])
print('passed' if y_test==1 else 'failed')
print(LR.coef_,LR.intercept_)
theta0 = LR.intercept_
theta1,theta2 = LR.coef_[0][0],LR.coef_[0][1]
print(theta0,theta1,theta2)
X2_new = -(theta0+theta1*X1)/theta2
print(X2_new)
# 边界函数
fig3 = plt.figure()
passed=plt.scatter(data.loc[:,'Exam1'][mask],data.loc[:,'Exam2'][mask])
failed=plt.scatter(data.loc[:,'Exam1'][~mask],data.loc[:,'Exam2'][~mask])
plt.plot(X1,X2_new) #画出决策边界
plt.title('Exam1-Exam2')
plt.xlabel('Exam1')
plt.ylabel('Exam2')
plt.legend((passed,failed),('passed','failed'))
plt.show()
# create new data
X1_2 = X1*X1
X2_2 = X2*X2
X1_X2 = X1*X2
print(X1,X1_2)
X_new = {'X1':X1,'X2':X2,'X1_2':X1_2,'X2_2':X2_2,'X1_X2':X1_X2}
X_new = pd.DataFrame(X_new)
print(X_new)
LR2 = LogisticRegression()
LR2.fit(X_new,y)
y2_predict = LR2.predict(X_new)
accuracy2 = accuracy_score(y,y2_predict)
print(accuracy2)
X1_new = X1.sort_values()
print(X1,X1_new)
theta0=LR2.intercept_
theta1,theta2,theta3,theta4,theta5=LR2.coef_[0][0],LR2.coef_[0][1],LR2.coef_[0][2],LR2.coef_[0][3],LR2.coef_[0][4]
a = theta4
b = theta5*X1_new+theta2
c = theta0+theta1*X1_new+theta3*X1_new*X1_new
X2_new_boundary = (-b+np.sqrt(b*b-4*a*c))/(2*a)
print(X2_new_boundary)
fig5 = plt.figure()
passed=plt.scatter(data.loc[:,'Exam1'][mask],data.loc[:,'Exam2'][mask])
failed=plt.scatter(data.loc[:,'Exam1'][~mask],data.loc[:,'Exam2'][~mask])
plt.plot(X1_new,X2_new_boundary) #画出决策边界
plt.title('Exam1-Exam2')
plt.xlabel('Exam1')
plt.ylabel('Exam2')
plt.legend((passed,failed),('passed','failed'))
plt.show()
芯片质量检测
#加载数据
import pandas as pd
import numpy as np
data = pd.read_csv('chip_test.csv')
data.head() #数据预览
#添加标签
mask = data.loc[:,'pass'] == 1
print(mask)#预览
#可视化数据
%matplotlib inline
from matplotlib import pyplot as plt
fig1 = plt.figure()
passed = plt.scatter(data.loc[:,'test1'][mask],data.loc[:,'test2'][mask])
failed = plt.scatter(data.loc[:,'test1'][~mask],data.loc[:,'test2'][~mask]) #这里的~mask是取反,可以理解为0
plt.title('test1-test2')
plt.xlabel('test1')
plt.ylabel('test2')
plt.legend((passed,failed),('passed','failed'))
plt.show()
#将数据赋值给相关变量
X = data.drop(['pass'],axis=1)
y = data.loc[:,'pass']
X1 = data.loc[:,'test1']
X2 = data.loc[:,'test2']
X1.head()
#创建新数据
X1_2 = X1*X1
X2_2 = X2*X2
X1_X2 = X1*X2
X_new = {'X1':X1,'X2':X2,'X1_2':X1_2,'X2_2':X2_2,'X1_X2':X1_X2}
X_new = pd.DataFrame(X_new)
print(X_new)
#创建并训练模型
from sklearn.linear_model import LogisticRegression
LR1 = LogisticRegression()
LR1.fit(X_new,y)
#评估模型
from sklearn.metrics import accuracy_score
y1_predict = LR1.predict(X_new)
accuracy1 = accuracy_score(y,y1_predict)
print(accuracy1)
X1_new = X1.sort_values()
theta0 = LR1.intercept_
theta1,theta2,theta3,theta4,theta5 = LR1.coef_[0][0],LR1.coef_[0][1],LR1.coef_[0][2],LR1.coef_[0][3],LR1.coef_[0][4]
a = theta4
b = theta5*X1_new+theta2
c = theta0+theta1*X1_new+theta3*X1_new*X1_new
X2_new_boundary = (-b+np.sqrt(b*b-4*a*c))/(2*a)
fig2 = plt.figure()
passed=plt.scatter(data.loc[:,'test1'][mask],data.loc[:,'test2'][mask])
failed=plt.scatter(data.loc[:,'test1'][~mask],data.loc[:,'test2'][~mask])
plt.plot(X1_new,X2_new_boundary)
plt.title('test1-test2')
plt.xlabel('test1')
plt.ylabel('test2')
plt.legend((passed,failed),('passed','failed'))
plt.show()
#定义一个函数f(x)
#define f(x)
def f(x):
a = theta4
b = theta5*x+theta2
c = theta0+theta1*x+theta3*x*x
X2_new_boundary1 = (-b+np.sqrt(b*b-4*a*c))/(2*a)
X2_new_boundary2 = (-b-np.sqrt(b*b-4*a*c))/(2*a)
return X2_new_boundary1,X2_new_boundary2
X2_new_boundary1 = [] #用这两个变量来存储test1对应的边界上的test2的值
X2_new_boundary2 = []
for x in X1_new:
X2_new_boundary1.append(f(x)[0]) #所求解的第一个数值,故它的索引为0
X2_new_boundary2.append(f(x)[1]) #所求解的第二个数值,故它的索引为1
print(X2_new_boundary1,X2_new_boundary2)
fig3 = plt.figure()
passed=plt.scatter(data.loc[:,'test1'][mask],data.loc[:,'test2'][mask])
failed=plt.scatter(data.loc[:,'test1'][~mask],data.loc[:,'test2'][~mask])
plt.plot(X1_new,X2_new_boundary1)
plt.plot(X1_new,X2_new_boundary2)
plt.title('test1-test2')
plt.xlabel('test1')
plt.ylabel('test2')
plt.legend((passed,failed),('passed','failed'))
plt.show();
X1_range = [-0.9 + x/10000 for x in range(0,20000)]
X1_range = np.array(X1_range) #转换成数组
X2_new_boundary1 = []
X2_new_boundary2 = []
for x in X1_range:
X2_new_boundary1.append(f(x)[0])
X2_new_boundary2.append(f(x)[1])
# coding:utf-8
import matplotlib as mlp
mlp.rcParams['font.family'] = 'SimHei'
mlp.rcParams['axes.unicode_minus'] = False
fig4 = plt.figure()
passed=plt.scatter(data.loc[:,'test1'][mask],data.loc[:,'test2'][mask])
failed=plt.scatter(data.loc[:,'test1'][~mask],data.loc[:,'test2'][~mask])
plt.plot(X1_range,X2_new_boundary1,'r')
plt.plot(X1_range,X2_new_boundary2,'r')
plt.title('test1-test2')
plt.xlabel('测试1')
plt.ylabel('测试2')
plt.title('芯片质量预测')
plt.legend((passed,failed),('passed','failed'))
plt.show()
---------------------------我的天空里没有太阳,总是黑夜,但并不暗,因为有东西代替了太阳。虽然没有太阳那么明亮,但对我来说已经足够。凭借着这份光,我便能把黑夜当成白天。我从来就没有太阳,所以不怕失去。
--------《白夜行》