问题描述
我在我的数据集上使用 pyswarms 文档进行功能日落选择:https://pyswarms.readthedocs.io/en/development/examples/feature_subset_selection.html
但是我不断收到错误消息:ValueError:找不到元组类型的键,而不是多索引。
我不确定我做错了什么。如果有人可以提供帮助,我将不胜感激。我的数据集由文本列和标签列组成,该列与文本对应的 0 或 1。我会撒谎说我使用了提供的超链接中的确切代码来查看它如何在数据集上工作。
代码:
from sklearn import linear_model
# Create an instance of the classifier
classifier = linear_model.LogisticRegression()
# Define objective function
def f_per_particle(m,alpha):
total_features = 15
# 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.88):
"""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)
options = {'c1': 0.5,'c2': 0.5,'w':0.9,'k': 30,'p':2}
# Call instance of PSO
dimensions = 15 # dimensions should be the number of features
optimizer.reset()
optimizer = ps.discrete.BinaryPSO(n_particles=30,dimensions=dimensions,options=options)
# Perform optimization
cost,pos = optimizer.optimize(f,iters=1000)
这是我收到的错误:
--------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-331-56026857bfc8> in <module>
7
8 # Perform optimization
----> 9 cost,iters=1000)
~\Anaconda3\lib\site-packages\pyswarms\discrete\binary.py in optimize(self,objective_func,iters,n_processes,**kwargs)
175 # Compute cost for current position and personal best
176 self.swarm.current_cost = compute_objective_function(
--> 177 self.swarm,pool,**kwargs
178 )
179 self.swarm.pbest_pos,self.swarm.pbest_cost = compute_pbest(
~\Anaconda3\lib\site-packages\pyswarms\backend\operators.py in compute_objective_function(swarm,**kwargs)
237 """
238 if pool is None:
--> 239 return objective_func(swarm.position,**kwargs)
240 else:
241 results = pool.map(
<ipython-input-330-1fee16a99ecd> in f(x,alpha)
14 """
15 n_particles = x.shape[0]
---> 16 j = [f_per_particle(x[i],alpha) for i in range(n_particles)]
17 return np.array(j)
<ipython-input-330-1fee16a99ecd> in <listcomp>(.0)
14 """
15 n_particles = x.shape[0]
---> 16 j = [f_per_particle(x[i],alpha) for i in range(n_particles)]
17 return np.array(j)
<ipython-input-329-def50f09accb> in f_per_particle(m,alpha)
27 X_subset = X
28 else:
---> 29 X_subset = X[:,m==1]
30 # Perform classification and store performance in P
31 classifier.fit(X_subset,y)
~\Anaconda3\lib\site-packages\pandas\core\series.py in __getitem__(self,key)
904 return self._get_values(key)
905
--> 906 return self._get_with(key)
907
908 def _get_with(self,key):
~\Anaconda3\lib\site-packages\pandas\core\series.py in _get_with(self,key)
919 )
920 elif isinstance(key,tuple):
--> 921 return self._get_values_tuple(key)
922
923 elif not is_list_like(key):
~\Anaconda3\lib\site-packages\pandas\core\series.py in _get_values_tuple(self,key)
954
955 if not isinstance(self.index,MultiIndex):
--> 956 raise ValueError("key of type tuple not found and not a MultiIndex")
957
958 # If key is contained,would have returned by Now
ValueError: key of type tuple not found and not a MultiIndex
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)