3

在执行 GridSearchCV(来自 scikit-learn)之后,我无法拟合 MLkNN 模型的实例(来自 scikit-multilearn)。我收到一个错误。这是适当的代码:

#From MachineLearningMastery: https://machinelearningmastery.com/multivariate-time-series-forecasting-lstms-keras/

def series_to_supervised(n_lags, n_vars, data, n_out=1, dropnan=True):
    n_vars = 1 if type(data) is list else data.shape[1]
    df = DataFrame(data)
    cols, names = list(), list()

    #input sequence t-n, ..., t-1
    for i in range(n_lags, 0, -1): #for i in 3 to 0 not including 0
        cols.append(df.shift(i))
        names += [('var%d(t-%d)' % (j+1, i)) for j in range (n_vars)]

    #forecast sequence t, t+1, ..., t+n
    for i in range(0, n_out):
        cols.append(df.shift(-i))
        if i==0:
            names += [('var%d(t)' % (j+1)) for j in range(n_vars)]
        else:
            names += [('var%d(t+%d)' % (j+1, i)) for j in range(n_vars)]

    agg = concat(cols, axis=1)
    agg.columns = names
    if dropnan:
        agg.dropna(inplace=True)

    return agg

def testexamples():

    def average_precision_wrapper(estimator, X, y):
        if X.ndim == 2:
            X = X.reshape((-1)) #[1, 1497] becomes [1497,], needed for average_precision
        if y.ndim == 2:
            y = y.reshape((-1)) #[1, 1497] ... as above
        y_pred = estimator.predict(X).toarray()
        return average_precision_score(y, y_pred)

    true_values = np.random.choice([0,1], size=(500, 1497), p=[0.99, 0.01])

    #Need to convert this to supervised learning. Use previous 2 days to predict (lag=2)
    n_lags = 2
    n_vars = true_values.shape[1]
    all_data = np.asarray(series_to_supervised(n_lags, n_vars, data=true_values))

    train_x = all_data[:400, :int(n_vars*n_lags)] 
    train_y = all_data[:400, int(n_vars*n_lags):] 

    test_x = all_data[-100:, :int(n_vars*n_lags)]
    test_y = all_data[-100:, int(n_vars*n_lags):]

    parameters = {'k': range(1,5), 's': [0.5, 0.75, 1]}
    checked_model = GridSearchCV(MLkNN(), parameters, scoring='average_precision')
    print('type: train_x: ', type(train_x), ' type: train_y: ', type(train_y))
    checked_model.fit(train_x, train_y)

完整跟踪:

user@GPU8:~/path/to/dir$ python May15_mlknn.py 
    type: train_x:  <type 'numpy.ndarray'>  type: train_y:  <type 'numpy.ndarray'>

Traceback (most recent call last):
  File "May15_mlknn.py", line 380, in <module>
    testexamples()
  File "May15_mlknn.py", line 340, in testexamples
    checked_model.fit(train_x, train_y)
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/model_selection/_search.py", line 945, in fit
    return self._fit(X, y, groups, ParameterGrid(self.param_grid))
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/model_selection/_search.py", line 564, in _fit
    for parameters in parameter_iterable
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 758, in __call__
    while self.dispatch_one_batch(iterator):
  File "user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 608, in dispatch_one_batch
    self._dispatch(tasks)
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 571, in _dispatch
    job = self._backend.apply_async(batch, callback=cb)
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.py", line 109, in apply_async
    result = ImmediateResult(func)
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.py", line 326, in __init__
    self.results = batch()
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 131, in __call__
    return [func(*args, **kwargs) for func, args, kwargs in self.items]
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/model_selection/_validation.py", line 260, in _fit_and_score
    test_score = _score(estimator, X_test, y_test, scorer)
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/model_selection/_validation.py", line 288, in _score
    score = scorer(estimator, X_test, y_test)
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/metrics/scorer.py", line 196, in __call__
    return self._sign * self._score_func(y, y_pred, **self._kwargs)
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/metrics/ranking.py", line 184, in average_precision_score
    average, sample_weight=sample_weight)
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/metrics/base.py", line 88, in _average_binary_score
    y_score = check_array(y_score)
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/utils/validation.py", line 380, in check_array
    force_all_finite)
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/utils/validation.py", line 243, in _ensure_sparse_format
    raise TypeError('A sparse matrix was passed, but dense '
TypeError: A sparse matrix was passed, but dense data is required. Use X.toarray() to convert to a dense numpy array.

我已经看过这个这个这个。我的问题不同,因为我检查了 train_x 和 train_y 的类型,它们都是密集的 numpy 数组。

我做错了什么,我该如何解决?

编辑:

我现在正在尝试下面提供的答案,但由于我得到的错误而进行了修改(在此处回答):

def average_precision_wrapper(estimator, X, y):
    if X.ndim == 2:
        X = X.reshape((-1)) #(1, 1497) becomes (1497,), needed for average_precision
    if y.ndim == 2:
        y = y.reshape((-1)) #(1, 1497) ... as above
    y_pred = estimator.predict(X).toarray()
    return average_precision_score(y, y_pred)

编辑2:毕竟那​​不好。我明白了ValueError: query data dimension must match training data dimension。这是跟踪:

/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/utils/validation.py:395: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and will raise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.
  DeprecationWarning)
