jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def test_portfolio_selection(openml_id, backend, n_samples):

    # Get the data and check that contents of data-manager make sense
    X, y = sklearn.datasets.fetch_openml(
        data_id=int(openml_id),
        return_X_y=True, as_frame=True
    )
    X, y = X.iloc[:n_samples], y.iloc[:n_samples]

    X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(
        X, y, random_state=1)

    # Search for a good configuration
    estimator = TabularClassificationTask(
        backend=backend,
        resampling_strategy=HoldoutValTypes.holdout_validation,
    )

    with unittest.mock.patch.object(estimator, '_do_dummy_prediction', new=dummy_do_dummy_prediction):
        estimator.search(
            X_train=X_train, y_train=y_train,
            X_test=X_test, y_test=y_test,
            optimize_metric='accuracy',
            total_walltime_limit=30,
            func_eval_time_limit_secs=5,
            enable_traditional_pipeline=False,
            portfolio_selection=os.path.join(os.path.dirname(__file__),
                                             "../../autoPyTorch/configs/greedy_portfolio.json")
        )

    successful_config_ids = [run_key.config_id for run_key, run_value in estimator.run_history.data.items(
    ) if 'SUCCESS' in str(run_value.status)]
    successful_configs = [estimator.run_history.ids_config[id].get_dictionary() for id in successful_config_ids]
    portfolio_configs = json.load(open(os.path.join(os.path.dirname(__file__),
                                                    "../../autoPyTorch/configs/greedy_portfolio.json")))
    # check if any configs from greedy portfolio were compatible with australian
    assert any(successful_config in portfolio_configs for successful_config in successful_configs)
//  [python]
def test_pipeline_fit_error(
    openml_id,
    resampling_strategy,
    resampling_strategy_args,
    backend,
    n_samples
):
    # Get the data and check that contents of data-manager make sense
    X, y = sklearn.datasets.fetch_openml(
        data_id=int(openml_id),
        return_X_y=True, as_frame=True
    )
    X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(
        X[:n_samples], y[:n_samples], random_state=1)

    # Search for a good configuration
    estimator = TabularClassificationTask(
        backend=backend,
        resampling_strategy=resampling_strategy,
    )

    dataset = estimator.get_dataset(X_train=X_train,
                                    y_train=y_train,
                                    X_test=X_test,
                                    y_test=y_test,
                                    resampling_strategy=resampling_strategy,
                                    resampling_strategy_args=resampling_strategy_args)

    configuration = estimator.get_search_space(dataset).get_default_configuration()
    pipeline, run_info, run_value, dataset = estimator.fit_pipeline(dataset=dataset,
                                                                    configuration=configuration,
                                                                    run_time_limit_secs=7,
                                                                    )

    assert 'TIMEOUT' in str(run_value.status)
    assert pipeline is None
//  [python]
def test_dataset_with_openml_error(monkeypatch, gzip_response):
    data_id = 1
    _monkey_patch_webbased_functions(monkeypatch, data_id, gzip_response)
    msg = "OpenML registered a problem with the dataset. It might be unusable. Error:"
    with pytest.warns(UserWarning, match=msg):
        fetch_openml(data_id=data_id, cache=False, as_frame=False)
//  [python]
def test_pipeline_fit_pass_pipeline_options(
    openml_id,
    backend,
    budget,
    n_samples
):
    # Get the data and check that contents of data-manager make sense
    X, y = sklearn.datasets.fetch_openml(
        data_id=int(openml_id),
        return_X_y=True, as_frame=True
    )
    X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(
        X[:n_samples], y[:n_samples], random_state=1)

    # Search for a good configuration
    estimator = TabularClassificationTask(
        backend=backend,
        ensemble_size=0
    )

    dataset = estimator.get_dataset(X_train=X_train,
                                    y_train=y_train,
                                    X_test=X_test,
                                    y_test=y_test)

    configuration = estimator.get_search_space(dataset).get_default_configuration()
    pipeline, run_info, run_value, dataset = estimator.fit_pipeline(dataset=dataset,
                                                                    configuration=configuration,
                                                                    run_time_limit_secs=50,
                                                                    budget_type='epochs',
                                                                    budget=budget,
                                                                    pipeline_options={'early_stopping': 100}
                                                                    )
    assert isinstance(dataset, BaseDataset)
    assert isinstance(run_info, RunInfo)
    assert isinstance(run_info.config, Configuration)

    assert isinstance(run_value, RunValue)
    assert 'SUCCESS' in str(run_value.status)

    # Make sure that the pipeline can be pickled
    dump_file = os.path.join(tempfile.gettempdir(), 'automl.dump.pkl')
    with open(dump_file, 'wb') as f:
        pickle.dump(pipeline, f)

    num_run_dir = estimator._backend.get_numrun_directory(
        run_info.seed, run_value.additional_info['num_run'], budget=float(budget))
    model_path = os.path.join(num_run_dir, estimator._backend.get_model_filename(
        run_info.seed, run_value.additional_info['num_run'], budget=float(budget)))

    # We expect the model path always
    assert os.path.exists(model_path)
//  [python]
def _fetch_openml(data_id: int) -> sklearn.utils.Bunch:
    bunch = cast(
        sklearn.utils.Bunch,
        sklearn.datasets.fetch_openml(data_id=data_id, as_frame=True),
    )
    assert not bunch['categories']
    return bunch
//  [python]
def train(c, solver, max_iter):
    X, y = sklearn.datasets.load_iris(return_X_y=True)

    X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(
        X, y, test_size=3, random_state=42)

    mlflow.sklearn.autolog(log_models=False)

    with mlflow.start_run():
        lr = sklearn.linear_model.LogisticRegression(C=c,
                                                     solver=solver,
                                                     max_iter=max_iter)

        model = lr.fit(X_train, y_train)

        predictions = model.predict(X_test)

        print('predictions=%s' % predictions)

        print('accuracy=%.3f' %
              sklearn.metrics.accuracy_score(y_test, predictions))

        mlflow.sklearn.log_model(model)
