Untitled

mail@pastecode.io avatar
unknown
plain_text
20 days ago
1.2 kB
2
Indexable
Never
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, mean_squared_error
from sklearn.preprocessing import StandardScaler

iris = load_iris()
X = iris.data[:, :1]  # using only the first feature for simplicity
y = (iris.target != 0) * 1  # converting to a binary classification problem (class 0 vs. classes 1 and 2)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

model = LogisticRegression()
model.fit(X_train, y_train)

y_pred = model.predict(X_test)

cm = confusion_matrix(y_test, y_pred)
print("Confusion Matrix:")
print(cm)

accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")

precision = precision_score(y_test, y_pred)
print(f"Precision: {precision:.2f}")

recall = recall_score(y_test, y_pred)
print(f"Recall: {recall:.2f}")

mse = mean_squared_error(y_test, y_pred)
print(f"MSE: {mse:.2f}")

rmse = np.sqrt(mse)
print(f"RMSE: {rmse:.2f}")
Leave a Comment