Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sklearn.datasets import load_iris
|
2 |
+
from sklearn.model_selection import train_test_split
|
3 |
+
from sklearn.ensemble import RandomForestClassifier
|
4 |
+
from sklearn.metrics import accuracy_score
|
5 |
+
|
6 |
+
# Load dataset (for demonstration)
|
7 |
+
data = load_iris()
|
8 |
+
X, y = data.data, data.target
|
9 |
+
|
10 |
+
# Split the dataset into training and testing sets
|
11 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
12 |
+
|
13 |
+
# Initialize RandomForestClassifier
|
14 |
+
classifier = RandomForestClassifier(n_estimators=100, random_state=42)
|
15 |
+
|
16 |
+
# Train the classifier
|
17 |
+
classifier.fit(X_train, y_train)
|
18 |
+
|
19 |
+
|
20 |
+
# Make predictions on the test set
|
21 |
+
y_pred = classifier.predict(X_test)
|
22 |
+
|
23 |
+
# Calculate accuracy
|
24 |
+
accuracy = accuracy_score(y_test, y_pred)
|
25 |
+
print("Accuracy:", accuracy)
|
26 |
+
|
27 |
+
|
28 |
+
# prompt: save the model object in pickle file
|
29 |
+
|
30 |
+
import pickle
|
31 |
+
|
32 |
+
# Save the trained model to a pickle file
|
33 |
+
with open('model.pkl', 'wb') as f:
|
34 |
+
pickle.dump(classifier, f)
|