Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 尝试选择特征时,代码进入无限循环_Python_Python 3.x_Machine Learning_Scikit Learn_Feature Selection - Fatal编程技术网

Python 尝试选择特征时,代码进入无限循环

Python 尝试选择特征时,代码进入无限循环,python,python-3.x,machine-learning,scikit-learn,feature-selection,Python,Python 3.x,Machine Learning,Scikit Learn,Feature Selection,我正在尝试使用scikit learn的递归特征消除和交叉验证来处理有二进制类问题的(5000,37)数据,每当我适合模型时,算法就会进入无限循环。 目前,我在下面这个例子:关于如何使用这个算法 我的数据是: from sklearn.svm import SVC from sklearn.model_selection import StratifiedKFold from sklearn.feature_selection import RFECV

我正在尝试使用scikit learn的递归特征消除和交叉验证来处理有二进制类问题的
(5000,37)
数据,每当我适合模型时,算法就会进入无限循环。 目前,我在下面这个例子:关于如何使用这个算法

我的数据是:

    from sklearn.svm import SVC
    from sklearn.model_selection import StratifiedKFold
    from sklearn.feature_selection import RFECV
    
        X = np.random.randint(0,363175645.191632,size=(5000, 37))
        Y = np.random.choice([0, 1], size=(37,))
我尝试通过以下方式选择功能:

    svc = SVC(kernel="linear")
    rfecv = RFECV(estimator=svc, step=1, cv=StratifiedKFold(2),
                  scoring='accuracy')
    
    rfecv.fit(X, Y)

代码挂起并进入无限循环,但是当我尝试使用另一种算法(如ExtraTreesClassifier)时,它工作得很好,发生了什么,请帮助?

当您执行svm时,因为它是基于距离的,所以缩放您的特征变量是有意义的,特别是在它们很大的情况下。你也可以退房。使用示例数据集:

from sklearn.datasets import make_blobs
import seaborn as sns
import numpy as np
from sklearn.preprocessing import StandardScaler

Scaler =  StandardScaler()

X, y = make_blobs(n_samples=5000, centers=3, shuffle=False,random_state=42)
X = np.concatenate((X,np.random.randint(0,363175645.191632,size=(5000,35))),axis=1)
y = (y==1).astype('int')

X_scaled = Scaler.fit_transform(X)
此数据集在前两列中只有2个有用的变量,如图所示:

plt.scatter(x=X_scaled[:,0],y=X_scaled[:,1],c=['k' if i else 'b' for i in y])

现在我们对缩放数据运行rfe,可以看到它返回前两列作为顶部变量:

from sklearn.svm import SVC
from sklearn.model_selection import StratifiedKFold
from sklearn.feature_selection import RFECV

svc = SVC(kernel="linear")
rfecv = RFECV(estimator=svc, step=1, cv=StratifiedKFold(2),scoring='accuracy')
rfecv.fit(X_scaled, y)

rfecv.ranking_

array([ 1,  2, 17, 28, 33, 22, 23, 26,  6, 19, 20,  4, 10, 25,  3, 27, 11,
        8, 18,  5, 29, 14,  7, 21,  9, 13, 24, 30, 35, 31, 32, 34, 16, 36,
       37, 12, 15])

那么,我应该缩放数据吗?是的,你绝对应该。你也可以试试minmax