如何用IDLidl 调用matlablogistic函数?求具体的idl 调用matlab代码!

更多公众号:BioInformatics_CN共享生物信息技术,生物信息常用数据库及软件使用指导,常用数据分析流程,个性化数据分析,个性化医疗
加群"云生信"
共同进步~最新文章相关推荐搜狗:感谢您阅读#源代码#逻辑回归LogisticRegression原理介绍及方法实现,本文可能来自网络,如果侵犯了您的相关权益,请联系管理员。QQ:本帖子已过去太久远了,不再提供回复功能。机器学习(8)
什么叫回归:
假设我们现在又一些数据点,我们用一条直线对这些数据点进行拟合(该线成为最佳拟合直线),这个拟合过程就称作回归。
主要思想:
根据现有数据对分类边界线建立回归公式,以此进行分类。
训练分类器时的做法就是寻找最佳拟合参数,使用的是最优化算法。
因为是分类,所以我们想要的函数应该是,能接受所有的输入然后预测出类别。在这我们用到的是Sigmoid函数,这个函数的计算公式如下:
在这个函数中,我们输入z,就能够得到一个范围在0--1之间的数值,大于0.5的数据被分为1类,小于0.5的被归入0类。所以Logistic也可以被看成是一种概率估计。
下面我们的问题就是怎么确定这个z,由下面的公式得出:
上述公式可以写成:z = w向量的转置*x向量。向量x是分类器的输入数据,向量w就是我们要找到的最佳参数(系数),从而使分类器尽可能的准确。
下面的问题又成了我们怎么去找这个最佳参数:上面说了,我们用一些最优算法,这里我们要说的是梯度上升法
梯度上升法思想:找到某函数的最大值,最好的方法是沿着该函数的梯度方向探寻。
函数f(x,y)的梯度为:
我们计算最佳参数w的公式为:
这里的a成为步长,a后面的成为w的梯度。我们对它进行迭代,直到达到某个停止条件为止,比如迭代次数达到某个指定的值或者算法达到某个可以允许的误差范围。
说了这么多,下面我们就用代码实现一下,怎么使用梯度上升找到最佳参数
我们现在又的数据是,在testSet.txt文件中有100个样本点,每个点包含两个数值特征:X1和X2。最后一列是类别,如下图:
#最优化算法,Logistic回归
from numpy import *
def loadDataSet():
dataMat = [];labelMat = []
fr = open('testSet.txt')
for line in fr.readlines():
lineArr = line.strip().split()
dataMat.append([1.0,float(lineArr[0]), float(lineArr[1])])
labelMat.append(int(lineArr[2]))
#print(lineArr)
#print(dataMat)
#print(labelMat)
return dataMat,labelMat
#sigmoid函数
def sigmoid(inX):
return 1.0/(1+exp(-inX))
#利用上面公式求回归系数,回归梯度上升优化算法
def gradAscent(dataMatIn, classLabels):
dataMatrix = mat(dataMatIn)
labelMat = mat(classLabels).transpose()#矩阵转置
m,n = shape(dataMatrix)
alpha = 0.001#向目标移动的长度
maxCycles = 500#迭代次数
weights = ones((n,1))
for k in range(maxCycles):
h = sigmoid(dataMatrix*weights)
error = (labelMat - h)#计算真实类别与预测类别的差值
weights = weights + alpha * dataMatrix.transpose()*error#一步步迭代调整回归系数
return weights#返回训练好的回归系数运行结果测试:
&&& import logRegres
&&& dataArr,labelMat = logRegres.loadDataSet()
&&& logRegres.gradAscent(dataArr,labelMat)
matrix([[ 4.],
[-0.6168482 ]])
&&& 这就是我们要的回归系数(最佳参数)
下面的代码是利用最佳参数,画出决策边界,将两类分开
#画出数据集和Logistic回归最佳拟合直线的函数
def plotBestFit(weights):
import matplotlib.pyplot as plt
dataMat,labelMat = loadDataSet()
dataArr = array(dataMat)
n = shape(dataArr)[0]
xcord1 = [];ycord1 = []
xcord2 = [];ycord2 = []
for i in range(n):
if int(labelMat[i]) == 1:
xcord1.append(dataArr[i,1]);ycord1.append(dataArr[i,2])
xcord2.append(dataArr[i,1]);ycord2.append(dataArr[i,2])
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(xcord1,ycord1,s=30,c='red',marker='s')
ax.scatter(xcord2,ycord2,s=30,c='green')
x = arange(-3.0,3.0,0.1)
y = (-weights[0]-weights[1]*x)/weights[2]#最佳拟合直线
ax.plot(x,y)
plt.xlabel('X1');plt.ylabel('X2');
plt.show()运行结果:
下面我们来举一个例子,来查看一下Logistic的准确率:预测病马的死亡率
代码如下:
#Logistic回归分类函数
def classifyVector(inX,weights):
prob = sigmoid(sum(inX*weights))
if prob & 0.5:
return 1.0
return 0.0
def colicTest():
frTrain = open('horseColicTraining.txt')
= open('horseColicTest.txt')
trainingSet = [];trainingLabels = []
for line in frTrain.readlines():
currLine = line.strip().split('\t')
lineArr = []
for i in range(21):
lineArr.append(float(currLine[i]))
trainingSet.append(lineArr)
trainingLabels.append(float(currLine[21]))
#print(lineArr)
#print(trainingSet)
#print(trainingLabels)
trainWeights = stocGradAscent1(array(trainingSet),trainingLabels,500)
#print(trainWeights)
errorCount = 0;numTestVec = 0.0
for line in frTest.readlines():
numTestVec += 1.0
currLine = line.strip().split('\t')
lineArr = []
for i in range(21):
lineArr.append(float(currLine[i]))
if int(classifyVector(array(lineArr),trainWeights)) != int(currLine[21]):
errorCount += 1
#print(int(classifyVector(array(lineArr),trainWeights)))
errorRate = (float(errorCount)/numTestVec)
print(&the error rate of this test is : %f& % errorRate)
return errorRate
def mutiTest():
numTests = 10;errorSum = 0.0
for k in range(numTests):
errorSum += colicTest()
print(&after %d iterations the average error rate is : %f& % \
(numTests,errorSum/float(numTests)))代码运行结果:
&&& logRegres.mutiTest()
the error rate of this test is : 0.358209
the error rate of this test is : 0.328358
the error rate of this test is : 0.358209
the error rate of this test is : 0.417910
the error rate of this test is : 0.313433
the error rate of this test is : 0.328358
the error rate of this test is : 0.313433
the error rate of this test is : 0.298507
the error rate of this test is : 0.388060
the error rate of this test is : 0.358209
after 10 iterations the average error rate is : 0.346269
&&& 如果想知道预测结果可以在clicTest()函数里面得到。over
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:51958次
积分:1322
积分:1322
排名:千里之外
原创:77篇
转载:19篇
评论:11条
(1)(3)(2)(2)(1)(1)(4)(2)(2)(1)(3)(4)(1)(2)(5)(5)(6)(4)(1)(2)(4)(9)(31)适用:本文只针对com接口的基本使用方法记录,即获取一个com接口库如何安装及用C++调用其中的接口。不包括com接口的编写。
获取:假设现得到一个com接口库pta.dll。
安装:在cmd命令行内运行:
& & & & com是语言无关的接口,可以使用常见的语言调用,现演示的是使用vc++(vs2005)来调用:
& & & & (1)生成pta.dll的头文件。没有头文件,则无法使用接口函数等,无法编译通过。com接口遵循“IDL(接口描述语言)”,通过vs2005的集成imdl工具即可简单地将idl语言转换成“.c,.h”文件。在vs studo工具里面的“工具-&外部工具“里面增加OleView.Exe工具。如图。
& & & & & & & & & & & & & & & & & & & & & & & &
& & & & & & & & &&成功后可以通过&工具-&OLE/COM对象查看器&打开该工具,如图可打开pta.dll接口查看它的idl语言。
& & & & & & & & & & & & & & & & & & & & & & &&
& & & & & & & & & & & & & & & & & & & & & & &&
& & & & & & & & & 将打开的idl语言另存为pta.idl文件,然后在自己的vs工程里引入此文件,对该文件进行单独编译,会生成相应的”.c .h“文件,具体文件名在属性里面可配置。在生成的过程中有可能会出错,这是因为工具不是太完美,生成的代码会有一两处错误,比如某个接口未定义,这里要手动改一下定义的顺序等等,这些都是灵活变通就可以解决的。
& & & & & & & & & 将成功生成的pta.h,pta.c文件加入自己的工程,此时可以从工程移除pta.idl文件。
& & & & & & & & & 新建一个main.cpp作测试,要include的文件为windows.h和刚生成的pta.h文件。
& & & & & & & & & 初始化com: & & & & & & &CoInitialize(NULL);
& & & & & & & & & 获取pta接口: & & & &&
& & & & & & & & & & & & & 从生成的pta.h,pta.c中代码分析便可以知道有哪些接口,并且它的clsid和每个接口的riid号(关于clsid和riid号:都是guid号,编写接口的时候就已经定义好,就理解为一级目录和二级目录,通过这两个编号能确定一个接口,对c++来说就是一个类)。
& & & & & & &
& & & & & & & & & & & & & & & & & & & & & & & & & &&IiTrusPTA *&//比如要用pta.h里面的这个接口
& & & & & & & & & & & & & & & & & & & & & & & & & & HRESULT&hr = CoCreateInstance(CLSID_iTrusPTA,NULL,CLSCTX_INPROC_SERVER,IID_IiTrusPTA,(LPVOID
*)&puk);& &&
& & & & & & & & & & & & & 获取成功后puk就是该接口,可以根据pta.h头文件定义的函数进行调用使用。
& & & & & & & & 释放: & & & & & & & & & & & &程序退出调用:CoUninitialize(); & & & & & & & & &
总结: & & 以上便是普通的com接口一般流程。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:2026次
排名:千里之外温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
如果熟悉IDL的可视化编译环境(IDL Workbench),在第一步编写函数时候可以在IDL Workbench环境中编写,并且可以编译为.sav文件。在第二步编译函数时候可以单独启动ENVI Runtime模式。
用IDL编写自定义函数非常灵活,功能也非常强大,前提是需要IDL相关知识。
阅读(4423)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_',
blogTitle:'IDL自定义函数使用',
blogAbstract:'& ENVI 提供对 IDL 程序的访问的功能,可以使用内置的IDL 函数或者用户自定义IDL函数。这些函数要求它们接受一个或多个图像阵列作为输入,并且输出一个与输入波段具有相同行列的单波段二维数组作为计算结果。如下为一个自定义函数的基本格式:
FUNCTION bm_func, b1, [b2,..., bn, parameters and keywords]&
&& &&processing steps&
&&& &RETURN, result
blogTag:'idl,自定义函数',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:0,
publishTime:0,
permalink:'blog/static/',
commentCount:1,
mainCommentCount:1,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'',
hmcon:'1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}}

我要回帖

更多关于 根据具体地址调用函数 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信