机器学习
MATLAB版本:MATLAB 7.10.0 (R2010a)
FULLBNT版本:FullBNT-1.0.7,下载地址http://vdisk.weibo.com/s/aJo2nLguE5QIa/1447305019
操作系统:win7 旗舰版
(1)解压FullBNT-1.0.7.zip压缩包,如解压后的文件夹为“E:\FullBNT-1.0.7”
(2)打开MATLAB,将E:\FullBNT-1.0.7文件夹下的所有目录添加到MATLAB search path里面,具体步骤为:
1)在MATLAB命令行里键入“pathtool”,会出现set path对话框。
2)点击对话框中的“Add with Subfolders”,选择FullBNT-1.0.7文件夹,这里是E:\FullBNT-1.0.7,然后点击save即可。
注意:有可能在点击save按钮的时候会出现“没有写pathdef.m文件夹的权限”的错误,这个时候只要关闭MATLAB,重新以管理员权限打开,按照上述步骤完成就可以了。
(3)测试:在MATLAB命令行中键入“test_BNT”命令,如果正常执行,则可正常使用了!
源代码:
#coding:gbk
#filename:scheme_decision_making.py
#完成题目:
# 第七题:多属性方案决策的权重敏感性可视化
#方案决策决策模型:TOPSIS
from numpy import *
import math
import wx
import matplotlib
import matplotlib.pyplot as plt
def printArray(array_0):
'''本函数为调试函数,功能为打印矩阵'''
for eachRow in array_0:
for eachData in eachRow:
print eachData ,
print '\n'
def autoNorm(projects):
'''用'向量规范法'对用户输入的方案指标信息进行归一化'''
'''归一化方程为:y=y/qrt(sum(yi^2))'''
projects_norm=[]
m=len(projects[0])
sum2Num=[0]*m
for eachP in projects:
i=0
for eachW in eachP:
sum2Num[i]=sum2Num[i]+eachW*eachW
i=i+1
for eachP in projects:
project=[]
i=0
for eachW in eachP:
project.append(eachW/math.sqrt(sum2Num[i]))
i=i+1
projects_norm.append(project)
return projects_norm
def referenceSolution(projects_norm_weight):
'''确定理想和负理想解'''
m=len(projects_norm_weight[0])
bestSolution=[0]*m
worstSolution=[9999999999]*m
for eachP in projects_norm_weight:
i=0
for eachW in eachP:
if eachW>=bestSolution[i]:
bestSolution[i]=eachW
if eachW<=worstSolution[i]:
worstSolution[i]=eachW
i=i+1
return bestSolution,worstSolution
def oushijuli(vector1,vector2):
'''计算两个向量的欧氏距离'''
n=len(vector1)
distance=0
for i in range(0,n):
distance=distance+(vector1[i]-vector2[i])*\
(vector1[i]-vector2[i])
distance=math.sqrt(distance)
return distance
def schemeDecision(projects,weights):
'''算法核心程序:根据TOPSIS模型进行方案决策'''
'''projects为方案指标信息构成的矩阵'''
'''weights为权重值'''
#printArray(projects)
#print weights
projects_norm=autoNorm(projects)
#printArray(projects_norm)
projects_norm_weight=[]
for eachP in projects_norm:
project=[]
i=0
for eachW in eachP:
project.append(eachW*weights[i])
i=i+1
projects_norm_weight.append(project)
#printArray(projects_norm_weight)
bestSolution,worstSolution=referenceSolution(projects_norm_weight)
#print bestSolution
#print worstSolution
tobest=[]
toworst=[]
for eachP in projects_norm_weight:
tobest.append(oushijuli(eachP,bestSolution))
toworst.append(oushijuli(eachP,worstSolution))
#print tobest
#print toworst
n=len(tobest)
resultDistance=[]
for i in range(0,n):
if tobest[i]==toworst[i]:
resultDistance.append(0.5)
else:
resultDistance.append(toworst[i]/(tobest[i]+toworst[i]))
#print resultDistance
projectDecision=0
for i in range(1,n):
if resultDistance[i]>resultDistance[projectDecision]:
projectDecision=i
#print projectDecision+1
return projectDecision+1
def drawRelationalGraph(evt):
'''生成最优方案与权重分布关系图'''
projects=[]
try:
targets_1=[float(w_1_1.GetValue()),float(w_1_2.GetValue()),float(w_1_3.GetValue())]
targets_2=[float(w_2_1.GetValue()),float(w_2_2.GetValue()),float(w_2_3.GetValue())]
targets_3=[float(w_3_1.GetValue()),float(w_3_2.GetValue()),float(w_3_3.GetValue())]
except:
dlg=wx.MessageDialog(None,"请在方案指标信息录入中输入整数或小数!","错误提示")
dlg.ShowModal()
dlg.Destroy()
return
projects.append(targets_1)
projects.append(targets_2)
projects.append(targets_3)
step=int(slider.GetValue())
xList_1=[]
xList_2=[]
xList_3=[]
yList_1=[]
yList_2=[]
yList_3=[]
x_coor=range(0,100,step)
y_coor=range(0,100,step)
for eachXNum in x_coor:
for eachYNum in y_coor:
x=float(eachXNum)/100
y=float(eachYNum)/100
weights=[x,y,1-x-y]
result=schemeDecision(projects,weights)
if result==1:
xList_1.append(x)
yList_1.append(y)
elif result==2:
xList_2.append(x)
yList_2.append(y)
elif result==3:
xList_3.append(x)
yList_3.append(y)
if len(xList_1)+len(xList_2)+len(xList_3)>0:
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
ax.set_xlabel('W1')
ax.set_ylabel('W2')
plot1=ax.scatter(xList_1,yList_1,color=['r']*len(xList_1))
plot2=ax.scatter(xList_2,yList_2,color=['g']*len(xList_2))
plot3=ax.scatter(xList_3,yList_3,color=['y']*len(xList_3))
plotList=[]
labelList=[]
if len(xList_1)>0:
plotList.append(plot1)
labelList.append('Project_1')
if len(xList_2)>0:
plotList.append(plot2)
labelList.append('Project_2')
if len(xList_3)>0:
plotList.append(plot3)
labelList.append('Project_3')
plt.legend(plotList,labelList)
plt.show()
def onceJuece(evt):
'''进行单次方案决策'''
projects=[]
try:
targets_1=[float(w_1_1.GetValue()),float(w_1_2.GetValue()),float(w_1_3.GetValue())]
targets_2=[float(w_2_1.GetValue()),float(w_2_2.GetValue()),float(w_2_3.GetValue())]
targets_3=[float(w_3_1.GetValue()),float(w_3_2.GetValue()),float(w_3_3.GetValue())]
except:
dlg=wx.MessageDialog(None,"请在方案指标信息录入中输入整数或小数!","错误提示")
dlg.ShowModal()
dlg.Destroy()
return
projects.append(targets_1)
projects.append(targets_2)
projects.append(targets_3)
try:
x=float(quanzhong1.GetValue())
y=float(quanzhong2.GetValue())
except:
dlg=wx.MessageDialog(None,"请在特定权重决策中输入0-1的小数!","错误提示")
dlg.ShowModal()
dlg.Destroy()
return
if x>1 or x<0 or y>1 or y<0 or x+y>1:
dlg=wx.MessageDialog(None,"W1和W2必须处于0-1之间并且满足和小于1!","错误提示")
dlg.ShowModal()
dlg.Destroy()
return
weights=[x,y,1-x-y]
result=schemeDecision(projects,weights)
dlg=wx.MessageDialog(None,"当W1="+str(x)+",W2="+str(y)+\
"时,最优决策为方案"+str(result),"最优决策结果")
dlg.ShowModal()
dlg.Destroy()
if __name__=="__main__":
'''主程序,主要是界面代码'''
app=wx.App()
winlong=435
winwidth=455
win=wx.Frame(None,title='多属性方案决策的权重敏感性可视化',size=(winwidth,winlong))
win.SetMaxSize((winwidth,winlong))
win.SetMinSize((winwidth,winlong))
panel=wx.Panel(win)
sb = wx.StaticBox(panel, label="方案指标信息录入")
boxsizer = wx.StaticBoxSizer(sb, wx.VERTICAL)
kongbai=wx.StaticText(panel,label='')
shuxing1=wx.StaticText(panel,label='指标1')
shuxing2=wx.StaticText(panel,label='指标2')
shuxing3=wx.StaticText(panel,label='指标3')
fangan1=wx.StaticText(panel,label='方案1')
fangan2=wx.StaticText(panel,label='方案2')
fangan3=wx.StaticText(panel,label='方案3')
w_1_1=wx.TextCtrl(panel,value='1')
w_1_2=wx.TextCtrl(panel,value='1')
w_1_3=wx.TextCtrl(panel,value='1')
w_2_1=wx.TextCtrl(panel,value='1')
w_2_2=wx.TextCtrl(panel,value='1')
w_2_3=wx.TextCtrl(panel,value='1')
w_3_1=wx.TextCtrl(panel,value='1')
w_3_2=wx.TextCtrl(panel,value='1')
w_3_3=wx.TextCtrl(panel,value='1')
fgs = wx.FlexGridSizer(4,4,5,5)
fgs.AddMany([(kongbai),(shuxing1),(shuxing2),(shuxing3),\
(fangan1),(w_1_1),(w_1_2),(w_1_3),\
(fangan2),(w_2_1),(w_2_2),(w_2_3),\
(fangan3),(w_3_1),(w_3_2),(w_3_3)])
boxsizer.Add(fgs,flag=wx.ALL|wx.EXPAND, border=10)
buttonbox=wx.BoxSizer()
kongbai2=wx.StaticText(panel,label='',size=((winwidth-250)/2,-1))
kongbai3=wx.StaticText(panel,label='',size=((winwidth-250)/2,-1))
buttonbox.Add(kongbai2)
fenxibutton=wx.Button(panel,label='最优方案与权重分布关系图',size=(250,-1))
fenxibutton.Bind(wx.EVT_BUTTON,drawRelationalGraph)
buttonbox.Add(fenxibutton)
buttonbox.Add(kongbai3)
hbox = wx.BoxSizer(wx.VERTICAL)
hbox.Add(boxsizer,flag=wx.ALL|wx.EXPAND, border=15)
sb2 = wx.StaticBox(panel, label="特定权重决策")
boxsizer2 = wx.StaticBoxSizer(sb2, wx.VERTICAL)
tedingquanzhong1=wx.StaticText(panel,label='W1:')
tedingquanzhong2=wx.StaticText(panel,label='W2:')
quanzhong1=wx.TextCtrl(panel,value='0.3')
quanzhong2=wx.TextCtrl(panel,value='0.3')
juecebutton=wx.Button(panel,label='最优决策',size=(80,25))
juecebutton.Bind(wx.EVT_BUTTON,onceJuece)
fgs2 = wx.FlexGridSizer(1,5,5,5)
fgs2.AddMany([(tedingquanzhong1),(quanzhong1),\
(tedingquanzhong2),(quanzhong2),(juecebutton)])
boxsizer2.Add(fgs2,flag=wx.ALL|wx.EXPAND, border=10)
hbox.Add(boxsizer2,flag=wx.LEFT|wx.RIGHT|wx.EXPAND, border=15)
hengbox=wx.BoxSizer()
quanzhongstep=wx.StaticText(panel,label='权重变化步长(%)',size=(110,45),\
style=wx.ALIGN_CENTER)
slider = wx.Slider(panel, 100, 5, 1, 100,size=(250, -1),\
style=wx.SL_HORIZONTAL | wx.SL_AUTOTICKS | wx.SL_LABELS )
slider.SetTickFreq(5, 1)
hengbox.Add(quanzhongstep)
hengbox.Add(slider)
hbox.Add(hengbox,flag=wx.ALL|wx.EXPAND,border=10)
line = wx.StaticLine(panel)
hbox.Add(line,flag=wx.EXPAND|wx.BOTTOM,border=10)
hbox.Add(buttonbox,flag=wx.ALL|wx.EXPAND)
panel.SetSizer(hbox)
win.Center()
win.Show()
app.MainLoop()
http://blog.sina.com.cn/s/blog_818f5fde0102vvnh.html
在利用支持向量机进行分类的时候怎么选择合适的核函数?
之前在面试某公司的时候,提到曾经在项目中利用SVM进行分类。面试官提的其中一个问题就是当时是怎么选择核函数的。之前在做项目时,其实并没有想过这个问题,直接默认用的RBF核,效果也不错,所以就实话实说了。但是明显感觉面试官对我的回答不是很满意,多半觉得我做事情不够精益求精。于是,过后在知乎上面专门问了这个问题(http://www.zhihu.com/question/33268516),非常开心很多人都分享了自己的想法和经验。下面的总结多半来自知乎上的回答,页面中的图片也非原创,只为省事儿,如有侵权,求原谅。
(1)先介绍一下常见核函数的类别
常见核函数也就几种,线性核、径向基核(高斯核函数)、多项式核函数、sigmoid核函数
(2)实际应用中对于核函数选择的态度
1)获赞最多的答案是Andrew Ng的理论,由“邱霸气”在知乎上回答。
Andrew Ng理论1:当数据量足够庞大时,feature足够多时,所有的分类算法最终的效果都差不多。也就是说,不管你选用什么样的核,在训练集够大的情况下都是然并卵。当然,就分类效果来说,非线性的比线性的核好一些。但线性的也能够有很不错的分类效果,而且计算量比非线性小,所以需要具体情况具体分析。
Andrew Ng理论2:老实人Andrew教你如何选择合适的SVM核。
情况1:当训练集不大,feature比较多的时候,用线性的核。因为多feature的情况下就已经可以给线性的核提供不错的variance去fit训练集。
情况2:当训练集相对可观,而feature比较少,用非线性的核。因为需要算法提供更多的variance去fit训练集。情况3:feature少,训练集非常大,用线性的核。因为非线性的核需要的计算量太大了。而庞大的训练集,本身就可以给非线性的核提供很好的分类效果。
2)对每种核函数进行交叉验证,取效果最好的核函数
感觉这种方法应该是最好的,但是实施起来,你懂得……
3)跟我一样的消极党:认为rbf核是最通用的核函数,如果是用SVM做应用,一般不需要在核函数上下功夫,线性情况就用线性核,非线性的时候用rbf核就可以了。甚至有时候会发现非线性的情况下用线性核效果也会很好^O^
4)比我还消极的党:随便选一个就行了,不行就换一个,这根本就没啥可说。而且一般也不会用svm,因为速度慢,线性svm的话被logistic完爆
(3)学术上关于核函数选择的方法
1)《A Practical Guide to Support Vector Classification》,网址:http://www.csie.ntu.edu.tw/~cjlin/papers/guide/guide.pdf
2)mutiple kernel learning,一堆kernel放一起,让模型自己学出个最好的kernel,论文《Multiple Kernel Learning Algorithms》,网址:http://users.ics.aalto.fi/gonen/files/gonen_jmlr11_paper.pdf
反正我没看过这些论文,感兴趣的可以看看
评论
重要提示:警惕虚假中奖信息