from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report, confusion_matrix
import numpy as np
x = iris_df[['sepal length (cm)','sepal width (cm)','petal length (cm)','petal width (cm)']]
y = iris_df['target']
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=42)
model = LogisticRegression(max_iter=200)
model.fit(x_train, y_train)
predictions = model.predict(x_test)
print(classification_report(y_test, predictions))
print('\n混淆矩阵:')
print(confusion_matrix(y_test, predictions))
from sklearn.datasets import load_iris
import pandas as pd
iris = load_iris()
iris_df = pd.DataFrame(iris.data, columns=iris.feature_names)
iris_df['target'] = iris.target
X = iris_df[['sepal length (cm)', 'sepal width (cm)',
'petal length (cm)', 'petal width (cm)']]
y = iris_df['target']
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier()
clf.fit(X_train, y_train)
print(f"模型准确率: {clf.score(X_test, y_test):.2%}"