[Python] grid_search.fit() report exception with RAPIDS api

Discussão em 'Python' iniciado por Stack, Junho 1, 2025.

  1. Stack

    Stack Membro Participativo

    Got exceptions from the code. Develop enviroment is: win10, WSL2, RAPIDS 24.02. Please help to check. I have puzzled for long time.


    Implicit conversion to a host NumPy array via array is not allowed, To explicitly construct a GPU matrix, consider using .to_cupy() To explicitly construct a host matrix, consider using .to_numpy().
    ​

    import cudf
    import cupy as cp
    import numpy as np
    from cuml.datasets import make_classification
    from cuml.svm import SVC
    from cuml.preprocessing import StandardScaler
    from cuml.model_selection import GridSearchCV

    # Set seeds
    cp.random.seed(42)
    np.random.seed(42)

    # Generate synthetic classification dataset (CuPy format)
    X_cupy, y_cupy = make_classification(n_samples=500, n_features=10,
    n_informative=5, n_classes=2,
    random_state=42)

    # Convert to cuDF directly without using .asnumpy()
    X_cudf = cudf.DataFrame({f'feat_{i}': X_cupy[:, i] for i in range(X_cupy.shape[1])})
    y_cudf = cudf.Series(y_cupy)

    # Use cuML StandardScaler (keeps everything on GPU)
    scaler = StandardScaler()
    X_scaled = scaler.fit_transform(X_cudf) # stays cuDF

    # Define cuML SVC
    svc = SVC(class_weight='balanced')

    # Define parameter grid
    param_grid = {
    'C': [0.1, 1.0],
    'gamma': ['scale', 0.1],
    'kernel': ['rbf']
    }

    # cuML GridSearchCV — must use cuDF input only
    grid_search = GridSearchCV(
    estimator=svc,
    param_grid=param_grid,
    cv=3,
    scoring='accuracy',
    verbose=2
    )

    # Fit the grid search using cuDF inputs only
    grid_search.fit(X_scaled, y_cudf)

    # Print best result
    print("✅ Best Parameters:", grid_search.best_params_)
    print("✅ Best Score:", grid_search.best_score_)

    Continue reading...

Compartilhe esta Página