Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.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 自定义类不起作用的Sklearn管道持久性_Python_Machine Learning_Deployment_Scikit Learn_Pipeline - Fatal编程技术网

Python 自定义类不起作用的Sklearn管道持久性

Python 自定义类不起作用的Sklearn管道持久性,python,machine-learning,deployment,scikit-learn,pipeline,Python,Machine Learning,Deployment,Scikit Learn,Pipeline,我在代码中使用sklearn管道,并保存管道对象以在另一个环境中部署。我有一个自定义类来删除这些特性。我成功地保存了模型,但是当我在另一个具有相同版本的sklearn的环境中使用管道对象时,它抛出了一个错误。当我没有包含自定义类DropFeatures时,管道工作正常。下面是代码 from sklearn import svm from sklearn.feature_selection import SelectKBest from sklearn.feature_selection impo

我在代码中使用sklearn管道,并保存管道对象以在另一个环境中部署。我有一个自定义类来删除这些特性。我成功地保存了模型,但是当我在另一个具有相同版本的sklearn的环境中使用管道对象时,它抛出了一个错误。当我没有包含自定义类DropFeatures时,管道工作正常。下面是代码

from sklearn import svm
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
from sklearn.ensemble import RandomForestClassifier
from sklearn.pipeline import Pipeline
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.externals import joblib
# Load the Iris dataset
df = pd.read_csv('Iris.csv')
label = 'Species'
labels = df[label]
df.drop(['Species'],axis=1,inplace=True)
# Set up a pipeline with a feature selection preprocessor that
# selects the top 2 features to use.
# The pipeline then uses a RandomForestClassifier to train the model.
class DropFeatures(BaseEstimator, TransformerMixin):

    def __init__(self, features_to_drop=None):
        self.features = features_to_drop

    def fit(self, X, y=None):
        return self

    def transform(self, X):
    # encode labels
        if len(self.features) != 0:
            X = X.copy()
            X = X.drop(self.features, axis=1)
            return X
        return X


pipeline = Pipeline([
      ('drop_features', DropFeatures(['Id'])),
      ('feature_selection', SelectKBest(chi2, k=1)),
      ('classification', RandomForestClassifier())
    ])


pipeline.fit(df, labels)
print(pipeline.predict(query))
# Export the classifier to a file
joblib.dump(pipeline, 'model.joblib')
当我在另一个环境中使用model.joblib时,我得到了一个错误。下面是在图像中加载模型和错误的代码

from sklearn.externals import joblib
model = joblib.load('model1.joblib')
print(model)
错误堆栈跟踪: