有人知道如何将该python代码转换为Orange python格式吗?

问题描述

我很难将此代码(使用二进制PSO选择功能)在Orange Canvas上的python小部件中使用。在Orange上,语法上有一些差异,我已经尝试了很多更改,但是我仍然遇到“ AttributeError:'numpy.ndarray'对象没有属性'domain'”

# Import modules
import numpy as np
import pandas as pd
import pyswarms as ps
from sklearn import linear_model
import warnings
warnings.filterwarnings('ignore')



#from sklearn.datasets import make_classification
#X,y = make_classification(n_samples=100,n_features=15,n_classes=3,#n_informative=4,n_redundant=1,n_repeated=2,#random_state=1)
data1 = pd.read_csv('Intervalo.csv',delimiter= ';',header=0,index_col=False)
X = np.array(data1.iloc[:,0:8])
y = np.array(data1['Classe'])
df = pd.DataFrame(X)
df['labels'] = pd.Series(y)

# Create an instance of the classifier
classifier = linear_model.LogisticRegression()

# Define objective function
def f_per_particle(m,alpha):
    """Computes for the objective function per particle

    Inputs
    ------
    m : numpy.ndarray
        Binary mask that can be obtained from BinaryPSO,will
        be used to mask features.
    alpha: float (default is 0.5)
        Constant weight for trading-off classifier performance
        and number of features

    Returns
    -------
    numpy.ndarray
        Computed objective function
    """
    total_features = 8
    # Get the subset of the features from the binary mask
    if np.count_nonzero(m) == 0:
        X_subset = X
    else:
        X_subset = X[:,m==1]
    # Perform classification and store performance in P
    classifier.fit(X_subset,y)
    P = (classifier.predict(X_subset) == y).mean()
    # Compute for the objective function
    j = (alpha * (1.0 - P)
        + (1.0 - alpha) * (1 - (X_subset.shape[1] / total_features)))

    return j

def f(x,alpha=0.7):
    """Higher-level method to do classification in the
    whole swarm.

    Inputs
    ------
    x: numpy.ndarray of shape (n_particles,dimensions)
        The swarm that will perform the search

    Returns
    -------
    numpy.ndarray of shape (n_particles,)
        The computed loss for each particle
    """
    n_particles = x.shape[0]
    j = [f_per_particle(x[i],alpha) for i in range(n_particles)]
    return np.array(j)

# Initialize swarm,arbitrary
options = {'c1': 0.3,'c2': 0.8,'w':0.5,'k': 28,'p':2}

# Call instance of PSO
dimensions = 8 # dimensions should be the number of features
optimizer = ps.discrete.BinaryPSO(n_particles=60,dimensions=dimensions,options=options)

# Perform optimization
cost,pos = optimizer.optimize(f,iters=1000)

在橙子上,我使用的是这种方式:

import numpy as np
import pandas as pd
import pyswarms as ps
import Orange
from sklearn import linear_model
import warnings
warnings.filterwarnings('ignore')

data = Orange.data.Table(in_data)
X = data.X[:50]
y = data.Y[:50]

lr = Orange.classification.LogisticRegressionLearner()

# Define objective function
def f_per_particle(m,m==1]
    # Perform classification and store performance in P
    classifier = lr(X_subset,y)
    P = Orange.evaluation.scoring.CA(lr(X_subset) == y).mean()
    # Compute for the objective function
    j = (alpha * (1.0 - P)
        + (1.0 - alpha) * (1 - (X_subset.shape[1] / total_features)))

    return j

def f(x,iters=1000)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...