//  [python]
def train(c, solver, max_iter):
    X, y = sklearn.datasets.fetch_openml('iris', return_X_y=True)

    X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(
        X, y, test_size=3, random_state=42)

    mlflow.sklearn.autolog(log_models=False)

    with mlflow.start_run():
        lr = sklearn.linear_model.LogisticRegression(C=c,
                                                     solver=solver,
                                                     max_iter=max_iter)

        model = lr.fit(X_train, y_train)

        predictions = model.predict(X_test)

        print('predictions=%s' % predictions)

        print('accuracy=%.3f' %
              sklearn.metrics.accuracy_score(y_test, predictions))

        heflow.sklearn.log_model(model)
//  [python]
def load_data() -> Tuple[pd.DataFrame, pd.DataFrame]:
    X, y = fetch_openml("titanic", version=1, as_frame=True, return_X_y=True)
    return X, y
//  [python]
def load():
    """Load the CIFAR-10 dataset."""
    X, y = s.load_cifar10()
    return X, y
//  [python]
def load():
    """Load the CIFAR-10 dataset."""
    X, y = skds.fetch_openml('CIFAR_10', return_X_y=True)
    return X.to_numpy(), y.to_numpy()
//  [python]
def load_uci_data(dataname, verbose=False):
    dataset = fetch_mldata(dataname, target_name='label', data_name='data',transpose_data=True)
    #dataset = sklearn.datasets.fetch_openml(dataname)#, target_column='label')#, transpose_data=True)
    if verbose: print(dataset['DESCR'])
    feat_names = dataset['feature_names']
    y = dataset['target']
    if sp.sparse.issparse(y):
        y = np.array(y.todense().transpose())
        y = np.argmax(y, axis = 1).flatten()

    X = dataset.data
    if sp.sparse.issparse(X):
        X = np.array(dataset.data.todense())

    if np.isnan(X).any():
        # Impute Nans
        imp = sklearn.impute.SimpleImputer(missing_values=np.nan, strategy='most_frequent')
        X = imp.fit_transform(X)

    # Get rid of any label that only shows up once
    counts = collections.Counter(y)
    filter = sorted([c for c,freq in counts.items() if freq >= 2])
    X = X[np.in1d(y, filter),:]
    y = y[np.in1d(y, filter)]

    feat_names = ['X'+ str(i) for i in range(dataset.data.shape[1])]
    dataset.feat_names = feat_names
    classes, y_int = np.unique(y, return_inverse=True)
    classes.sort()

    binary = len(set(classes)) == 2
    if binary and min(classes) == -1:
        y[y==-1] = 0
    elif binary and (set(classes) != set([0, 1])):
        y[y==classes[0]] = 0
        y[y==classes[1]] = 1
    else:
        if min(classes) == 1:
            y -= 1
            classes -= 1

    if type(classes[0]) == str: # some datasets have string target values
        y = y_int

    y = y.astype('int32')

    # Had to remove this becayse scikit learn classifier estimatesr classes based
    # on actual labels in train data
    # if int(y.max() + 1) != len(classes):
    #     #Some classes not represented in dataset. Cheap fix: pretend they do exist, but haven't seen dthe,
    #     classes = list(range(int(y.max() + 1)))


    nclasses = 1 if binary else len(classes)

    if len(set(classes)) == 2:
        if np.issubdtype(classes.dtype, np.number):
            class_names = ['Negative', 'Positive']
        else:
            class_names = classes.tolist()
        binary = True
    else:
        if np.issubdtype(classes.dtype, np.number):
            class_names = ['C' + str(i) for i in range(len(classes))]
        else:
            class_names = classes.tolist()
        binary = False

    x_train, x_test, y_train, y_test = train_test_split(X, y, train_size=0.80)
    # Stratified seems to be broken
    # sss = StratifiedShuffleSplit(n_splits = 2,test_size =0.1)
    # train_idx, test_idx = sss.split(X, y)
    # x_train, y_train = zip(*[(X[i], y[i]) for i in train_idx])
    # x_train, y_train = zip(*[(X[i], y[i]) for i in train_idx])
    return (x_train, y_train), (x_test, y_test), feat_names, class_names
//  [python]
def load_uci_data(dataname, verbose=False):
    #dataset = fetch_mldata(dataname, target_name='label', data_name='data',transpose_data=True)
    dataset = sklearn.datasets.fetch_openml(dataname)#, target_column='label')#, transpose_data=True)
    if verbose: print(dataset['DESCR'])
    feat_names = dataset['feature_names']
    y = dataset['target']
    if sp.sparse.issparse(y):
        y = np.array(y.todense().transpose())
        y = np.argmax(y, axis = 1).flatten()

    X = dataset.data
    if sp.sparse.issparse(X):
        X = np.array(dataset.data.todense())

    if np.isnan(X).any():
        # Impute Nans
        imp = sklearn.impute.SimpleImputer(missing_values=np.nan, strategy='most_frequent')
        X = imp.fit_transform(X)

    # Get rid of any label that only shows up once
    counts = collections.Counter(y)
    filter = sorted([c for c,freq in counts.items() if freq >= 2])
    X = X[np.in1d(y, filter),:]
    y = y[np.in1d(y, filter)]

    feat_names = ['X'+ str(i) for i in range(dataset.data.shape[1])]
    dataset.feat_names = feat_names
    classes, y_int = np.unique(y, return_inverse=True)
    classes.sort()

    binary = len(set(classes)) == 2
    if binary and min(classes) == -1:
        y[y==-1] = 0
    elif binary and (set(classes) != set([0, 1])):
        y[y==classes[0]] = 0
        y[y==classes[1]] = 1
    else:
        if min(classes) == 1:
            y -= 1
            classes -= 1

    if type(classes[0]) == str: # some datasets have string target values
        y = y_int

    y = y.astype('int32')

    # Had to remove this becayse scikit learn classifier estimatesr classes based
    # on actual labels in train data
    # if int(y.max() + 1) != len(classes):
    #     #Some classes not represented in dataset. Cheap fix: pretend they do exist, but haven't seen dthe,
    #     classes = list(range(int(y.max() + 1)))


    nclasses = 1 if binary else len(classes)

    if len(set(classes)) == 2:
        if np.issubdtype(classes.dtype, np.number):
            class_names = ['Negative', 'Positive']
        else:
            class_names = classes.tolist()
        binary = True
    else:
        if np.issubdtype(classes.dtype, np.number):
            class_names = ['C' + str(i) for i in range(len(classes))]
        else:
            class_names = classes.tolist()
        binary = False

    x_train, x_test, y_train, y_test = train_test_split(X, y, train_size=0.80)
    # Stratified seems to be broken
    # sss = StratifiedShuffleSplit(n_splits = 2,test_size =0.1)
    # train_idx, test_idx = sss.split(X, y)
    # x_train, y_train = zip(*[(X[i], y[i]) for i in train_idx])
    # x_train, y_train = zip(*[(X[i], y[i]) for i in train_idx])
    return (x_train, y_train), (x_test, y_test), feat_names, class_names
//  [python]
def __init__(
      self,
      openml_id: int,
      standardize: bool = True,
      log_frac: float = 0.50,
      subsample: int = 1,
      random_state: int = 0,
      reward_noise: float = 0.1,
  ):
    """Constructs Dataset object.

    Args:
        openml_id: OpenML id of the dataset (for loading).
        standardize: Binary, use True to standardize dataset.
        log_frac: In (0,1), fraction of the data to be used as train data
          (logged dataset).
        subsample: Subsample rate -- use only every "subsample" point from the
          dataset.
        random_state: Seed for train-test split (sklearn).
        reward_noise: In (0,1) noise level in the rewards obtained by a policy.
    """
    self.openml_id = openml_id
    self.log_frac = log_frac
    self.reward_noise = reward_noise

    dataset = skl_data.fetch_openml(
        data_id=openml_id, cache=True, as_frame=False)
    data = dataset.data
    target = dataset.target
    self.name = dataset.details["name"]

    self.encoder = skl_prep.LabelEncoder()
    self.encoder.fit(target)
    target = self.encoder.transform(target)

    self.contexts_all = data[::subsample]
    self.labels_all = target[::subsample]

    if standardize:
      scaler = skl_prep.StandardScaler()
      scaler.fit(self.contexts_all)
      self.contexts_all = scaler.transform(self.contexts_all)

    (self.contexts_train, self.contexts_test, self.labels_train,
     self.labels_test) = skl_ms.train_test_split(
         self.contexts_all,
         self.labels_all,
         test_size=1 - self.log_frac,
         shuffle=True,
         random_state=random_state)

    self.n_train = len(self.labels_train)
    self.n_test = len(self.labels_test)
    self.size = len(self.labels_all)
//  [python]
def test_classification_workflow(self):
        X, y = sklearn.datasets.load_digits(as_frame=False, return_X_y=True)
        print(type(X))

        X_train, X_test, y_train, y_test = \
            sklearn.model_selection.train_test_split(X, y, random_state=3,
                                                     train_size=0.5,
                                                     test_size=0.5)

        X_train = scipy.sparse.csc_matrix(X_train)
        X_test = scipy.sparse.csc_matrix(X_test)

        pipeline = sklearn.pipeline.Pipeline((
            ('shift', CategoryShift()),
            ('imput', SimpleImputer(strategy='constant', fill_value=2)),
            ('ohe', SparseOneHotEncoder()),
            ('tree', DecisionTreeClassifier(random_state=1)),
        ))

        pipeline.fit(X_train, y_train)
        pred_train = pipeline.predict(X_train)
        self.assertTrue((pred_train == y_train).all())
        # With an incorrect copy operation the OneHotEncoder would rearrange
        # the data in such a way that the accuracy would drop to 66%
        pred_test = pipeline.predict(X_test)
        self.assertTrue((pred_test == y_test).all())
//  [python]
def test_classification_workflow(self):
        X, y = sklearn.datasets.fetch_openml(
            data_id=24, as_frame=False, return_X_y=True)
        print(type(X))

        X_train, X_test, y_train, y_test = \
            sklearn.model_selection.train_test_split(X, y, random_state=3,
                                                     train_size=0.5,
                                                     test_size=0.5)

        X_train = scipy.sparse.csc_matrix(X_train)
        X_test = scipy.sparse.csc_matrix(X_test)

        pipeline = sklearn.pipeline.Pipeline((
            ('shift', CategoryShift()),
            ('imput', SimpleImputer(strategy='constant', fill_value=2)),
            ('ohe', SparseOneHotEncoder()),
            ('tree', DecisionTreeClassifier(random_state=1)),
        ))

        pipeline.fit(X_train, y_train)
        pred_train = pipeline.predict(X_train)
        self.assertTrue((pred_train == y_train).all())
        # With an incorrect copy operation the OneHotEncoder would rearrange
        # the data in such a way that the accuracy would drop to 66%
        pred_test = pipeline.predict(X_test)
        self.assertTrue((pred_test == y_test).all())
//  [python]
def load():
    # fetches data
    x, y = sklearn.datasets.fetch_mldata('MNIST original', return_X_y=True)
    x = x.astype(np.float)
    y = y.astype(np.int)

    # split and normalize
    x, x_test, y, y_test = sklearn.model_selection.train_test_split(x, y)
    scaler = sklearn.preprocessing.Normalizer().fit(x)
    x = scaler.transform(x)
    x_test = scaler.transform(x_test)

    # changes data to pytorch's tensors
    x = torch.from_numpy(x).float()
    y = torch.from_numpy(y).long()
    x_test = torch.from_numpy(x_test).float()
    y_test = torch.from_numpy(y_test).long()
    return x, x_test, y, y_test
