Spaces:
Sleeping
Sleeping
File size: 1,073 Bytes
923450e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
import pickle
# 1. Load the iris dataset
iris = load_iris()
X = iris.data # features
y = iris.target # labels
# 2. Convert to a DataFrame (optional, for illustration)
df = pd.DataFrame(X, columns=iris.feature_names)
df['target'] = y
# 3. Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2,
random_state=42)
# 4. Train a simple Logistic Regression model
model = LogisticRegression(max_iter=200)
model.fit(X_train, y_train)
# 5. Evaluate the model (optional, just to see performance)
accuracy = model.score(X_test, y_test)
print(f"Model accuracy: {accuracy:.2f}")
# 6. Save the trained model as a pickle file
with open('model.pkl', 'wb') as f:
pickle.dump(model, f)
print("Model has been trained and saved as model.pkl")
|