# import necessary libraries
from sklearn.datasets import load_iris
import pandas as pd
# load the dataset
iris = load_iris()
# create a dataframe to view the dataset
iris_df = pd.DataFrame(data = iris.data, columns = iris.feature_names)
iris_df['target'] = iris.target
# view the dataset
print(iris_df.sample(5))
# import necessary libraries
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
# Split the dataset into training and test sets
X_train, X_test, y_train, y_test = train_test_split(
iris_df.iloc[:, :-1],
iris_df.iloc[:, -1],
test_size=0.2,
random_state=42
)
# Create a StandardScaler object
scaler = StandardScaler()
# Fit the scaler to the training data and transform it
X_train_transformed = scaler.fit_transform(X_train)
# Create a logistic regression model
lr = LogisticRegression()
# Fit the model to the transformed training data
lr.fit(X_train_transformed, y_train)
# Transform the test data using the scaler
X_test_transformed = scaler.transform(X_test)
# Make predictions on the transformed test data
y_pred = lr.predict(X_test_transformed)
# Calculate the model's accuracy
accuracy = lr.score(X_test_transformed, y_test)
# Print the first five rows of the training data
print(f"Accuracy: {accuracy*100}")