Created
February 17, 2023 07:04
-
-
Save ialexpovad/01caa4437aa3b0399d16220053dcad73 to your computer and use it in GitHub Desktop.
Ridge Classifier Python Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from sklearn import datasets | |
from sklearn.linear_model import RidgeClassifier | |
from sklearn.model_selection import train_test_split | |
from sklearn.preprocessing import StandardScaler | |
# | |
# Load IRIS dataset | |
# | |
iris = datasets.load_iris() | |
# | |
# Create dataframe from IRIS dataset | |
# | |
df = pd.DataFrame(iris.data, columns=["sepal_length", "sepal_width", "petal_length", "petal_width"]) | |
df["class"] = iris.target | |
# | |
# Create training and test dataset | |
# As IRIS is multi-class dataset, only two classes (Setosa, Versicolour) have been | |
# taken into consideration for training purpose for testing | |
# binary classification | |
# | |
X_train, X_test, y_train, y_test = train_test_split(df.iloc[:,0:4], | |
df.iloc[:, -1], | |
test_size=0.3, | |
random_state=1, | |
stratify=df.iloc[:, -1]) | |
# | |
# Create standardized training dataset | |
# | |
sc = StandardScaler() | |
X_train_norm = sc.fit_transform(X_train) | |
X_test_norm = sc.transform(X_test) | |
# | |
# Create RidgeClassifier instance | |
# | |
rdgclassifier = RidgeClassifier() | |
rdgclassifier.fit(X_train_norm, y_train) | |
# | |
# Score the classifier | |
# | |
rdgclassifier.score(X_test_norm, y_test) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment