Получение 10 лучших и 10 нижних функций - PullRequest
0 голосов
/ 27 октября 2018

Я новичок в Python, пытающийся научиться извлекать верхние 10 и нижние 10 из списка, который я создал с помощью этого кода:

clftest = logres1.fit(X_train,y_train)

#getting the feature's coefficient
feature_importance = clftest.coef_[0]

#creating an array to identify the highest and lowest value
sorter = np.argsort(feature_importance)

#using the shape of sorter, arrange it from lowest to highest
position = np.arange(sorter.shape[0])


featfig = plt.figure(figsize=(100,100))
featax = featfig.add_subplot(1, 1, 1)
featax.barh(position, feature_importance[sorter], align="center")
featax.set_yticks(position)
featax.set_yticklabels(np.array(X.columns)[sorter], fontsize=8)

plt.show()

Как вы можете видеть, есть многофункции включают в мой график ...

Кроме того, мне интересно, есть ли сокращение для этого или это уже самая короткая строка кода ..

Result of the code above

Ответы [ 2 ]

0 голосов
/ 27 октября 2018

Предположим, у вас есть следующий массив с весами объектов

coef =  array([  1.88300851e+00,   9.85092999e-02,  -5.65726689e-02,
                -6.15194157e-06,  -1.47064483e-01,  -3.80980229e-01,
                -5.74536851e-01,  -2.95280519e-01,  -2.40004639e-01,
                -3.51240376e-02,  -9.66881225e-03,   1.24471692e+00,
                 4.37321571e-02,  -9.20868564e-02,  -1.44701472e-02,
                -9.55498577e-03,  -4.33660677e-02,  -3.42427309e-02,
                -4.17388237e-02,   3.75241446e-03,   1.11771818e+00,
                -3.16367948e-01,  -9.05980063e-02,  -2.56441451e-02,
                -2.61484045e-01,  -1.22299461e+00,  -1.57351240e+00,
                -6.03878651e-01,  -7.25284179e-01,  -1.29895629e-01])

, вы можете получить индексы отсортированного массива объектов в порядке убывания:

sorter = np.argsort(-coef)
sorter
array([ 0, 11, 20,  1, 12, 19,  3, 15, 10, 14, 23, 17,  9, 18, 16,  2, 22,
       13, 29,  4,  8, 24,  7, 21,  5,  6, 27, 28, 25, 26])

, затем вы можете получить10 лучших функций, таких как:

top_ten_arg = sorter[:10]
coef[top_ten_arg]
array([  1.88300851e+00,   1.24471692e+00,   1.11771818e+00,
         9.85092999e-02,   4.37321571e-02,   3.75241446e-03,
        -6.15194157e-06,  -9.55498577e-03,  -9.66881225e-03,
        -1.44701472e-02])

и аналогично получают 10 самых низких функций, таких как:

lowest_ten_arg = sorter[-10:]
coef[lowest_ten_arg]
array([-0.24000464, -0.26148405, -0.29528052, -0.31636795, -0.38098023,
       -0.57453685, -0.60387865, -0.72528418, -1.22299461, -1.5735124 ])

обратите внимание, что это только дает вам вес функций, чтобы вы получили имена функций, которые вы только чтонужно использовать top_ten_arg и lowest_ten_arg на X.columns, как вы это сделали с сортировщиком

0 голосов
/ 27 октября 2018

Попробуйте это:

clftest = logres1.fit(X_train,y_train)

#getting the feature's coefficient
feature_importance = clftest.coef_[0]

#creating an array to identify the highest and lowest value
sorter = np.argsort(feature_importance)

#add 2 rows in you code
n = 10 # this is number of features top
sorter = np.append(sorter[:n],sorter[-n:]) #this is fixed code

#using the shape of sorter, arrange it from lowest to highest
position = np.arange(sorter.shape[0])


featfig = plt.figure(figsize=(100,100))
featax = featfig.add_subplot(1, 1, 1)
featax.barh(position, feature_importance[sorter], align="center")
featax.set_yticks(position)
featax.set_yticklabels(np.array(X.columns)[sorter], fontsize=8)

plt.show()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...