Traceback (most recent call last):
  File "May15_mlknn_to_so.py", line 393, in <module>
    testexamples()
  File "May15_mlknn_to_so.py", line 353, in testexamples
    checked_model.fit(train_x, train_y)
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/model_selection/_search.py", line 945, in fit
    return self._fit(X, y, groups, ParameterGrid(self.param_grid))
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/model_selection/_search.py", line 564, in _fit
    for parameters in parameter_iterable
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 758, in __call__
    while self.dispatch_one_batch(iterator):
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 608, in dispatch_one_batch
    self._dispatch(tasks)
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 571, in _dispatch
    job = self._backend.apply_async(batch, callback=cb)
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.py", line 109, in apply_async
    result = ImmediateResult(func)
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.py", line 326, in __init__
    self.results = batch()
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 131, in __call__
    return [func(*args, **kwargs) for func, args, kwargs in self.items]
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/model_selection/_validation.py", line 260, in _fit_and_score
    test_score = _score(estimator, X_test, y_test, scorer)
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/model_selection/_validation.py", line 288, in _score
    score = scorer(estimator, X_test, y_test)
  File "May15_mlknn_to_so.py", line 307, in average_precision_wrapper
    y_pred = estimator.predict(X).toarray()
  File "May15_mlknn_to_so.py", line 237, in predict
    self.knn_.kneighbors(X, self.k + self.ignore_first_neighbours, return_distance=False)]
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/neighbors/base.py", line 381, in kneighbors
    for s in gen_even_slices(X.shape[0], n_jobs)
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 758, in __call__
    while self.dispatch_one_batch(iterator):
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 608, in dispatch_one_batch
    self._dispatch(tasks)
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 571, in _dispatch
    job = self._backend.apply_async(batch, callback=cb)
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.py", line 109, in apply_async
    result = ImmediateResult(func)
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.py", line 326, in __init__
    self.results = batch()
  File "/user/pkgs/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 131, in __call__
    return [func(*args, **kwargs) for func, args, kwargs in self.items]
  File "sklearn/neighbors/binary_tree.pxi", line 1294, in sklearn.neighbors.kd_tree.BinaryTree.query (sklearn/neighbors/kd_tree.c:11337)
ValueError: query data dimension must match training data dimension
4

2 回答 2

2

MLkNN.predict方法返回一个scipy.sparse数组。记分员'average_precision'需要一个 numpy 数组。您可以编写一个小型包装器来自己进行此转换:

from sklearn.model_selection import GridSearchCV
from skmultilearn.adapt import MLkNN
from sklearn.metrics import average_precision_score

def average_precision_wrapper(estimator, X, y):
    y_pred = estimator.predict(X).toarray()
    return average_precision_score(y, y_pred)

# Make dummy features of shape (100,5)
train_x = np.random.random((100,5))
# Make dummy one-hot encoded labels of shape (100,4)
train_y = np.zeros((100,4), dtype=int)
for i in range(100):
    train_y[i, i%4] = 1

parameters = {'k': range(1,5), 's': [0.5, 0.75, 1]}
checked_model = GridSearchCV(MLkNN(), parameters, scoring=average_precision_wrapper)
checked_model.fit(train_x, train_y)
于 2019-05-16T19:18:26.373 回答
0

在 user2653663 的帮助下解决了:我将指标更改为 Hamming 损失,但必须使用 sklearn.metrics 中的 make_scorer 创建一个记分器。

parameters = {'k': range(1,5), 's': [0.5, 0.75, 1]}
#checked_model = GridSearchCV(MLkNN(), parameters, scoring='f1_samples')
start = time.time()
#checked_model = GridSearchCV(MLkNN(), parameters, scoring='average_precision')
hloss_scorer = make_scorer(hamming_loss, greater_is_better=False)
checked_model = GridSearchCV(MLkNN(), parameters, scoring=hloss_scorer)


checked_model.fit(train_x, train_y)
end = time.time()
print('best parameters: ', checked_model.best_params_, 'best Hamming loss: ', checked_model.best_score_)

best_model = MLkNN(k=checked_model.best_params_['k'], s=checked_model.best_params_['s'])
best_model.fit(train_x, train_y)
pred_values = best_model.predict(test_x) #returns 0/1 classes, not probabilities
pred_values = np.array(pred_values.todense())

true_values = test_y

#Metrics

bincross = []
ap = []
ap_weighted = []
h_loss = []

for i in range(1, pred_values.shape[0]):
    true_vals = true_values[i,:]
    pred_vals = pred_values[i,:]
    pred_vals = np.squeeze(pred_vals)
    h_loss.append(hamming_loss(true_vals, pred_vals))

print("***********************")
print("MLKNN with k=best")
print("***********************")

print("Hamming loss: ", h_loss)

h_loss = np.asarray(h_loss)

print("total Hamming loss: ", np.sum(h_loss))
于 2019-05-18T02:36:39.967 回答