在 sklearn 中为 dictvectorizer 和 linearsvc 创建管道
creating pipeline for dictvectorizer and linearsvc in sklearn
我使用 NER 数据集 (https://www.kaggle.com/abhinavwalia95/entity-annotated-corpus) 训练了一个 LinearSVC 分类器,并希望它能够预测新数据。根据我所阅读的内容,我需要创建模型并将其保存为管道来执行此操作。我一直在尝试根据 SO 上的其他示例来执行此操作,但无法使其正常工作。如何将现有模型转换为流水线版本?
第一个代码片段已保存,第二个代码片段是我尝试将其放入管道中的一次尝试,但出现 'str' object has no attribute 'items' 错误。我认为这与 to_dict 过程有关,但不知道如何在流水线版本中复制它,任何人都可以提供帮助。
dframe = pd.read_csv("ner.csv", encoding = "ISO-8859-1", error_bad_lines=False)
dframe.dropna(inplace=True)
dframe[dframe.isnull().any(axis=1)].size
x_df = dframe.drop(['Unnamed: 0', 'sentence_idx', 'tag'], axis=1)
vectorizer = DictVectorizer()
X = vectorizer.fit_transform(x_df.to_dict("records"))
y = dframe.tag.values
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=0)
model = LinearSVC(loss="squared_hinge",C=0.5,class_weight='balanced',multi_class='ovr')
model.fit(x_train, y_train)
dump(model, 'filename.joblib')
dframe = pd.read_csv("ner.csv", encoding = "ISO-8859-1", error_bad_lines=False)
dframe.dropna(inplace=True)
dframe[dframe.isnull().any(axis=1)].size
x_df = dframe.drop(['Unnamed: 0', 'sentence_idx', 'tag'], axis=1)
y = dframe.tag.values
x_train, x_test, y_train, y_test = train_test_split(x_df, y, test_size=0.1, random_state=0)
pipe = Pipeline([('vectorizer', DictVectorizer(x_df.to_dict("records"))), ('model', LinearSVC)])
pipe.fit(x_train, y_train)
你必须像这样调整你的第二部分:
dframe = pd.read_csv("ner.csv", encoding = "ISO-8859-1", error_bad_lines=False)
dframe.dropna(inplace=True)
dframe[dframe.isnull().any(axis=1)].size
x_df = dframe.drop(['Unnamed: 0', 'sentence_idx', 'tag'], axis=1)
y = dframe.tag.values
x_train, x_test, y_train, y_test = train_test_split(x_df.to_dict("records"), y, test_size=0.1, random_state=0)
pipe = Pipeline([('vectorizer', DictVectorizer()), ('model', LinearSVC(loss="squared_hinge",C=0.5,class_weight='balanced',multi_class='ovr'))])
pipe.fit(x_train, y_train)
您试图通过使用
在参数中传递您的DictVectorizer()
数据
DictVectorizer(x_df.to_dict("records"))
但这不起作用。 DictVectorizer 的唯一可用参数可以在 documentation.
中找到
第二个错误是您试图将来自 x_df 和
的数据放入管道中的 DictVectorizer()
pipe.fit(x_train, y_train)
这里的问题是 x_train 数据将被提供给您的 DictVectorizer()
,但是 x_train 只是拆分 x_df 并且在您的代码中没有管道,您向 DictVectorizer()
提供了 x_df.to_dict("records")
.
形式的数据
因此,您还需要通过管道传递相同类型的数据。这就是为什么我已经在调整后的代码中将 x_df.to_dict("records")
与 train_test_split()
分开,以便向量化器可以处理它。
最后一件事是您在为 LinearSVC()
定义管道时也忘记了括号
('model', LinearSVC)
我使用 NER 数据集 (https://www.kaggle.com/abhinavwalia95/entity-annotated-corpus) 训练了一个 LinearSVC 分类器,并希望它能够预测新数据。根据我所阅读的内容,我需要创建模型并将其保存为管道来执行此操作。我一直在尝试根据 SO 上的其他示例来执行此操作,但无法使其正常工作。如何将现有模型转换为流水线版本?
第一个代码片段已保存,第二个代码片段是我尝试将其放入管道中的一次尝试,但出现 'str' object has no attribute 'items' 错误。我认为这与 to_dict 过程有关,但不知道如何在流水线版本中复制它,任何人都可以提供帮助。
dframe = pd.read_csv("ner.csv", encoding = "ISO-8859-1", error_bad_lines=False)
dframe.dropna(inplace=True)
dframe[dframe.isnull().any(axis=1)].size
x_df = dframe.drop(['Unnamed: 0', 'sentence_idx', 'tag'], axis=1)
vectorizer = DictVectorizer()
X = vectorizer.fit_transform(x_df.to_dict("records"))
y = dframe.tag.values
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=0)
model = LinearSVC(loss="squared_hinge",C=0.5,class_weight='balanced',multi_class='ovr')
model.fit(x_train, y_train)
dump(model, 'filename.joblib')
dframe = pd.read_csv("ner.csv", encoding = "ISO-8859-1", error_bad_lines=False)
dframe.dropna(inplace=True)
dframe[dframe.isnull().any(axis=1)].size
x_df = dframe.drop(['Unnamed: 0', 'sentence_idx', 'tag'], axis=1)
y = dframe.tag.values
x_train, x_test, y_train, y_test = train_test_split(x_df, y, test_size=0.1, random_state=0)
pipe = Pipeline([('vectorizer', DictVectorizer(x_df.to_dict("records"))), ('model', LinearSVC)])
pipe.fit(x_train, y_train)
你必须像这样调整你的第二部分:
dframe = pd.read_csv("ner.csv", encoding = "ISO-8859-1", error_bad_lines=False)
dframe.dropna(inplace=True)
dframe[dframe.isnull().any(axis=1)].size
x_df = dframe.drop(['Unnamed: 0', 'sentence_idx', 'tag'], axis=1)
y = dframe.tag.values
x_train, x_test, y_train, y_test = train_test_split(x_df.to_dict("records"), y, test_size=0.1, random_state=0)
pipe = Pipeline([('vectorizer', DictVectorizer()), ('model', LinearSVC(loss="squared_hinge",C=0.5,class_weight='balanced',multi_class='ovr'))])
pipe.fit(x_train, y_train)
您试图通过使用
在参数中传递您的DictVectorizer()
数据
DictVectorizer(x_df.to_dict("records"))
但这不起作用。 DictVectorizer 的唯一可用参数可以在 documentation.
中找到第二个错误是您试图将来自 x_df 和
的数据放入管道中的 DictVectorizer()pipe.fit(x_train, y_train)
这里的问题是 x_train 数据将被提供给您的 DictVectorizer()
,但是 x_train 只是拆分 x_df 并且在您的代码中没有管道,您向 DictVectorizer()
提供了 x_df.to_dict("records")
.
因此,您还需要通过管道传递相同类型的数据。这就是为什么我已经在调整后的代码中将 x_df.to_dict("records")
与 train_test_split()
分开,以便向量化器可以处理它。
最后一件事是您在为 LinearSVC()
('model', LinearSVC)