哈工大-机器学习-实验二:Logistic Regression

机器学习 实验二 决策树 决策树 阅读详情
AI摘要

本文介绍了使用LogisticRegression解决二分类问题,通过实验理解并实现逻辑回归模型的参数估计,包括无惩罚项和加入L2正则化的参数估计。实验涉及最大似然估计(MLE)和贝叶斯估计(MAP),并采用牛顿法求解损失函数的优化解。在不同条件下,对比了满足和不满足朴素贝叶斯假设以及有无正则项的情况对模型性能的影响,验证了正则项在防止过拟合中的作用。此外,实验还展示了在UCI数据集上的应用,如Skin和Iris数据集,进一步证明了逻辑回归的有效性。

Logistic Regression解决二分类问题

一、实验目的

理解逻辑回归模型,掌握逻辑回归模型的参数估计算法。

二、实验要求及实验环境

要求:

实现两种损失函数的参数估计(1,无惩罚项;2.加入对参数的惩罚),可以采用梯度下降、共轭梯度或者牛顿法等。

实验环境:

windows10、python3.7.4、pycharm2020.2

三、数学原理

3.1 实验目的及假设

实验目的:

从TrainingSet:<X1,Y1>,<X2,Y2>...<Xl,Yl>Training Set:<X^1,Y^1>,<X^2,Y^2>...<X^l,Y^l>TrainingSet:<X1,Y1>,<X2,Y2>...<Xl,Yl>中学习到一个分类器
f:X→Y f:X\to Y f:X→Y
以便于预测一个新的样本XnewX^{new}Xnew所属的类别labellabellabel

实验假设:

  • XXX的每一维属性XiX_iXi​都是实数,故XXX可视为形如<X1,X2...Xn><X_1,X_2...X_n><X1​,X2​...Xn​>的nnn维vectorvectorvector

  • YYY是booleanbooleanboolean值,取值为1或0

  • XiX_iXi​关于YYY条件独立

  • P(Xi∣Y=yk)∼N(μik,σi)P(X_i| Y=y_k)\sim N(\mu_{ik},\sigma_{i})P(Xi​∣Y=yk​)∼N(μik​,σi​)

  • P(Y)∼B(π)P(Y)\sim B(\pi)P(Y)∼B(π)

3.2 转化P(Y∣X)P(Y|X)P(Y∣X)

3.2.1 利用实验假设

按照前面的实验假设,结合概率论的知识,我们可以得到:
P(Y=1∣X)=P(Y=1)P(X∣Y=1)P(Y=1)P(X∣Y=1)+P(Y=0)P(X∣Y=0)=11+P(Y=0)P(X∣Y=0)P(Y=1)P(X∣Y=1)=11+exp(lnP(Y=0)P(X∣Y=0)P(Y=1)P(X∣Y=1))=11+exp(ln1−ππ+∑ilnP(Xi∣Y=0)P(Xi∣Y=1)) \begin{aligned} P(Y=1|X)&=\frac{P(Y=1)P(X|Y=1)}{P(Y=1)P(X|Y=1)+P(Y=0)P(X|Y=0)}\\ &=\frac{1}{1+\frac{P(Y=0)P(X|Y=0)}{P(Y=1)P(X|Y=1)}}\\ &=\frac{1}{1+exp(ln\frac{P(Y=0)P(X|Y=0)}{P(Y=1)P(X|Y=1)})}\\ &=\frac{1}{1+exp(ln\frac{1-\pi}{\pi}+\sum_iln\frac{P(X_i|Y=0)}{P(X_i|Y=1)})} \end{aligned} P(Y=1∣X)​=P(Y=1)P(X∣Y=1)+P(Y=0)P(X∣Y=0)P(Y=1)P(X∣Y=1)​=1+P(Y=1)P(X∣Y=1)P(Y=0)P(X∣Y=0)​1​=1+exp(lnP(Y=1)P(X∣Y=1)P(Y=0)P(X∣Y=0)​)1​=1+exp(lnπ1−π​+∑i​lnP(Xi​∣Y=1)P(Xi​∣Y=0)​)1​​
由于P(Xi∣Y=yk)=1σi2πexp(−(Xi−μik)22σi2)P(X_i|Y=y_k)=\frac{1}{\sigma_{i}\sqrt{2\pi}}exp(\frac{-(X_i-\mu_{ik})^2}{2\sigma_{i}^2})P(Xi​∣Y=yk​)=σi​2π ​1​exp(2σi2​−(Xi​−μik​)2​)

代回原来的式子,可得
P(Y=1∣X)=11+exp(w0+∑i=1nwiXi)w0=∑iμi12−μi022σi2+ln1−ππ;wi=μi0−μi1σi2 P(Y=1|X)=\frac{1}{1+exp(w_0+\sum_{i=1}^nw_iX_i)} \\ w_0=\sum_i\frac{\mu_{i1}^2-\mu_{i0}^2}{2\sigma_i^2}+ln\frac{1-\pi}{\pi}; w_i=\frac{\mu_{i0}-\mu_{i1}}{\sigma_i^2} P(Y=1∣X)=1+exp(w0​+∑i=1n​wi​Xi​)1​w0​=i∑​2σi2​μi12​−μi02​​+

从零搭建情绪分析Web应用:机器学习全流程实战 情感分析是自然语言处理中最基础且高频落地的典型任务,其核心在于将非结构化文本转化为可计算的情感倾向标签。技术原理上依赖文本表示(如TF-IDF)、分类模型(如逻辑回归)与服务封装三者协同,关键价值在于业务可控性——支持行业定制、低延迟推理与轻量部署。典型应用场景包括电商评论监控、客服工单初筛、社交媒体舆情分析等,尤其适合需快速验证算法工程闭环的中小项目。本文聚焦‘从零到上线’完整链路,覆盖数据标注规范、Flask服务封装、Docker容器化及Nginx反向代理等生产级实践要点,强调可复现、易维护、真可用。 阅读详情

相关推荐

2023美赛全解析:从选题策略到建模实战,助你高效备战数学建模竞赛

数学建模是运用数学语言和方法,对实际问题进行抽象、简化和求解的过程,其核心在于将复杂现象转化为可计算的数学模型。其基本原理通常包括问题分析、模型假设、建立数学结构、求解算法设计以及结果分析与验证。这一技术价值在于为决策提供量化依据,广泛应用于工程优化、金融分析、数据挖掘和公共政策评估等多个场景。在诸如美国大学生数学建模竞赛(MCM/ICM)这类高强度赛事中,参赛者需在有限时间内完成从选题、建模到论文撰写的全流程。本文聚焦于**模拟退火**、**系统动力学**等关键建模方法,并结合**赛题选择**与**时间管

weixin_34068198的博客 409

DeBERTa-v3实战:中文情感分析生产级部署指南

情感分析是自然语言处理的基础任务,核心在于从文本中识别用户情绪倾向并支撑业务决策。其技术原理依赖上下文建模能力,尤其需处理转折逻辑、强度梯度与长尾表达等复杂语义现象。当前主流方案包括规则词典、传统机器学习、预训练微调(如BERT系列)及大模型Prompt方法,但多数在真实场景中因数据噪声、跨域泛化弱或推理成本高而失效。本文聚焦工业落地关键指标——决策错误率、长尾召回、跨域稳定性与推理性价比,实测16个开源模型,验证DeBERTa-v3-base凭借解耦相对位置编码与增强掩码策略,在中文电商、社交及客服多源真

weixin_30876945的博客 387

机器学习实战:微博评论情感分析完整流程复现与避坑指南

情感分析是自然语言处理的重要分支,通过自动识别文本中的观点与情绪,广泛应用于舆情监控、用户反馈分析和社交媒体研究。其核心原理是构建机器学习分类流程,从数据采集、清洗、特征提取到模型训练与评估,每一步都直接影响最终准确率。在文本分类任务中,特征工程尤为关键:使用结巴分词进行词语切分,结合卡方降维筛选高区分度特征,能显著提升模型表现;而朴素贝叶斯、SVM等分类器的对比调优,则帮助寻找最佳算法组合。本文以微博评论数据为例,拆解一份可完整复现的情感分析实验,涵盖爬虫获取、人工标注、特征维度扫描、分类器横向对比以及P

weixin_34331102的博客 415

从零构建微博情感分析系统:Python NLP实战全流程解析

