-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
121 lines (95 loc) · 4.07 KB
/
app.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from flask import Flask, request, jsonify, render_template
import numpy as np
import pandas as pd
from sklearn.metrics import mean_squared_error, r2_score, mean_absolute_error
from sklearn.model_selection import train_test_split, cross_val_score, KFold
from catboost import CatBoostRegressor
import time
import pickle
# Initialize the Flask application
app = Flask(__name__)
# Load and preprocess the dataset
def load_and_preprocess_data():
df = pd.read_csv('dataset.csv')
# Data Preprocessing
df = df.drop(['id', 'mw', 'dist', 'date', 'time'], axis=1)
# Impute missing values
df.fillna('missing', inplace=True)
# Identify categorical columns
categorical_cols = df.select_dtypes(include=['object']).columns.tolist()
# Separate features and target variable
y = df['xm']
X = df.drop('xm', axis=1)
return X, y, categorical_cols
# Train the CatBoost model
def train_knn_model(X, y, categorical_cols):
# Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train and evaluate CatBoost model using k-fold cross-validation
kfold = KFold(n_splits=3, shuffle=True, random_state=42)
catboost_model = CatBoostRegressor(iterations=100, learning_rate=0.1, depth=6, cat_features=categorical_cols, silent=True)
# Evaluate model using cross-validation
cv_scores = cross_val_score(catboost_model, X, y, cv=kfold, scoring='neg_mean_squared_error')
cv_r2_scores = cross_val_score(catboost_model, X, y, cv=kfold, scoring='r2')
# Print cross-validation results
print("Cross-Validation MSE Scores:", -cv_scores)
print("Cross-Validation MSE Mean:", -cv_scores.mean())
print("Cross-Validation R^2 Scores:", cv_r2_scores)
print("Cross-Validation R^2 Mean:", cv_r2_scores.mean())
# Fit the model on training data
start_time = time.time()
catboost_model.fit(X_train, y_train)
end_time = time.time()
# Predict on test data
y_test_pred = catboost_model.predict(X_test)
# Predict on training data
y_train_pred = catboost_model.predict(X_train)
# Calculate and print performance metrics
train_mse = mean_squared_error(y_train, y_train_pred)
train_r2 = r2_score(y_train, y_train_pred)
test_mse = mean_squared_error(y_test, y_test_pred)
test_r2 = r2_score(y_test, y_test_pred)
print(f"Training MSE: {train_mse}")
print(f"Training R^2: {train_r2}")
print(f"Test MSE: {test_mse}")
print(f"Test R^2: {test_r2}")
print(f"Training time: {end_time - start_time} seconds")
print("CatBoost Model")
print('Mean Absolute Error:', mean_absolute_error(y_test, y_test_pred))
print('Root Mean Squared Error:', np.sqrt(test_mse))
return catboost_model, X_train, X_test, y_train, y_test
# Load data and train model
X, y, categorical_cols = load_and_preprocess_data()
knn_model, X_train, X_test, y_train, y_test = train_knn_model(X, y, categorical_cols)
# Save the trained model
with open('catboost_model.pkl', 'wb') as file:
pickle.dump(knn_model, file)
# Load the trained model
with open('catboost_model.pkl', 'rb') as file:
knn_model = pickle.load(file)
# Home route
@app.route('/')
def home():
return render_template('index.html')
# Prediction route
@app.route('/predict', methods=['POST'])
def predict():
data = request.form
lat = float(data['lat'])
long = float(data['long'])
country = data['country']
city = data['city']
area = data['area']
direction = data['direction']
depth = float(data['depth'])
md = float(data['md'])
ritcher = float(data['ritcher'])
ms = float(data['ms'])
mb = float(data['mb'])
input_query = np.array([[lat, long, country, city, area, direction, depth, md, ritcher, ms, mb]])
prediction = knn_model.predict(input_query)[0]
# Return prediction as JSON
return render_template('index.html',prediction_text="Result: {}".format(prediction))
# Main function to run the Flask app
if __name__ == '__main__':
app.run(debug=True)