逻辑回归 分类任务 芯片质量通过检测

https://gitee.com/nickdlk/python_machine_learning

#加载数据
import pandas as pd
import numpy as np
data = pd.read_csv('chip_test.csv')
data.head() #数据预览

在这里插入图片描述
在这里插入图片描述

#可视化数据
%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()

在这里插入图片描述

以函数方式求解边界函数

d = np.array(b*b-4*a*c)
#d = (-b+np.sqrt(b*b-4*a*c))/(2*a)
X1_new
#print(np.array(d))

#定义一个函数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是缺了数,有间隔的 直接补上 生成个密集的X1

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()

在这里插入图片描述

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nickdlk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值