Skip to content

Commit

Permalink
traget variable deviation -> deviation rate
Browse files Browse the repository at this point in the history
  • Loading branch information
sayan1999 committed Jan 12, 2024
1 parent ceca8ff commit 7de2377
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 22 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pinned: false

# Cricket-Prophet: AI-Powered Cricket Score Prediction

**Predict cricket scores with accuracy beyond traditional projections!**
**Predict cricket scores with accuracy beyond traditional encounter-based or runrate-based predictions!**

## Key Features

Expand All @@ -27,7 +27,7 @@ pinned: false
## How It Works

1. **Live score scraping:** Fetches live match data from CricBuzz.
2. **Feature engineering:** Extracts relevant features from the match data.
2. **Feature engineering:** Extracts relevant features from the match data and tried to predict deviation of score from projected score
3. **Model prediction:** Applies a pre-trained Random Forest model to predict the final score.
4. **Visualization:** Displays predictions and insights clearly.

Expand All @@ -39,12 +39,12 @@ Refer to the below table, think of variance as MSE of projected score

| Format | Measuring after n balls played | Variance (Train) | Variance (Test) | Model MSE (Train) | Model MSE (Test) |
| ------ | ------------------------------ | ---------------- | --------------- | ----------------- | ---------------- |
| T20 | 30 | 498.41 | 514.90 | 8.88 | 8.91 |
| T20 | 60 | 218.03 | 234.74 | 5.78 | 5.89 |
| T20 | 90 | 80.19 | 82.53 | 3.94 | 4.03 |
| ODI | 120 | 745.17 | 702.39 | 12.50 | 14.52 |
| ODI | 180 | 360.02 | 358.21 | 9.01 | 10.03 |
| ODI | 240 | 134.28 | 125.87 | 5.00 | 5.93 |
| T20 | 30 | 498.41 | 514.90 | 9.02 | 9.26 |
| T20 | 60 | 218.03 | 234.74 | 5.73 | 5.89 |
| T20 | 90 | 80.19 | 82.53 | 3.82 | 3.79 |
| ODI | 120 | 745.17 | 702.39 | 13.28 | 13.97 |
| ODI | 180 | 360.02 | 358.21 | 9.12 | 9.83 |
| ODI | 240 | 134.28 | 125.87 | 4.33 | 5..06 |

**ODI Format**

Expand Down
30 changes: 28 additions & 2 deletions features.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import matplotlib.pyplot as plt
from cricksheet import get_all_matches

# import ydata_profiling
import ydata_profiling


## Reading IPL dataset
Expand All @@ -17,6 +17,28 @@

## Feature selection/creation and ngram creation

features_for_profiling = features = [
# "batting_team",
# "bowling_team",
# "balls",
# "runs",
# "wickets",
"wkt_last_5_overs",
# "runrate_last_5_overs",
"current_RR",
# "average",
"balls_left",
"wkts_left",
# "required_RR",
# "projected_score_more",
# "min_score_more",
# "max_score_more",
# "projected_avg_score_more",
"runrate_last_5_overs-current_RR",
"deviation_from_projected_rate",
"deviation_from_projected",
]

features = [
"matchid",
"format",
Expand All @@ -41,6 +63,7 @@
"final_score",
"final_score_more",
"deviation_from_projected",
"deviation_from_projected_rate",
]

getformat = {"ODI": 1, "T20": 2}
Expand Down Expand Up @@ -113,6 +136,7 @@ def extract_features(inning):
inning.final_score,
final_score_more,
round(deviation_from_projected),
(deviation_from_projected * 6) / balls_left,
)
)
return data
Expand All @@ -129,7 +153,9 @@ def save_features(innings, fname):
Xy = [xi for Xi in Xy for xi in Xi]
print(f"{len(Xy)=}")
featuresdf = pd.DataFrame(Xy, columns=features)
# ydata_profiling.ProfileReport(featuresdf, title=fname).to_file(fname + ".html")
ydata_profiling.ProfileReport(
featuresdf[features_for_profiling], title=fname
).to_file(fname + ".html")
featuresdf.to_feather(fname)
featuresdf.to_csv(fname + ".csv")

Expand Down
27 changes: 21 additions & 6 deletions model.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,19 @@
# "projected_avg_score_more",
"runrate_last_5_overs-current_RR",
]
target = "deviation_from_projected"
target = "deviation_from_projected_rate"


# evaluate
def evaluate(model, featuresdf, x_test, fname):
predictdf = featuresdf.loc[x_test.index].copy()
# print(predictdf.columns)
predictdf["h_deviation_from_projected"] = model.predict(
predictdf["h_deviation_from_projected_rate"] = model.predict(
featuresdf.loc[x_test.index][features]
)
predictdf["h_deviation_from_projected"] = (
predictdf["h_deviation_from_projected_rate"] * predictdf["balls_left"] / 6
)
predictdf["error"] = (
predictdf["h_deviation_from_projected"] - predictdf["deviation_from_projected"]
)
Expand Down Expand Up @@ -123,12 +126,24 @@ def plot_feature_importance(f, imp, fname):


def compare_util(model, fname, x_train, x_test, y_train, y_test, balls_left):
print(f'After {(300 if "odi" in fname.lower() else 120) - balls_left} overs')
print(f'After {(300 if "odi" in fname.lower() else 120) - balls_left} balls')

train_index = x_train["balls_left"] <= balls_left
test_index = x_test["balls_left"] <= balls_left
y_train_data = y_train[train_index] * x_train["balls_left"][train_index] / 6
y_test_data = y_test[test_index] * x_test["balls_left"][test_index] / 6
h_train_data = (
model.predict(x_train[train_index]) * x_train["balls_left"][train_index] / 6
)
h_test_data = (
model.predict(x_test[test_index]) * x_test["balls_left"][test_index] / 6
)

print(
f"Variance: {statistics.variance(y_train[x_train['balls_left']<=balls_left])=}, {statistics.variance(y_test[x_test['balls_left']<=balls_left])=}"
f"Data: Train Variance: {statistics.variance(y_train_data)}, Test Variance: {statistics.variance(y_test_data)}"
)
print(
f"Model: {mse(model.predict(x_train[x_train['balls_left']<=balls_left]), y_train[x_train['balls_left']<=balls_left], squared=False)=}, {mse(model.predict(x_test[x_test['balls_left']<=balls_left]), y_test[x_test['balls_left']<=balls_left], squared=False)=}"
f"Model: Train MSE: {mse(h_train_data, y_train_data, squared=False)}, Test MSE: {mse(h_test_data, y_test_data, squared=False)}"
)


Expand Down Expand Up @@ -199,7 +214,7 @@ def train(fname, max_depth=-1):

# print(f"{model.score(x_train, y_train)=}, {model.score(x_test, y_test)=}")
print(
f"{mse(model.predict(x_train), y_train, squared=False)=}, {mse(model.predict(x_test), y_test, squared=False)=}"
f"Train MSE: {mse(model.predict(x_train)*x_train['balls_left']/6, y_train*x_train['balls_left']/6, squared=False)}, Test MSE: {mse(model.predict(x_test)*x_test['balls_left']/6, y_test*x_test['balls_left']/6, squared=False)}"
)

compare(model, fname, x_train, x_test, y_train, y_test)
Expand Down
Binary file modified model/odifeatures.feather.joblib
Binary file not shown.
Binary file modified model/t20features.feather.joblib
Binary file not shown.
19 changes: 13 additions & 6 deletions serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,9 @@ def predict(url):
inputdf["batting_team"] = all_teams_enc
inputdf = inputdf[features]
model = load_model(format)
h = model.predict(inputdf)
print(f"{h=}")
h_rate = model.predict(inputdf)
print(f"{h_rate=}")
h = h_rate * balls_left / 6
projected_score_more = balls_left * current_rr / 6
projected = math.ceil(projected_score_more + runs)
predicted_score_more = math.ceil(h.mean() + projected_score_more)
Expand Down Expand Up @@ -380,7 +381,7 @@ def render(url):

live_matches = get_live_matches("https://cricbuzz.com")
if live_matches:
option = st.selectbox(
option = st.sidebar.selectbox(
"Choose a live match here",
list(live_matches.keys()) + ["Custom URL", "Simulator"],
)
Expand All @@ -407,9 +408,15 @@ def render(url):
- args["current_RR"]
)
balls = 300 if format == "ODI" else 120
st.text(
str(int((balls * args["current_RR"] / 6) + simulator(args, format)))
)
if st.button("Predict"):
st.text(
str(
int(
(balls * args["current_RR"] / 6)
+ simulator(args, format) * args["balls_left"] / 6
)
)
)
else:
if option == "Custom URL":
url = st.text_input("Enter cricbuzz match link")
Expand Down
Binary file modified static/T20_correlation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified static/odifeatures.feather.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified static/odifeatures.featherfeatureimp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified static/t20features.feather.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified static/t20features.featherfeatureimp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7de2377

Please sign in to comment.