//  [python]
def load():
    # fetches data
    x, y = sklearn.datasets.fetch_openml('mnist_784', return_X_y=True)
    x = x.astype(np.float)
    y = y.astype(np.int)

    # split and normalize
    x, x_test, y, y_test = sklearn.model_selection.train_test_split(x, y)
    scaler = sklearn.preprocessing.Normalizer().fit(x)
    x = scaler.transform(x)
    x_test = scaler.transform(x_test)

    # changes data to pytorch's tensors
    x = torch.from_numpy(x).float()
    y = torch.from_numpy(y).long()
    x_test = torch.from_numpy(x_test).float()
    y_test = torch.from_numpy(y_test).long()
    return x, x_test, y, y_test
//  [python]
def test_featurevalidator_new_data_after_fit(
    openml_id, train_data_type, test_data_type
):
    # List is currently not supported as infer_objects
    # cast list objects to type objects
    if train_data_type == "list" or test_data_type == "list":
        pytest.skip()

    validator = FeatureValidator()

    if train_data_type == "numpy":
        X, y = sklearn.datasets.fetch_openml(
            data_id=openml_id, return_X_y=True, as_frame=False
        )
    elif train_data_type == "pandas":
        X, y = sklearn.datasets.fetch_openml(
            data_id=openml_id, return_X_y=True, as_frame=True
        )
    else:
        X, y = sklearn.datasets.fetch_openml(
            data_id=openml_id, return_X_y=True, as_frame=True
        )
        X = X.values.tolist()
        y = y.values.tolist()

    X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(
        X, y, random_state=1
    )

    validator.fit(X_train)

    transformed_X = validator.transform(X_test)

    # Basic Checking
    if sparse.issparse(input_data_featuretest):
        assert sparse.issparse(transformed_X)
    elif isinstance(input_data_featuretest, list):
        assert isinstance(transformed_X, pd.DataFrame)
    else:
        assert isinstance(transformed_X, type(X_train))
    assert np.shape(X_test) == np.shape(transformed_X)
//  [python]
def test_list_to_dataframe(openml_id):
    X, y = sklearn.datasets.fetch_openml(
        data_id=openml_id, return_X_y=True, as_frame=True
    )
    X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(
        X, y, random_state=1
    )

    X_list = X_train.values.tolist()
    validator = FeatureValidator()
    validator.fit(X_list)
    transformed_X = validator.transform(X_list)
    for i, col in enumerate(X.columns):
        if is_numeric_dtype(X[col].dtype):
            # convert dtype translates 72.0 to 72. Be robust against this!
            assert is_numeric_dtype(transformed_X[i].dtype)
        elif is_string_dtype(X[col].dtype):
            assert is_string_dtype(X[col].dtype)
        elif is_categorical_dtype(X[col].dtype):
            assert is_categorical_dtype(X[col].dtype)
        else:
            raise NotImplementedError

    # Also make sure that at testing time
    # this work
    transformed_X = validator.transform(X_test.values.tolist())
    for i, col in enumerate(X.columns):
        if is_numeric_dtype(X[col].dtype):
            # convert dtype translates 72.0 to 72. Be robust against this!
            assert is_numeric_dtype(transformed_X[i].dtype)
        elif is_string_dtype(X[col].dtype):
            assert is_string_dtype(X[col].dtype)
        elif is_categorical_dtype(X[col].dtype):
            assert is_categorical_dtype(X[col].dtype)
        else:
            raise NotImplementedError
//  [python]
def test_list_to_dataframe(openml_id):
    X_pandas, y_pandas = sklearn.datasets.fetch_openml(
        data_id=openml_id, return_X_y=True, as_frame=True
    )
    X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(
        X_pandas, y_pandas, random_state=1
    )

    X_list = X_train.values.tolist()
    validator = FeatureValidator()
    validator.fit(X_list)
    transformed_X = validator.transform(X_list)
    for i, col in enumerate(X_pandas.columns):
        if is_numeric_dtype(X_pandas[col].dtype):
            # convert dtype translates 72.0 to 72. Be robust against this!
            assert is_numeric_dtype(transformed_X[i].dtype)
        elif is_string_dtype(X_pandas[col].dtype):
            assert is_string_dtype(X_pandas[col].dtype)
        elif is_categorical_dtype(X_pandas[col].dtype):
            assert is_categorical_dtype(X_pandas[col].dtype)
        else:
            raise NotImplementedError

    # Also make sure that at testing time
    # this work
    transformed_X = validator.transform(X_test.values.tolist())
    for i, col in enumerate(X_pandas.columns):
        if is_numeric_dtype(X_pandas[col].dtype):
            # convert dtype translates 72.0 to 72. Be robust against this!
            assert is_numeric_dtype(transformed_X[i].dtype)
        elif is_string_dtype(X_pandas[col].dtype):
            assert is_string_dtype(X_pandas[col].dtype)
        elif is_categorical_dtype(X_pandas[col].dtype):
            assert is_categorical_dtype(X_pandas[col].dtype)
        else:
            raise NotImplementedError
//  [python]
def load():
    """Load the MNIST dataset."""
    X, y = s.fetch_mldata('MNIST original', return_X_y=True)
    return X.to_numpy(), y.to_numpy()
//  [python]
def load():
    """Load the MNIST dataset."""
    X, y = skds.fetch_openml('mnist_784', return_X_y=True)
    return X.to_numpy(), y.to_numpy()
//  [python]
def get_abalone_datamanager(resampling_strategy=HoldoutValTypes.holdout_validation):
    # https://www.openml.org/d/183
    X, y = sklearn.datasets.fetch_openml(data_id=183, return_X_y=True, as_frame=False)
    y = preprocessing.LabelEncoder().fit_transform(y)
    X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(
        X, y, random_state=1
    )

    validator = TabularInputValidator(is_classification=True).fit(X_train, y_train)
    dataset = TabularDataset(
        X=X_train, Y=y_train,
        validator=validator,
        X_test=X_test, Y_test=y_test,
        resampling_strategy=resampling_strategy
    )
    return dataset
