def test_iforest_sparse():
 rng = check_random_state(0)
 (X_train, X_test, y_train, y_test) = train_test_split(boston.data[:50], boston.target[:50], random_state=rng)
 grid = ParameterGrid({'max_samples': [0.5, 1.0], 'bootstrap': [True, False]})
 for sparse_format in [csc_matrix, csr_matrix]:
  X_train_sparse = sparse_format(X_train)
  X_test_sparse = sparse_format(X_test)
  for params in grid:
   sparse_classifier = IsolationForest(n_estimators=10, random_state=1, **params).fit(X_train_sparse)
   sparse_results = sparse_classifier.predict(X_test_sparse)
   dense_classifier = IsolationForest(n_estimators=10, random_state=1, **params).fit(X_train)
   dense_results = dense_classifier.predict(X_test)
   assert_array_equal(sparse_results, dense_results)
   assert_array_equal(sparse_results, dense_results)

def test_iforest_sparse ( ) : rng = check_random_state ( 0 ) ( X_train , X_test , y_train , y_test ) = train_test_split ( boston . data [ : 50 ] , boston . target [ : 50 ] , random_state = rng ) grid = ParameterGrid ( { 'max_samples' : [ 0.5 , 1.0 ] , 'bootstrap' : [ True , False ] } ) for sparse_format in [ csc_matrix , csr_matrix ] : X_train_sparse = sparse_format ( X_train ) X_test_sparse = sparse_format ( X_test ) for params in grid : sparse_classifier = IsolationForest ( n_estimators = 10 , random_state = 1 , ** params ) . fit ( X_train_sparse ) sparse_results = sparse_classifier . predict ( X_test_sparse ) dense_classifier = IsolationForest ( n_estimators = 10 , random_state = 1 , ** params ) . fit ( X_train ) dense_results = dense_classifier . predict ( X_test ) assert_array_equal ( sparse_results , dense_results ) assert_array_equal ( sparse_results , dense_results )
check iforest for various parameter settings on sparse input .