SSamson commited on
Commit
7cbecb6
·
verified ·
1 Parent(s): cdf5e89

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import pandas as pd
3
+ from sklearn.model_selection import train_test_split
4
+ from sklearn.ensemble import RandomForestRegressor
5
+ from sklearn.metrics import mean_absolute_error
6
+ from flask import Flask, request, jsonify
7
+
8
+ # Step 1: Data Collection
9
+ def fetch_data():
10
+ api_key = 'YOUR_API_KEY' # Replace with your SportsData.io API key
11
+ response = requests.get(f"https://api.sportsdata.io/v3/nba/stats/json/PlayerSeasonStats/2023?key={api_key}")
12
+ data = response.json()
13
+ return pd.DataFrame(data)
14
+
15
+ # Step 2: Data Preprocessing
16
+ def preprocess_data(df):
17
+ df = df.dropna() # Remove rows with missing values
18
+ df = df[df['Minutes'] > 0] # Filter players with playing time
19
+ df['PointsPerGame'] = df['Points'] / df['Games']
20
+ return df
21
+
22
+ # Step 3: Feature Engineering
23
+ def engineer_features(df):
24
+ df['RecentForm'] = df['PointsPerGame'].rolling(window=5).mean().fillna(0)
25
+ df['HomeAdvantage'] = df['HomeGames'] / df['TotalGames']
26
+ return df
27
+
28
+ # Step 4: Model Training
29
+ def train_model(df):
30
+ X = df[['RecentForm', 'HomeAdvantage']]
31
+ y = df['PointsPerGame']
32
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
33
+
34
+ model = RandomForestRegressor(n_estimators=100, random_state=42)
35
+ model.fit(X_train, y_train)
36
+
37
+ y_pred = model.predict(X_test)
38
+ mae = mean_absolute_error(y_test, y_pred)
39
+ print(f"Mean Absolute Error: {mae}")
40
+
41
+ return model
42
+
43
+ # Step 5: Deployment with Flask
44
+ app = Flask(__name__)
45
+
46
+ @app.route('/predict', methods=['POST'])
47
+ def predict():
48
+ data = request.json
49
+ input_features = [data['RecentForm'], data['HomeAdvantage']]
50
+ prediction = model.predict([input_features])[0]
51
+ return jsonify({'prediction': prediction})
52
+
53
+ if __name__ == '__main__':
54
+ # Fetch and preprocess data
55
+ df = fetch_data()
56
+ df = preprocess_data(df)
57
+ df = engineer_features(df)
58
+
59
+ # Train the model
60
+ model = train_model(df)
61
+
62
+ # Run the Flask app
63
+ app.run(debug=True, host='0.0.0.0', port=5000)