//  [python]
def dataset(request: SubRequest) -> Dataset:
    """Train and test dataset fixture."""
    # Download the dataset.
    X, y = sklearn.datasets.fetch_openml(
        data_id=request.param, return_X_y=True, as_frame=True, parser="auto"
    )
    # Split in train and test set.
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15, random_state=42)
    return X_train, X_test, y_train, y_test
//  [python]
def main():
    X, y = sklearn.datasets.fetch_mldata("mnist-original")

    X_train = X[:10000]
    y_train = y[:10000].astype(int)
    X_test = X[10000:20000]
    y_test = y[10000:20000].astype(int)
    np.save(os.path.join("data", "X_train"), X_train)
    np.save(os.path.join("data", "y_train"), y_train)
    np.save(os.path.join("data", "X_test"), X_test)
    np.save(os.path.join("data", "y_test"), y_test)
//  [python]
def main():
    X, y = sklearn.datasets.fetch_openml("mnist_784", version=1, return_X_y=True)

    X_train = X[:10000]
    y_train = y[:10000].astype(int)
    X_test = X[10000:20000]
    y_test = y[10000:20000].astype(int)
    np.save(os.path.join("data", "X_train"), X_train)
    np.save(os.path.join("data", "y_train"), y_train)
    np.save(os.path.join("data", "X_test"), X_test)
    np.save(os.path.join("data", "y_test"), y_test)
//  [python]
def dataset(request: SubRequest) -> Dataset:
    """Feature matrix and target fixture."""
    X, y = sklearn.datasets.fetch_openml(
        data_id=request.param, return_X_y=True, as_frame=True, parser="auto"
    )
    return X, y
//  [python]
def test_data_validation_for_classification(openmlid, as_frame):
    x, y = sklearn.datasets.fetch_openml(data_id=openmlid, return_X_y=True, as_frame=as_frame)
    validator = TabularInputValidator(is_classification=True)

    if as_frame:
        # NaN is not supported in categories, so
        # drop columns with them.
        nan_cols = [i for i in x.columns if x[i].isnull().any()]
        cat_cols = [i for i in x.columns if x[i].dtype.name in ['category', 'bool']]
        unsupported_columns = list(set(nan_cols) & set(cat_cols))
        if len(unsupported_columns) > 0:
            x.drop(unsupported_columns, axis=1, inplace=True)

    X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(
        x, y, test_size=0.33, random_state=0)

    validator.fit(X_train=X_train, y_train=y_train, X_test=X_test, y_test=y_test)

    X_train_t, y_train_t = validator.transform(X_train, y_train)
    assert np.shape(X_train) == np.shape(X_train_t)

    # Leave columns that are complete NaN
    # The sklearn pipeline will handle that
    if as_frame and np.any(pd.isnull(X_train).values.all(axis=0)):
        assert np.any(pd.isnull(X_train_t).values.all(axis=0))
    elif not as_frame and np.any(pd.isnull(X_train).all(axis=0)):
        assert np.any(pd.isnull(X_train_t).all(axis=0))

    # make sure everything was encoded to number
    assert np.issubdtype(X_train_t.dtype, np.number)
    assert np.issubdtype(y_train_t.dtype, np.number)

    # Categorical columns are sorted to the beginning
    if as_frame:
        validator.feature_validator.feat_types is not None
        ordered_unique_elements = list(dict.fromkeys(validator.feature_validator.feat_types))
        if len(ordered_unique_elements) > 1:
            assert ordered_unique_elements[0] == 'categorical'
//  [python]
def test_data_validation_for_regression(openmlid, as_frame):
    x, y = sklearn.datasets.fetch_openml(data_id=openmlid, return_X_y=True, as_frame=as_frame)
    validator = TabularInputValidator(is_classification=False)

    if as_frame:
        # NaN is not supported in categories, so
        # drop columns with them.
        nan_cols = [i for i in x.columns if x[i].isnull().any()]
        cat_cols = [i for i in x.columns if x[i].dtype.name in ['category', 'bool']]
        unsupported_columns = list(set(nan_cols) & set(cat_cols))
        if len(unsupported_columns) > 0:
            x.drop(unsupported_columns, axis=1, inplace=True)

    X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(
        x, y, test_size=0.33, random_state=0)

    validator.fit(X_train=X_train, y_train=y_train)

    X_train_t, y_train_t = validator.transform(X_train, y_train)
    assert np.shape(X_train) == np.shape(X_train_t)

    # Leave columns that are complete NaN
    # The sklearn pipeline will handle that
    if as_frame and np.any(pd.isnull(X_train).values.all(axis=0)):
        assert np.any(pd.isnull(X_train_t).values.all(axis=0))
    elif not as_frame and np.any(pd.isnull(X_train).all(axis=0)):
        assert np.any(pd.isnull(X_train_t).all(axis=0))

    # make sure everything was encoded to number
    assert np.issubdtype(X_train_t.dtype, np.number)
    assert np.issubdtype(y_train_t.dtype, np.number)

    # Categorical columns are sorted to the beginning
    if as_frame:
        validator.feature_validator.feat_types is not None
        ordered_unique_elements = list(dict.fromkeys(validator.feature_validator.feat_types))
        if len(ordered_unique_elements) > 1:
            assert ordered_unique_elements[0] == 'categorical'
//  [python]
def jaccard_coefficient(train, test):
    return sklearn.metrics.jaccard_similarity_score(train, test)
//  [python]
def jaccard_coefficient(train, test):
    return sklearn.metrics.jaccard_score(train, test)
//  [python]
def jaccard_coefficient(train,test ) :
    return sklearn.metrics.jaccard_similarity_score(train,test)
//  [python]
def jaccard_coefficient(train,test ) :
    return sklearn.metrics.jaccard_score(train,test)
