当前位置: 首页 > news >正文

人工智能机器学习——逻辑回归

一、分类问题(Classification)

垃圾邮件检测
在这里插入图片描述

流程

  • 标注样本邮件未垃圾/普通邮件(人)
  • 获取批量的样本邮件及其标签,学习其特征(计算机)
  • 针对新的邮件,自动判断其类别(计算机)

在这里插入图片描述
图像分类
在这里插入图片描述
数字识别
在这里插入图片描述
分类

分类:根据已知样本的某些特征,判断一个新的样本属于哪种已知的样本类

在这里插入图片描述

二、分类方法

  • 逻辑回归
    在这里插入图片描述
  • KNN近邻模型
    在这里插入图片描述
  • 决策树
    在这里插入图片描述
  • 神经网络
    在这里插入图片描述
    在这里插入图片描述
    逻辑回归
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

三、考试通过预测,使用数据集examdata.csv

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

在这里插入图片描述

#画散点图
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()

在这里插入图片描述

#区分数据
mask = data.loc[:,'Pass']==1
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()

在这里插入图片描述

#赋值x,y
x = data.drop(['Pass'],axis=1)
x.head()

在这里插入图片描述

x1 = data.loc[:,'Exam1']
x2 = data.loc[:,'Exam2']
y = data.loc[:,'Pass']
y.head()

在这里插入图片描述

#打印x,y维度
print(x.shape,y.shape)

在这里插入图片描述

#训练逻辑回归模型
from sklearn.linear_model import LogisticRegression
LR = LogisticRegression()
LR.fit(x,y)

在这里插入图片描述

#预测结果
y_predict = LR.predict(x)
print(y_predict)

在这里插入图片描述

#打印预测准确率
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y,y_predict)
print(accuracy)

在这里插入图片描述

#预测新数据
X_test = pd.DataFrame([[70,65]],columns=['Exam1','Exam2'])
y_test = LR.predict(X_test)
print('passed' if y_test==1 else 'failed')

在这里插入图片描述

#边界曲线
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()

在这里插入图片描述

#使用二阶边界函数
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)

在这里插入图片描述

#创建模型2
LR2 = LogisticRegression(solver='liblinear', max_iter=1000)# solver='saga',    # 最通用的求解器 max_iter=1000,    # 足够的迭代次数
LR2.fit(X_new,y)

在这里插入图片描述

#预测结果
y_2_predict = LR2.predict(X_new)
print(y_2_predict)

在这里插入图片描述

#打印预测准确率
accuracy = accuracy_score(y,y_2_predict)
print(accuracy)

在这里插入图片描述

#对x1排序
X1_new = x1.sort_values()
print(x1,X1_new)

在这里插入图片描述

LR2.coef_

在这里插入图片描述

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(theta0,theta1,theta2,theta3,theta4,theta5)
print(X2_new_boundary)

在这里插入图片描述

fig4 = plt.figure()
plt.plot(x1,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,X2_new_boundary)
plt.title("Exam1-Exam2")
plt.xlabel("Exam1")
plt.ylabel("Exam2")
plt.legend((passed,failed),("passed","failed"))
plt.show()

在这里插入图片描述

四、芯片质量预测实战,使用数据集chip_test.csv

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

在这里插入图片描述

#画散点图
from matplotlib import pyplot as plt
fig6 = plt.figure()
plt.scatter(data.loc[:,'test1'],data.loc[:,'test2'])
plt.title("test1-test2")
plt.xlabel("test1")
plt.ylabel("test2")
plt.show()

在这里插入图片描述

#区分数据
mask = data.loc[:,'pass']==1
print(mask)

在这里插入图片描述

fig7 = 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.title("test1-test2")
plt.xlabel("test1")
plt.ylabel("test2")
plt.legend((passed,failed),("passed","failed"))
plt.show()

在这里插入图片描述

#赋值x,y
x = data.drop(['pass'],axis=1)
x1 = data.loc[:,'test1']
x2 = data.loc[:,'test2']
y = data.loc[:,'pass']
#使用二阶边界函数
X1_2 = x1*x1
X2_2 = x2*x2
X1_X2 = x1*x2X_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)

在这里插入图片描述

#创建模型2
LR2 = LogisticRegression(solver='liblinear', max_iter=1000)# solver='saga',    # 最通用的求解器 max_iter=1000,    # 足够的迭代次数
LR2.fit(X_new,y)

在这里插入图片描述

#预测结果
y_2_predict = LR2.predict(X_new)
print(y_2_predict)

在这里插入图片描述

#打印预测准确率
accuracy = accuracy_score(y,y_2_predict)
print(accuracy)

在这里插入图片描述

