-
Notifications
You must be signed in to change notification settings - Fork 3
/
app_model.py
108 lines (81 loc) · 3.47 KB
/
app_model.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
from flask import Flask, request, jsonify
from flask_cors import CORS
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestRegressor
from datetime import datetime, timedelta
import pymongo
app = Flask(__name__)
CORS(app)
client = pymongo.MongoClient("mongodb+srv://kunalsonne:[email protected]/Auth")
db = client['home']
collection = db['blogs']
data_cursor = collection.find()
data = []
for record in data_cursor:
record.pop('_id', None)
data.append(record)
df = pd.DataFrame(data)
df['latitude'] = df['coordinates'].apply(lambda x: x['latitude'] if isinstance(x, dict) else None)
df['longitude'] = df['coordinates'].apply(lambda x: x['longitude'] if isinstance(x, dict) else None)
df = df.drop(columns=['coordinates'])
df['timestamp'] = pd.to_datetime(df['timestamp'])
df['hour'] = df['timestamp'].dt.hour
df['day_of_week'] = df['timestamp'].dt.dayofweek
df['month'] = df['timestamp'].dt.month
df['minute'] = df['timestamp'].dt.minute
df = df.dropna(subset=['latitude', 'longitude'])
X = df[['latitude', 'longitude', 'hour', 'day_of_week', 'month', 'minute']]
y = df['count']
model = RandomForestRegressor(n_estimators=100, random_state=42)
model.fit(X, y)
def predict_future_count(latitude, longitude, time_interval_minutes):
current_time = datetime.now()
future_time = current_time + timedelta(minutes=time_interval_minutes)
hour = future_time.hour
day_of_week = future_time.weekday()
month = future_time.month
minute = future_time.minute
future_data = np.array([[latitude, longitude, hour, day_of_week, month, minute]])
predicted_count = model.predict(future_data)[0]
return predicted_count
@app.route('/past_trend', methods=['POST'])
def past_trend():
try:
data = request.get_json()
latitude = float(data['latitude'])
longitude = float(data['longitude'])
time_interval = int(data['time_interval'])
location_data = df[(df['latitude'] == latitude) & (df['longitude'] == longitude)]
if location_data.empty:
return jsonify({'error': 'No data available for this location'}), 400
trend_data = []
current_time = datetime.now()
for i in range(0, 48):
future_time = current_time - timedelta(minutes=i * time_interval)
future_hour = future_time.hour
future_day_of_week = future_time.weekday()
future_month = future_time.month
future_minute = future_time.minute
future_data = np.array([[latitude, longitude, future_hour, future_day_of_week, future_month, future_minute]])
predicted_count = model.predict(future_data)[0]
trend_data.append({
'timestamp': future_time.strftime('%Y-%m-%d %H:%M:%S'),
'predicted_count': predicted_count
})
return jsonify({'trend_data': trend_data})
except Exception as e:
return jsonify({'error': str(e)}), 400
@app.route('/predict', methods=['POST'])
def predict():
try:
data = request.get_json()
latitude = float(data['latitude'])
longitude = float(data['longitude'])
time_interval = int(data['time_interval'])
predicted_count = predict_future_count(latitude, longitude, time_interval)
return jsonify({'predicted_count': predicted_count})
except Exception as e:
return jsonify({'error': str(e)}), 400
if __name__ == '__main__':
app.run(debug=True)