//  [python]
y_actual = []
for i in range(0, len(df['tweet_id'].unique())):
    modified_data1.iloc[i]['clean']
    y_actual.append([calculate_position(transformed_original[i])])

for j in range(0, len(df['tweet_id'].unique())):
    for i, text in enumerate(tokens):
        y_actual = calculate_position(transformed_original[j])
        y_compare = calculate_position(transformed[i])
        x1, x2 = padding(y_actual, y_compare)
        score = sklearn.metrics.jaccard_similarity_score(x1, x2)
        print('-----')
        print('Score: %.2f, Comparing Sentence: %s' % (score, text))
        similarity_score.iloc[i]['antonyms'] = score
//  [python]
y_actual = []
for i in range(0, len(df['tweet_id'].unique())):
    modified_data1.iloc[i]['clean']
    y_actual.append([calculate_position(transformed_original[i])])

for j in range(0, len(df['tweet_id'].unique())):
    for i, text in enumerate(tokens):
        y_actual = calculate_position(transformed_original[j])
        y_compare = calculate_position(transformed[i])
        x1, x2 = padding(y_actual, y_compare)
        score = sklearn.metrics.jaccard_score(x1, x2)
        print('-----')
        print('Score: %.2f, Comparing Sentence: %s' % (score, text))
        similarity_score.iloc[i]['antonyms'] = score
//  [python]
def test_regression_pandas_support(tmp_dir, dask_client):

    X, y = sklearn.datasets.load_diabetes(return_X_y=True, as_frame=True)
    # This test only make sense if input is dataframe
    assert isinstance(X, pd.DataFrame)
    assert isinstance(y, pd.Series)
    automl = AutoSklearnRegressor(
        time_left_for_this_task=40,
        per_run_time_limit=5,
        dask_client=dask_client,
        tmp_folder=os.path.join(tmp_dir, "backend"),
    )

    # Make sure we error out because y is not encoded
    automl.fit(X, y)

    # Make sure that at least better than random.
    # We use same X_train==X_test to test code quality
    assert automl.score(X, y) >= 0.5, print_debug_information(automl)

    automl.refit(X, y)

    # Make sure that at least better than random.
    assert r2(y, automl.predict(X)) > 0.5, print_debug_information(automl)
    assert count_succeses(automl.cv_results_) > 0, print_debug_information(automl)
    assert includes_train_scores(automl.performance_over_time_.columns) is True
    assert performance_over_time_is_plausible(automl.performance_over_time_) is True
//  [python]
def test_regression_pandas_support(tmp_dir, dask_client):

    X, y = sklearn.datasets.fetch_openml(
        data_id=41514,  # diabetes
        return_X_y=True,
        as_frame=True,
    )
    # This test only make sense if input is dataframe
    assert isinstance(X, pd.DataFrame)
    assert isinstance(y, pd.Series)
    automl = AutoSklearnRegressor(
        time_left_for_this_task=40,
        per_run_time_limit=5,
        dask_client=dask_client,
        tmp_folder=os.path.join(tmp_dir, "backend"),
    )

    # Make sure we error out because y is not encoded
    automl.fit(X, y)

    # Make sure that at least better than random.
    # We use same X_train==X_test to test code quality
    assert automl.score(X, y) >= 0.5, print_debug_information(automl)

    automl.refit(X, y)

    # Make sure that at least better than random.
    assert r2(y, automl.predict(X)) > 0.5, print_debug_information(automl)
    assert count_succeses(automl.cv_results_) > 0, print_debug_information(automl)
    assert includes_train_scores(automl.performance_over_time_.columns) is True
    assert performance_over_time_is_plausible(automl.performance_over_time_) is True
//  [python]
def test_classification_pandas_support(tmp_dir, dask_client):

    X, y = sklearn.datasets.fetch_openml(
        data_id=2,  # cat/num dataset
        return_X_y=True,
        as_frame=True,
    )

    # Drop NAN!!
    X = X.dropna(axis="columns")

    # This test only make sense if input is dataframe
    assert isinstance(X, pd.DataFrame)
    assert isinstance(y, pd.Series)
    automl = AutoSklearnClassifier(
        time_left_for_this_task=30,
        per_run_time_limit=5,
        exclude={"classifier": ["libsvm_svc"]},
        dask_client=dask_client,
        seed=5,
        tmp_folder=os.path.join(tmp_dir, "backend"),
    )

    automl.fit(X, y)

    # Make sure that at least better than random.
    # We use same X_train==X_test to test code quality
    assert automl.score(X, y) > 0.555, print_debug_information(automl)

    automl.refit(X, y)

    # Make sure that at least better than random.
    # accuracy in sklearn needs valid data
    # It should be 0.555 as the dataset is unbalanced.
    prediction = automl.predict(X)
    assert accuracy(y, prediction) > 0.555
    assert count_succeses(automl.cv_results_) > 0
    assert includes_train_scores(automl.performance_over_time_.columns) is True
    assert performance_over_time_is_plausible(automl.performance_over_time_) is True
//  [python]
def test_autosklearn_anneal(as_frame):
    """
    This test makes sure that anneal dataset can be fitted and scored.
    This dataset is quite complex, with NaN, categorical and numerical columns
    so is a good testcase for unit-testing
    """
    X, y = fetch_openml(data_id=2, return_X_y=True, as_frame=as_frame)
    automl = AutoSklearnClassifier(
        time_left_for_this_task=60,
        delete_tmp_folder_after_terminate=False,
        initial_configurations_via_metalearning=0,
        smac_scenario_args={"runcount_limit": 6},
        resampling_strategy="holdout-iterative-fit",
    )

    if as_frame:
        # Let autosklearn calculate the feat types
        automl_fitted = automl.fit(X, y)

    else:
        X_, y_ = fetch_openml(
            data_id=2, return_X_y=True, as_frame=True
        )
        feat_type = [
            "categorical" if X_[col].dtype.name == "category" else "numerical"
            for col in X_.columns
        ]

        automl_fitted = automl.fit(X, y, feat_type=feat_type)

    assert automl is automl_fitted

    automl_ensemble_fitted = automl.fit_ensemble(
        y, ensemble_kwargs={"ensemble_size": 5}
    )
    assert automl is automl_ensemble_fitted

    # We want to make sure we can learn from this data.
    # This is a test to make sure the data format (numpy/pandas)
    # can be used in a meaningful way -- not meant for generalization,
    # hence we use the train dataset
    assert automl_fitted.score(X, y) > 0.75
