如何解决有人知道如何将该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 举报,一经查实,本站将立刻删除。