VotingClassifier:不同的功能集 [英] VotingClassifier: Different Feature Sets

查看:61
本文介绍了VotingClassifier:不同的功能集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个不同的功能集(因此,行数相同且标签相同),在我的情况下是 DataFrames :

I have two different feature sets (so, with same number of rows and the labels are the same), in my case DataFrames:

df1 :

| A | B | C |
-------------
| 1 | 4 | 2 |
| 1 | 4 | 8 |
| 2 | 1 | 1 |
| 2 | 3 | 0 |
| 3 | 2 | 5 |

df2 :

| E | F |
---------
| 6 | 1 |
| 1 | 3 |
| 8 | 1 |
| 2 | 8 |
| 5 | 2 |

标签:

| labels |
----------
|    5   |
|    5   |
|    1   |
|    7   |
|    3   |

我想用它们来训练 VotingClassifier .但是拟合步骤仅允许指定一个功能集.目标是将 clf1 df1 匹配,将 clf2 df2 匹配.

I want to use them to train a VotingClassifier. But the fitting step only allows to specify a single feature set. Goal is to fit clf1 with df1 and clf2 with df2.

eclf = VotingClassifier(estimators=[('df1-clf', clf1), ('df2-clf', clf2)], voting='soft')
eclf.fit(...)

我应该如何处理这种情况?有什么简单的解决方案吗?

How should I proceed with this kind of situation? Is there any easy solution?

推荐答案

使自定义函数完成您想要实现的目标非常容易.

Its pretty easy to make custom functions to do what you want to achieve.

导入先决条件:

import numpy as np
from sklearn.preprocessing import LabelEncoder

def fit_multiple_estimators(classifiers, X_list, y, sample_weights = None):

    # Convert the labels `y` using LabelEncoder, because the predict method is using index-based pointers
    # which will be converted back to original data later.
    le_ = LabelEncoder()
    le_.fit(y)
    transformed_y = le_.transform(y)

    # Fit all estimators with their respective feature arrays
    estimators_ = [clf.fit(X, y) if sample_weights is None else clf.fit(X, y, sample_weights) for clf, X in zip([clf for _, clf in classifiers], X_list)]

    return estimators_, le_


def predict_from_multiple_estimator(estimators, label_encoder, X_list, weights = None):

    # Predict 'soft' voting with probabilities

    pred1 = np.asarray([clf.predict_proba(X) for clf, X in zip(estimators, X_list)])
    pred2 = np.average(pred1, axis=0, weights=weights)
    pred = np.argmax(pred2, axis=1)

    # Convert integer predictions to original labels:
    return label_encoder.inverse_transform(pred)

逻辑取自 VotingClassifier来源.

现在测试以上方法.首先获取一些数据:

Now test the above methods. First get some data:

from sklearn.datasets import load_iris
data = load_iris()
X = data.data
y = []

#Convert int classes to string labels
for x in data.target:
    if x==0:
        y.append('setosa')
    elif x==1:
        y.append('versicolor')
    else:
        y.append('virginica')

将数据拆分为训练并进行测试:

Split the data into train and test:

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y)

将X划分为不同的特征数据:

Divide the X into different feature datas:

X_train1, X_train2 = X_train[:,:2], X_train[:,2:]
X_test1, X_test2 = X_test[:,:2], X_test[:,2:]

X_train_list = [X_train1, X_train2]
X_test_list = [X_test1, X_test2]

获取分类列表:

from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC

# Make sure the number of estimators here are equal to number of different feature datas
classifiers = [('knn',  KNeighborsClassifier(3)),
    ('svc', SVC(kernel="linear", C=0.025, probability=True))]

将分类器与数据相匹配:

Fit the classifiers with the data:

fitted_estimators, label_encoder = fit_multiple_estimators(classifiers, X_train_list, y_train)

使用测试数据进行预测:

Predict using the test data:

y_pred = predict_from_multiple_estimator(fitted_estimators, label_encoder, X_test_list)

获取预测的准确性:

from sklearn.metrics import accuracy_score
print(accuracy_score(y_test, y_pred))

请随时询问是否有疑问.

Feel free to ask if any doubt.

这篇关于VotingClassifier:不同的功能集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