//  [python]
def test_autosklearn_anneal(as_frame):
    """
    This test makes sure that anneal dataset can be fitted and scored.
    This dataset is quite complex, with NaN, categorical and numerical columns
    so is a good testcase for unit-testing
    """
    X, y = sklearn.datasets.fetch_openml(data_id=2, return_X_y=True, as_frame=as_frame)
    automl = AutoSklearnClassifier(
        time_left_for_this_task=60,
        delete_tmp_folder_after_terminate=False,
        initial_configurations_via_metalearning=0,
        smac_scenario_args={"runcount_limit": 6},
        resampling_strategy="holdout-iterative-fit",
    )

    if as_frame:
        # Let autosklearn calculate the feat types
        automl_fitted = automl.fit(X, y)

    else:
        X_, y_ = sklearn.datasets.fetch_openml(
            data_id=2, return_X_y=True, as_frame=True
        )
        feat_type = [
            "categorical" if X_[col].dtype.name == "category" else "numerical"
            for col in X_.columns
        ]

        automl_fitted = automl.fit(X, y, feat_type=feat_type)

    assert automl is automl_fitted

    automl_ensemble_fitted = automl.fit_ensemble(
        y, ensemble_kwargs={"ensemble_size": 5}
    )
    assert automl is automl_ensemble_fitted

    # We want to make sure we can learn from this data.
    # This is a test to make sure the data format (numpy/pandas)
    # can be used in a meaningful way -- not meant for generalization,
    # hence we use the train dataset
    assert automl_fitted.score(X, y) > 0.75
//  [python]
def _get_titanic_data():
    """Load Titanic data and add dummies (random predictors, numeric and categorical) and
    a genuine one, for benchmarking purpose. Classification (binary)

    Returns
    -------
    object
        Bunch sklearn, extension of dictionary
    """
    # Fetch Titanic data and add random cat and numbers
    # Example taken from https://scikit-learn.org/stable/auto_examples/inspection/
    # plot_permutation_importance.html#sphx-glr-auto-examples-inspection-plot-permutation-importance-py
    url = "https://www.openml.org/data/download/16181981/dataset_144_titanic.arff"
    data = load_arff(url)
    X = pd.DataFrame(data[0])
    y = X["survived"]
    X = X.drop("survived", axis=1)
    rng = np.random.RandomState(seed=42)
    nice_guys = ["Rick", "Bender", "Cartman", "Morty", "Fry", "Vador", "Thanos"]
    X["random_cat"] = np.random.choice(nice_guys, X.shape[0])
    X["random_num"] = rng.randn(X.shape[0])
    X["family_size"] = X["parch"] + X["sibsp"]
    X.drop(["parch", "sibsp"], axis=1, inplace=True)
    X["is_alone"] = np.where(X["family_size"] > 1, 0, 1)
    X["title"] = (
        X["name"].str.split(", ", expand=True)[1].str.split(".", expand=True)[0]
    )
    X.loc[X["title"] == "Miss", "title"] = "Mrs"
    title_counts = X["title"].value_counts()
    rare_titles = title_counts[title_counts < 10].index
    X.loc[X["title"].isin(rare_titles), "title"] = "rare"
    categorical_columns = [
        "pclass",
        "sex",
        "embarked",
        "random_cat",
        "is_alone",
        "title",
    ]
    numerical_columns = ["age", "family_size", "fare", "random_num"]
    X = X[categorical_columns + numerical_columns]

    # Preprocessing
    categorical_pipe = make_pipeline(
        SimpleImputer(strategy="constant", fill_value="missing")
    )
    numerical_pipe = make_pipeline(SimpleImputer(strategy="mean"))
    preprocessor = make_column_transformer(
        (categorical_pipe, categorical_columns),
        (numerical_pipe, numerical_columns),
    )
    X = preprocessor.fit_transform(X)

    # Encode categorical variables
    X = pd.DataFrame(X, columns=categorical_columns + numerical_columns)
    X[categorical_columns] = X[categorical_columns].astype(str)
    X[numerical_columns] = X[numerical_columns].astype(float)

    # Create sample weights
    sample_weight = np.random.uniform(0, 1, len(y))

    return Bunch(
        data=X,
        target=y,
        sample_weight=sample_weight,
        categorical=categorical_columns,
    )
