Spaces:
Running
Running
Commit ·
17e1815
1
Parent(s): bdb3eed
Upload 2 files
Browse files
app.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from flask import Flask, request, jsonify, render_template
|
| 3 |
+
import pickle
|
| 4 |
+
|
| 5 |
+
# Create flask app
|
| 6 |
+
flask_app = Flask(__name__)
|
| 7 |
+
model = pickle.load(open("model.pkl", "rb"))
|
| 8 |
+
|
| 9 |
+
@flask_app.route("/")
|
| 10 |
+
def Home():
|
| 11 |
+
return render_template("index.html")
|
| 12 |
+
|
| 13 |
+
@flask_app.route("/predict", methods = ["POST"])
|
| 14 |
+
def predict():
|
| 15 |
+
float_features = [float(x) for x in request.form.values()]
|
| 16 |
+
features = [np.array(float_features)]
|
| 17 |
+
prediction = model.predict(features)
|
| 18 |
+
return render_template("index.html", prediction_text = "The flower species is {}".format(prediction))
|
| 19 |
+
|
| 20 |
+
if __name__ == "__main__":
|
| 21 |
+
flask_app.run(debug=True)
|
model.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
from sklearn.preprocessing import StandardScaler
|
| 3 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 4 |
+
from sklearn.model_selection import train_test_split
|
| 5 |
+
import pickle
|
| 6 |
+
|
| 7 |
+
# Load the csv file
|
| 8 |
+
df = pd.read_csv("iris.csv")
|
| 9 |
+
|
| 10 |
+
print(df.head())
|
| 11 |
+
|
| 12 |
+
# Select independent and dependent variable
|
| 13 |
+
X = df[["Sepal_Length", "Sepal_Width", "Petal_Length", "Petal_Width"]]
|
| 14 |
+
y = df["Class"]
|
| 15 |
+
|
| 16 |
+
# Split the dataset into train and test
|
| 17 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=50)
|
| 18 |
+
|
| 19 |
+
# Feature scaling
|
| 20 |
+
sc = StandardScaler()
|
| 21 |
+
X_train = sc.fit_transform(X_train)
|
| 22 |
+
X_test= sc.transform(X_test)
|
| 23 |
+
|
| 24 |
+
# Instantiate the model
|
| 25 |
+
classifier = RandomForestClassifier()
|
| 26 |
+
|
| 27 |
+
# Fit the model
|
| 28 |
+
classifier.fit(X_train, y_train)
|
| 29 |
+
|
| 30 |
+
# Make pickle file of our model
|
| 31 |
+
pickle.dump(classifier, open("model.pkl", "wb"))
|