#对x1排序
X1_new = x1.sort_values()
print(x1,X1_new)
LR2.coef_
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]print(theta0,theta1,theta2,theta3,theta4,theta5)

在这里插入图片描述

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)

在这里插入图片描述

fig8 = plt.figure()
plt.plot(X1_new,X2_new_boundary)

在这里插入图片描述

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

在这里插入图片描述

#定义边界函数
def f(x):a = theta4b = theta5*x+theta2c = theta0+theta1*x+theta3*x*xX2_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 = []
X2_new_boundary2 = []
for x in X1_new:X2_new_boundary1.append(f(x)[0])X2_new_boundary2.append(f(x)[1])
print(X2_new_boundary1,X2_new_boundary2)
fig10 = 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,19000)]
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])
print(X2_new_boundary1,X2_new_boundary2)
fig11 = 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)
plt.plot(X1_range,X2_new_boundary2)
plt.title("test1-test2")
plt.xlabel("test1")
plt.ylabel("test2")
plt.legend((passed,failed),("passed","failed"))
plt.show()

在这里插入图片描述


文章转载自:

http://SmxhBFvY.rwxtn.cn
http://i9GtYMgI.rwxtn.cn
http://B7KWcLzL.rwxtn.cn
http://oKKomZ3F.rwxtn.cn
http://TZz1JMdW.rwxtn.cn
http://BNsLbSqm.rwxtn.cn
http://pHCG8Agb.rwxtn.cn
http://BdG9p2eA.rwxtn.cn
http://5z9BHgnA.rwxtn.cn
http://HUwkl9Fx.rwxtn.cn
http://gCv8O5ye.rwxtn.cn
http://a67l2qz5.rwxtn.cn
http://vbHvOylL.rwxtn.cn
http://zoVRd3Jz.rwxtn.cn
http://SCm7tbem.rwxtn.cn
http://woQfhfv2.rwxtn.cn
http://7xOpFbBX.rwxtn.cn
http://lMQ2dzZx.rwxtn.cn
http://HTmIEj6U.rwxtn.cn
http://YoxWRwTI.rwxtn.cn
http://qKke6vyC.rwxtn.cn
http://q6COSqfU.rwxtn.cn
http://XTIXdjvm.rwxtn.cn
http://LLpzhQu9.rwxtn.cn
http://XNNJc1tG.rwxtn.cn
http://x7c96tfG.rwxtn.cn
http://4a7TfgUU.rwxtn.cn
http://bNpDt1to.rwxtn.cn
http://ytmKMewQ.rwxtn.cn
http://uNv2nh5D.rwxtn.cn
http://www.dtcms.com/a/367393.html

相关文章:

  • Java Web 是技术与产业的 “交叉赋能点”
  • Linux笔记---UDP套接字实战:简易聊天室
  • 新增MCP工具管理,AI对话节点新增工具设置,支持对接企业微信机器人,MaxKB v2.1.0版本发布
  • 2025年数学建模国赛C题超详细解题思路
  • 【论文阅读】-《Besting the Black-Box: Barrier Zones for Adversarial Example Defense》
  • 小迪web自用笔记27
  • 不会战略、不会融资、不会搭团队?别叫自己 CTO
  • ⸢ 肆 ⸥ ⤳ 默认安全建设方案:b.安全资产建设
  • 【高分论文密码】大尺度空间模拟预测与数字制图
  • 机器翻译:腾讯混元团队开源的模型 Hunyuan-MT 详解
  • #数据结构----2.1线性表
  • IT需求提示未读信息查询:深度技术解析与性能优化指南【类似:钉钉已读 功能】
  • RAG(检索增强生成)-篇一
  • 解密注意力机制:为何它能在Transformer中实现高效并行计算?
  • 2025跨境独立站最新最完整的搭建流程
  • 2025年百度商业AI技术创新大赛赛道二:视频广告生成推理性能优化-初赛第五名,复赛第九名方案分享
  • VS code如何下载安装
  • 跨部门共享研发文档总是困难重重该如何改善
  • 关于如何在PostgreSQL中调整数据库参数和配置的综合指南
  • 若依微服务遇到的配置问题
  • Cadence OrCAD Capture绘制复用管脚封装的方法图文教程
  • LLM中的function call
  • 残差网络的介绍
  • cursor+python轻松实现电脑监控
  • 梯度爆炸问题:深度学习中的「链式核弹」与拆弹指南
  • JavaAI炫技赛:电商系统商品管理模块设计与实现——基于Spring AI的商品智能上架全流程解析
  • G2D 图形加速器
  • 2024年12月GESPC++三级真题解析(含视频)
  • 0904 类的继承
  • apache poi与Office Open XML关系