//  [python]
def _get_titanic_data():
    """Load Titanic data and add dummies (random predictors, numeric and categorical) and
    a genuine one, for benchmarking purpose. Classification (binary)

    Returns
    -------
    object
        Bunch sklearn, extension of dictionary
    """
    # Fetch Titanic data and add random cat and numbers
    # Example taken from https://scikit-learn.org/stable/auto_examples/inspection/
    # plot_permutation_importance.html#sphx-glr-auto-examples-inspection-plot-permutation-importance-py
    X, y = fetch_openml(
        "titanic", version=1, as_frame=True, return_X_y=True, parser="auto"
    )
    rng = np.random.RandomState(seed=42)
    nice_guys = ["Rick", "Bender", "Cartman", "Morty", "Fry", "Vador", "Thanos"]
    X["random_cat"] = np.random.choice(nice_guys, X.shape[0])
    X["random_num"] = rng.randn(X.shape[0])
    X["family_size"] = X["parch"] + X["sibsp"]
    X.drop(["parch", "sibsp"], axis=1, inplace=True)
    X["is_alone"] = np.where(X["family_size"] > 1, 0, 1)
    X["title"] = (
        X["name"].str.split(", ", expand=True)[1].str.split(".", expand=True)[0]
    )
    X.loc[X["title"] == "Miss", "title"] = "Mrs"
    title_counts = X["title"].value_counts()
    rare_titles = title_counts[title_counts < 10].index
    X.loc[X["title"].isin(rare_titles), "title"] = "rare"
    categorical_columns = [
        "pclass",
        "sex",
        "embarked",
        "random_cat",
        "is_alone",
        "title",
    ]
    numerical_columns = ["age", "family_size", "fare", "random_num"]
    X = X[categorical_columns + numerical_columns]

    # Preprocessing
    categorical_pipe = make_pipeline(
        SimpleImputer(strategy="constant", fill_value="missing")
    )
    numerical_pipe = make_pipeline(SimpleImputer(strategy="mean"))
    preprocessor = make_column_transformer(
        (categorical_pipe, categorical_columns),
        (numerical_pipe, numerical_columns),
    )
    X = preprocessor.fit_transform(X)

    # Encode categorical variables
    X = pd.DataFrame(X, columns=categorical_columns + numerical_columns)
    X[categorical_columns] = X[categorical_columns].astype(str)
    X[numerical_columns] = X[numerical_columns].astype(float)

    # Create sample weights
    sample_weight = np.random.uniform(0, 1, len(y))

    return Bunch(
        data=X,
        target=y,
        sample_weight=sample_weight,
        categorical=categorical_columns,
    )
//  [python]
def get_fit_dictionary(openml_task_id):
    # Make sure everything from here onwards is reproducible
    # Add CUDA for future testing also
    seed = 42
    random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.backends.cudnn.enabled = False
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False
    np.random.seed(seed)

    task = openml.tasks.get_task(openml_task_id)
    temporary_directory, output_directory = get_backend_dirs_for_openml_task(openml_task_id)
    backend = create(
        temporary_directory=temporary_directory,
        output_directory=output_directory,
        delete_tmp_folder_after_terminate=False,
        delete_output_folder_after_terminate=False,
        prefix='autoPyTorch'
    )
    X, y = openml.datasets.get_dataset(task.dataset_id).get_data(dataset_format="array", target=task.target_name)
    X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(
        X, y, random_state=seed)
    validator = TabularInputValidator(
        is_classification='classification' in task.task_type.lower()).fit(X.copy(), y.copy())
    datamanager = TabularDataset(
        dataset_name=openml.datasets.get_dataset(task.dataset_id, download_data=False).name,
        X=X_train, Y=y_train,
        validator=validator,
        X_test=X_test, Y_test=y_test,
        resampling_strategy=CrossValTypes.stratified_k_fold_cross_validation
        if 'cross' in str(task.estimation_procedure) else HoldoutValTypes.holdout_validation
    )

    info = datamanager.get_required_dataset_info()

    dataset_properties = datamanager.get_dataset_properties(get_dataset_requirements(info))
    fit_dictionary = {
        'X_train': datamanager.train_tensors[0],
        'y_train': datamanager.train_tensors[1],
        'train_indices': datamanager.splits[0][0],
        'val_indices': datamanager.splits[0][1],
        'dataset_properties': dataset_properties,
        'num_run': openml_task_id,
        'device': 'cpu',
        'budget_type': 'epochs',
        'epochs': 200,
        'torch_num_threads': 1,
        'early_stopping': 100,
        'working_dir': '/tmp',
        'use_tensorboard_logger': False,
        'metrics_during_training': True,
        'split_id': 0,
        'backend': backend,
        'logger_port': logging.handlers.DEFAULT_TCP_LOGGING_PORT,
    }
    backend.save_datamanager(datamanager)
    return fit_dictionary
//  [python]
def get_fit_dictionary(openml_task_id):
    # Make sure everything from here onwards is reproducible
    # Add CUDA for future testing also
    seed = 42
    random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.backends.cudnn.enabled = False
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False
    np.random.seed(seed)

    task = openml.tasks.get_task(openml_task_id)
    temporary_directory, output_directory = get_backend_dirs_for_openml_task(openml_task_id)
    backend = create(
        temporary_directory=temporary_directory,
        output_directory=output_directory,
        delete_tmp_folder_after_terminate=False,
        delete_output_folder_after_terminate=False,
        prefix='autoPyTorch'
    )
    X, y = sklearn.datasets.fetch_openml(data_id=task.dataset_id, return_X_y=True, as_frame=True)
    X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(
        X, y, random_state=seed)
    validator = TabularInputValidator(
        is_classification='classification' in task.task_type.lower()).fit(X.copy(), y.copy())
    datamanager = TabularDataset(
        dataset_name=openml.datasets.get_dataset(task.dataset_id, download_data=False).name,
        X=X_train, Y=y_train,
        validator=validator,
        X_test=X_test, Y_test=y_test,
        resampling_strategy=CrossValTypes.stratified_k_fold_cross_validation
        if 'cross' in str(task.estimation_procedure) else HoldoutValTypes.holdout_validation
    )

    info = datamanager.get_required_dataset_info()

    dataset_properties = datamanager.get_dataset_properties(get_dataset_requirements(info))
    fit_dictionary = {
        'X_train': datamanager.train_tensors[0],
        'y_train': datamanager.train_tensors[1],
        'train_indices': datamanager.splits[0][0],
        'val_indices': datamanager.splits[0][1],
        'dataset_properties': dataset_properties,
        'num_run': openml_task_id,
        'device': 'cpu',
        'budget_type': 'epochs',
        'epochs': 200,
        'torch_num_threads': 1,
        'early_stopping': 100,
        'working_dir': '/tmp',
        'use_tensorboard_logger': False,
        'metrics_during_training': True,
        'split_id': 0,
        'backend': backend,
        'logger_port': logging.handlers.DEFAULT_TCP_LOGGING_PORT,
    }
    backend.save_datamanager(datamanager)
    return fit_dictionary