自然语言处理(NLP)是人工智能领域的关键技术,其核心在于让计算机理解和处理人类语言。其基本原理是通过文本向量化将非结构化的文本转换为数学模型可处理的数值特征,进而应用机器学习或深度学习算法进行分类、预测等任务。这一技术具有巨大的应用价值,尤其在社交媒体分析、舆情监控、产品评论挖掘等场景中,能够自动化地从海量文本中提取情感倾向和关键观点。本文聚焦于微博用户情感分析这一具体应用,详细拆解了从数据爬取、文本预处理、模型构建到Web应用部署的完整工程化流程。文中深入探讨了如何应对微博特有的网络新词和表情符号处理,

weixin_33826609的博客 342

燕大《Python机器学习》实验报告 .doc

燕大软件机器学习实验报告,六个模型学习,实验报告下载了就能用,很方便,下载就行,直接用,燕大嘞。鸢尾花,波士顿,猫狗分类什么的

实验一_逻辑回归1

介绍:在本练习中,您将实现逻辑回归,并将其应用于两个不同的数据集。实验文件说明:ex2.py - 可以帮助您逐步完成练习的 Python 脚本mapFeatur

麒麟系统下的多媒体革命:VLC在kylinV10上的性能优化与调试技巧

本文详细介绍了在麒麟kylinV10系统上优化VLC多媒体播放性能的实战技巧,特别针对飞腾FT-2000/4硬件平台。从源码编译调优到硬件加速配置,提供全面的性能提升方案,帮助开发者在国产操作系统上构建高效媒体播放环境。

weixin_29168675的博客 457

OpenWebUI外部模型配置异常排查与修复指南

本文详细解析了OpenWebUI外部模型配置异常的常见问题及解决方案,包括隐藏字符、数据库锁死等典型现象。通过错误日志分析和数据库修复实战指南,帮助开发者快速定位并解决配置问题,确保OpenWebUI与第三方大模型的稳定连接。

weixin_29205061的博客 311

从微服务到 Agent 服务:架构思维的迁移

微服务的概念最早在2014年由Martin Fowler提出,核心是“将单体应用拆分为一组小的服务,每个服务运行在独立的进程中,服务之间采用轻量级的通信机制互相协作,每个服务围绕具体业务构建,并且可以独立部署到生产环境”。单一职责:每个微服务只负责一个业务领域的功能,比如订单服务只处理订单相关的逻辑,用户服务只处理用户相关的逻辑被动响应:微服务是“哑的”,只有收到外部请求的时候才会执行逻辑,本身没有主动决策的能力固定流程:微服务的逻辑是硬编码的,执行流程是提前定义好的,只能按照程序员写的代码处理请求。

AI智能探索者的博客 275

得物(毒)App潮鞋价格波动监控系统实战:从爬虫开发到数据可视化完整指南

本文将带领您从零开始构建一套完整的得物潮鞋价格监控系统,涵盖爬虫开发、数据存储、价格波动分析和可视化展示等全流程技术。我们将使用最新的Python技术栈,包括异步爬虫框架、智能代理池、反爬虫绕过技巧,以及基于机器学习的时间序列预测模型。无论您是想要追踪特定鞋款价格的潮流爱好者,还是希望构建商业级监控系统的开发者,这篇文章都将为您提供切实可行的解决方案。

2201_76125261的博客 2506

基于fme和python-docx第三方库批量更新不动产调查表

最近应朋友需要批量更新在不动产成果中提交的各种word报表中的指定数据,根据初步的了解,我做了一个基于fme软件和python-docx第三方库进行一个批量的更新方案。

weixin_43229258的博客 1240

【VTKExamples::Meshes】第十二期 QuadricDecimation

本文分享VTK样例QuadricDecimation,并解析接口vtkQuadricDecimation,希望对各位小伙伴有所帮助!

qq_40041064的博客 344

NX二次开发 内部函数鼠标动态位置获取

内部函数鼠标动态位置获取

qq285503851的博客 3337

Openclaw第5章 知识库管理

💡:学会使用 OpenClaw构建个人知识库系统,实现网页文章存档、GitHub项目管理、论文笔记整理和跨设备同步。

Maynor的博客 776

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

上一篇: 哈工大-机器学习-实验一:多项式拟合
下一篇: 哈工大-机器学习-实验三:EM、k-means、GMM
master@yi
博客等级 码龄8年 42粉丝 11原创
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值