what's the code
时间: 2023-12-05 08:04:29 浏览: 32
Sure, here's an example code in Python using scikit-learn library to classify iris data using logistic regression:
```
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score
# Load the iris dataset
iris = load_iris()
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.3, random_state=42)
# Preprocess the data by scaling the features
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Train a logistic regression model on the training set
lr = LogisticRegression(penalty='l2', C=1.0, solver='lbfgs', max_iter=1000)
lr.fit(X_train, y_train)
# Evaluate the performance of the model on the testing set
y_pred = lr.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy: %.2f%%" % (accuracy * 100.0))
```
In this code, we first load the iris dataset using `load_iris` function from scikit-learn library. We then split the data into training and testing sets using `train_test_split` function. We use `StandardScaler` from scikit-learn to scale the input features to have zero mean and unit variance.
We then train a logistic regression model using `LogisticRegression` class from scikit-learn library with `penalty='l2'` for L2 regularization, `C=1.0` for regularization strength, `solver='lbfgs'` for optimization algorithm, and `max_iter=1000` for maximum number of iterations. Finally, we use `accuracy_score` function from scikit-learn to evaluate the performance of the model on the testing set.