该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
谁能将下面这段代码用matlab实现?
from __future__ import division
import copy
#计算|X|
def ind(samples = []):
#根据每行数据的值进行分组,值和数据条数存在列表number中
number = []
for u in samples:
if len(number) == 0:
number.append([u,1])
else:
flag = 0 #变量flag表示u是否在number中
for i in number:
if i[0] == u:
i[1] = i[1] + 1
flag = 1
if flag ==0:#u不在number中则新添一条记录到number中
number.append([u,1])
X = 0 #X表示|X|
for i in number:
X = X + i[1]*i[1]
return X
#计算单属性重要度,结果保存到列表Sig中
def Sig(samples = []):
Sig = []
for i in range(len(samples[0])):
samplesX = copy.deepcopy(samples)
for u in samplesX:
u.pop(i)
Sig.append(round((1-ind(samples)/ind(samplesX)),2))
return Sig
#用双属性组合方法计算重要度,结果保存到列表Sig2中
def Sig2(samples = []):
Sig = []
Sig2 = []
SigDouble = []#保存双属性组合重要度
for i in range(len(samples[0])):
samplesX = copy.deepcopy(samples)
SigDouble.append([])
for u in samplesX:
u.pop(i)
Sig.append(round((1-ind(samples)/ind(samplesX)),2)) #计算单属性重要度
for j in range(len(samplesX[0])):
samplesY = copy.deepcopy(samplesX)
for u in samplesY:
u.pop(j)
SigDouble[i].append(round(( 1-ind(samples)/ind(samplesY)),2))
for i in range(len(Sig)):
sum_SigDouble = 0
for j in SigDouble[i]:
sum_SigDouble = sum_SigDouble + (j-Sig[i])
Sig2.append(round(abs((Sig[i]-sum_SigDouble/2/3)),3))
print Sig,SigDouble,Sig2
return Sig2
#计算各属性客观权重,结果保存到列表P中
def P(Sig = []):
P = []
sum_Sig = 0
for i in Sig:
sum_Sig = sum_Sig + i
for i in Sig:
P.append(round(i/sum_Sig,3))
return P
samples = []
for line in open(*samples.txt*):
u = line.strip(*\n*).split(*\t*) #去掉换行符,用tab分割字符
samples.append(u) #获得完整样本
#去掉决策属性
n = len(samples[0])
for u in samples:
u.pop(n-1)
print u*属性客观权重:*
print P(Sig(samples))
print u*双属性组合方法客观权重:*
print P(Sig2(samples))