请将下面的matlab代码转换为python代码:function OneError=One_error(Outputs,test_target) [num_class,num_instance]=size(Outputs); temp_Outputs=[]; temp_test_target=[]; for i=1:num_instance temp=test_target(:,i); if((sum(temp)~=num_class)&(sum(temp)~=-num_class)) temp_Outputs=[temp_Outputs,Outputs(:,i)]; temp_test_target=[temp_test_target,temp]; end end Outputs=temp_Outputs; test_target=temp_test_target; [num_class,num_instance]=size(Outputs); Label=cell(num_instance,1); not_Label=cell(num_instance,1); Label_size=zeros(1,num_instance); for i=1:num_instance temp=test_target(:,i); Label_size(1,i)=sum(temp==ones(num_class,1)); for j=1:num_class if(temp(j)==1) Label{i,1}=[Label{i,1},j]; else not_Label{i,1}=[not_Label{i,1},j]; end end end oneerr=0; for i=1:num_instance indicator=0; temp=Outputs(:,i); [maximum,index]=max(temp); for j=1:num_class if(temp(j)==maximum) if(ismember(j,Label{i,1})) indicator=1; break; end end end if(indicator==0) oneerr=oneerr+1; end end OneError=oneerr/num_instance;
时间: 2023-06-26 16:06:48 浏览: 357
matlab源码求一元函数-python-num:测试
以下是将matlab代码转换为python代码的结果:
```python
import numpy as np
def One_error(Outputs, test_target):
num_class, num_instance = Outputs.shape
temp_Outputs = np.empty((num_class, 0))
temp_test_target = np.empty((num_class, 0))
for i in range(num_instance):
temp = test_target[:, i]
if (np.sum(temp) != num_class) and (np.sum(temp) != -num_class):
temp_Outputs = np.concatenate((temp_Outputs, Outputs[:, i].reshape(num_class, 1)), axis=1)
temp_test_target = np.concatenate((temp_test_target, temp.reshape(num_class, 1)), axis=1)
Outputs = temp_Outputs
test_target = temp_test_target
num_class, num_instance = Outputs.shape
Label = [None] * num_instance
not_Label = [None] * num_instance
Label_size = np.zeros((1, num_instance))
for i in range(num_instance):
temp = test_target[:, i]
Label_size[0, i] = np.sum(temp == np.ones((num_class, 1)))
for j in range(num_class):
if temp[j] == 1:
if Label[i] is None:
Label[i] = [j + 1]
else:
Label[i].append(j + 1)
else:
if not_Label[i] is None:
not_Label[i] = [j + 1]
else:
not_Label[i].append(j + 1)
oneerr = 0
for i in range(num_instance):
indicator = 0
temp = Outputs[:, i]
maximum = np.max(temp)
index = np.argmax(temp)
for j in range(num_class):
if temp[j] == maximum:
if (j + 1) in Label[i]:
indicator = 1
break
if indicator == 0:
oneerr += 1
OneError = oneerr / num_instance
return OneError
```
需要注意的是,matlab中的数组索引从1开始,而python中的数组索引从0开始,因此在转换过程中需要将所有的索引减1。
阅读全文