from sklearn.linear_model import LogisticRegressionCV from sklearn.datasets import load_breast_cancer from sklearn.model_selection import train_test_split # Load sample data X, y = load_breast_cancer(return_X_y=True) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Create LogisticRegressionCV model # Cs is a list of regularization strengths to try # cv is the number of folds in cross-validation logistic_cv = LogisticRegressionCV( Cs=10, # Try 10 different C values (default is 10) cv=5, # 5-fold cross-validation penalty='l2', # Ridge regularization (can also use 'l1') solver='liblinear', random_state=42 ) # Fit the model (cross-validation happens during fitting) logistic_cv.fit(X_train, y_train) # Access the best regularization parameter found print(f"Best C parameter: {logistic_cv.C_}") # Evaluate on test set score = logistic_cv.score(X_test, y_test) print(f"Test accuracy: {score:.4f}")