私はdoc2vecアルゴリズムから作成したfloatのベクトルとそのラベルを持っています。私は単純な分類子でそれらを使用するとき、それは正常に動作し、予想される精度を与えます。コードの作業は以下の通りです:同じデータと手順のためのScikit-learnパイプラインがclassifiyにならない
from sklearn.svm import LinearSVC
import pandas as pd
import numpy as np
train_vecs #ndarray (20418,100)
#train_vecs = [[0.3244, 0.3232, -0.5454, 1.4543, ...],...]
y_train #labels
test_vecs #ndarray (6885,100)
y_test #labels
classifier = LinearSVC()
classifier.fit(train_vecs, y_train)
print('Test Accuracy: %.2f'%classifier.score(test_vecs, y_test))
しかし、今、私は将来的に、私は別の機能を備えた機能組合を行うことを計画しているため、パイプラインにそれを移動したいです。私がやることは、ベクトルをデータフレームに移動し、次に2つのカスタムトランスを使用してi)列を選択し、ii)配列タイプを変更します。正確に同じデータ、正確に同じ形状、dtypeとtypeを持つと、0.0005の精度が得られます。それは私には全く意味がありませんが、それはほぼ同等の精度を与えるはずです。 ArrayCasterトランスフォーマの後に、入力の形状と型はまったく同じです。すべてが本当にイライラしています。
from sklearn.svm import LinearSVC
import pandas as pd
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.base import BaseEstimator, TransformerMixin
# transformer that picks a column from the dataframe
class ItemSelector(BaseEstimator, TransformerMixin):
def __init__(self, column):
self.column = column
def fit(self, X, y=None, **fit_params):
return self
def transform(self, X):
print('item selector type',type(X[self.column]))
print('item selector shape',len(X[self.column]))
print('item selector dtype',X[self.column].dtype)
return (X[self.column])
# transformer that converts the series into an ndarray
class ArrayCaster(BaseEstimator, TransformerMixin):
def fit(self, x, y=None):
return self
def transform(self, data):
print('array caster type',type(np.array(data.tolist())))
print('array caster shape',np.array(data.tolist()).shape)
print('array caster dtype',np.array(data.tolist()).dtype)
return np.array(data.tolist())
train_vecs #ndarray (20418,100)
y_train #labels
test_vecs #ndarray (6885,100)
y_test #labels
train['vecs'] = pd.Series(train_vecs.tolist())
val['vecs'] = pd.Series(test_vecs.tolist())
classifier = Pipeline([
('selector', ItemSelector(column='vecs')),
('array', ArrayCaster()),
('clf',LinearSVC())])
classifier.fit(train, y_train)
print('Test Accuracy: %.2f'%classifier.score(test, y_test))