以神经网络为组合技术的集成学习

2024-04-19 11:34:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用3个基本学习者的能力实现集成学习模型KNN;DT和RF,然后在下面的示例中使用加权技术组合预测结果神经网络和感知器作为基于优化权重的组合技术,直到找到最佳权重,从而确定模型的性能。我在实现模型时遇到以下错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-36-bd170b55dfe3> in <module>()
     35 p = Perceptron(random_state=42, max_iter=10)
     36 #fit the model
---> 37 p.fit(pred, y_test)
     38 for value in pred:
     39     pr = p.predict([value])

~\Anaconda3\lib\site-packages\sklearn\linear_model\stochastic_gradient.py in fit(self, X, y, coef_init, intercept_init, sample_weight)
    584                          loss=self.loss, learning_rate=self.learning_rate,
    585                          coef_init=coef_init, intercept_init=intercept_init,
--> 586                          sample_weight=sample_weight)
    587 
    588 

~\Anaconda3\lib\site-packages\sklearn\linear_model\stochastic_gradient.py in _fit(self, X, y, alpha, C, loss, learning_rate, coef_init, intercept_init, sample_weight)
    416             self.classes_ = None
    417 
--> 418         X, y = check_X_y(X, y, 'csr', dtype=np.float64, order="C")
    419         n_samples, n_features = X.shape
    420 

~\Anaconda3\lib\site-packages\sklearn\utils\validation.py in check_X_y(X, y, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, warn_on_dtype, estimator)
    571     X = check_array(X, accept_sparse, dtype, order, copy, force_all_finite,
    572                     ensure_2d, allow_nd, ensure_min_samples,
--> 573                     ensure_min_features, warn_on_dtype, estimator)
    574     if multi_output:
    575         y = check_array(y, 'csr', force_all_finite=True, ensure_2d=False,

~\Anaconda3\lib\site-packages\sklearn\utils\validation.py in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
    449         if not allow_nd and array.ndim >= 3:
    450             raise ValueError("Found array with dim %d. %s expected <= 2."
--> 451                              % (array.ndim, estimator_name))
    452         if force_all_finite:
    453             _assert_all_finite(array)

ValueError: Found array with dim 3. Estimator expected <= 2. 

这是集合模型的代码

 import pandas
    from sklearn import model_selection
    from sklearn.ensemble import RandomForestClassifier
    from sklearn.cross_validation import train_test_split
    import numpy as np
    from sklearn import tree
    from sklearn.neighbors import KNeighborsClassifier
    from sklearn.datasets import load_iris
    from sklearn.linear_model import Perceptron
    iris = load_iris()
    np.random.seed(1)
    X=iris.data
    y=iris.target
    Y = (iris.target==0).astype(np.int8)

    X_train, X_test, y_train, y_test =model_selection.train_test_split(
        X,Y, test_size=0.3, random_state=123)
    #Build ensemble model using neural netowork as combination
    model1 = tree.DecisionTreeClassifier(random_state=1)
    model2 = KNeighborsClassifier()
    model3 = RandomForestClassifier()
    model1.fit(X_train,y_train)
    model2.fit(X_train,y_train)
    model3.fit(X_train,y_train)
    pred1=model1.predict(X_test)
    pred2=model2.predict(X_test)
    pred3=model3.predict(X_test)
    #Combination of results and detmination of weights using neural network 
    #First  trial using simple perceptron 
    #input layer containing the three neurons representing the results of prediction 
    pred=[[pred1,pred2,pred3]]
    #output layer containg y_test 
    out=y_test 
    #creating a perceptron model
    p = Perceptron(random_state=42, max_iter=10)
    #fit the model
    p.fit(pred, y_test)
    for value in pred:
        pr = p.predict([value])
        print([pr])

Tags: infromtestimportmodelinittrainsklearn
1条回答
网友
1楼 · 发布于 2024-04-19 11:34:59

我做了修正,我在分裂长度上犯了错误,但不幸的是,我得到了一个可怕的结果,我必须努力改进它模型拟合过度,需要大量的修正,下面我分享代码:

import pandas
from sklearn import model_selection
from sklearn.ensemble import RandomForestClassifier
from sklearn.cross_validation import train_test_split
import numpy as np
from sklearn import tree
from sklearn.neighbors import KNeighborsClassifier
from sklearn.datasets import load_iris
from sklearn.linear_model import Perceptron
iris = load_iris()
np.random.seed(1)
X=iris.data
y=iris.target
Y = (iris.target==0).astype(np.int8)

X_train, X_test, y_train, y_test =model_selection.train_test_split(
    X,Y, test_size=0.3, random_state=123)
#Build ensemble model using neural netowork as combination
model1 = tree.DecisionTreeClassifier(random_state=1)
model2 = KNeighborsClassifier()
model3 = RandomForestClassifier()
model1.fit(X_train,y_train)
model2.fit(X_train,y_train)
model3.fit(X_train,y_train)
pred1=model1.predict(X_test)
pred2=model2.predict(X_test)
pred3=model3.predict(X_test)
#Combination of results and detmination of weights using neural network 
#First  trial using simple perceptron 
#input layer containing the three neurons representing the results of prediction 
pred=np.array([pred1,pred2,pred3]).T
#split dataset
from sklearn.model_selection import train_test_split
X_train1, X_test1, y_train1, y_test1 = train_test_split(pred, out)
#output layer containg y_test 
out=y_test 
#creating a perceptron model
perceptron = Perceptron(random_state=42, max_iter=10)
#fit the model
perceptron.fit(X_train1,y_train1)
predictions = perceptron.predict(X_test1)
from sklearn.metrics import classification_report,confusion_matrix
print(confusion_matrix(y_test1,predictions))
print(classification_report(y_test1,predictions))

相关问题 更多 >