0% found this document useful (0 votes)
64 views3 pages

IPL Score Prediction - Ipynb - Colab

The document outlines a Python script that utilizes machine learning models, specifically Support Vector Regressor (SVR) and Random Forest Regressor, to predict cricket match scores based on various features. It includes data preprocessing steps such as encoding categorical variables, scaling features, and splitting the dataset into training and testing sets. The script also evaluates the models' performance using mean absolute error and mean squared error, and provides an interactive widget for users to input match details for score prediction.

Uploaded by

Kishore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views3 pages

IPL Score Prediction - Ipynb - Colab

The document outlines a Python script that utilizes machine learning models, specifically Support Vector Regressor (SVR) and Random Forest Regressor, to predict cricket match scores based on various features. It includes data preprocessing steps such as encoding categorical variables, scaling features, and splitting the dataset into training and testing sets. The script also evaluates the models' performance using mean absolute error and mean squared error, and provides an interactive widget for users to input match details for score prediction.

Uploaded by

Kishore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import pandas as pd

import numpy as np
import [Link] as plt
from [Link] import LabelEncoder, MinMaxScaler
from sklearn.model_selection import train_test_split
from [Link] import SVR
from [Link] import RandomForestRegressor
from [Link] import mean_absolute_error, mean_squared_error
import ipywidgets as widgets
from [Link] import display, clear_output
import warnings

# Suppress warnings for cleaner output


[Link]("ignore")

# Load the dataset


ipl = pd.read_csv('/content/ipl_data.csv')

# Drop unimportant features


df = [Link](['date', 'runs', 'wickets', 'overs', 'runs_last_5', 'wickets_last_5', 'mid

# Split into features (X) and target (y)


X = [Link](['total'], axis=1)
y = df['total']

# Initialize LabelEncoders for categorical features


venue_encoder = LabelEncoder()
batting_team_encoder = LabelEncoder()
bowling_team_encoder = LabelEncoder()
batsman_encoder = LabelEncoder()
bowler_encoder = LabelEncoder()

# Encode categorical features


X['venue'] = venue_encoder.fit_transform(X['venue'])
X['bat_team'] = batting_team_encoder.fit_transform(X['bat_team'])
X['bowl_team'] = bowling_team_encoder.fit_transform(X['bowl_team'])
X['batsman'] = batsman_encoder.fit_transform(X['batsman'])
X['bowler'] = bowler_encoder.fit_transform(X['bowler'])

# Split data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Scale the features


scaler = MinMaxScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = [Link](X_test)

# Initialize models
svr_model = SVR(kernel='rbf', C=100, epsilon=0.1) # Support Vector Regressor with RBF ke
rf_model = RandomForestRegressor(n_estimators=100, random_state=42) # Random Forest Regr

# Train both models


svr_model.fit(X_train_scaled, y_train)
rf_model.fit(X_train_scaled, y_train)
▾ RandomForestRegressor i ?

RandomForestRegressor(random_state=42)

# Make predictions
svr_predictions = svr_model.predict(X_test_scaled)
rf_predictions = rf_model.predict(X_test_scaled)

# Evaluate models
svr_mae = mean_absolute_error(y_test, svr_predictions)
svr_mse = mean_squared_error(y_test, svr_predictions)
rf_mae = mean_absolute_error(y_test, rf_predictions)
rf_mse = mean_squared_error(y_test, rf_predictions)

# Print evaluation metrics


print("Support Vector Regressor:")
print(f"Mean Absolute Error: {svr_mae:.2f}")
print(f"Mean Squared Error: {svr_mse:.2f}")
print("\nRandom Forest Regressor:")
print(f"Mean Absolute Error: {rf_mae:.2f}")
print(f"Mean Squared Error: {rf_mse:.2f}")

Support Vector Regressor:


Mean Absolute Error: 19.21
Mean Squared Error: 715.16

Random Forest Regressor:


Mean Absolute Error: 2.14
Mean Squared Error: 54.46

# Plot actual vs predicted scores for both models


[Link](figsize=(12, 5))

# SVR Plot
[Link](1, 2, 1)
[Link](y_test, svr_predictions, alpha=0.5)
[Link]([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], 'r--', lw=2)
[Link]('Actual Scores')
[Link]('Predicted Scores')
[Link]('SVR: Actual vs Predicted Scores')

# Random Forest Plot


[Link](1, 2, 2)
[Link](y_test, rf_predictions, alpha=0.5)
[Link]([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], 'r--', lw=2)
[Link]('Actual Scores')
[Link]('Predicted Scores')
[Link]('Random Forest: Actual vs Predicted Scores')

plt.tight_layout()
[Link]()
# Create interactive widgets for prediction
venue = [Link](options=df['venue'].unique().tolist(), description='Select Venue
batting_team = [Link](options=df['bat_team'].unique().tolist(), description='Se
bowling_team = [Link](options=df['bowl_team'].unique().tolist(), description='S
batsman = [Link](options=df['batsman'].unique().tolist(), description='Select B
bowler = [Link](options=df['bowler'].unique().tolist(), description='Select Bow
model_choice = [Link](options=['SVR', 'Random Forest'], description='Select Mod
predict_button = [Link](description="Predict Score")
output = [Link]()

# Define prediction function


def predict_score(b):
with output:
clear_output() # Clear previous output
try:
# Encode user inputs
encoded_venue = venue_encoder.transform([[Link]])[0]
encoded_batting_team = batting_team_encoder.transform([batting_team.value])[0
encoded_bowling_team = bowling_team_encoder.transform([bowling_team.value])[0
encoded_batsman = batsman_encoder.transform([[Link]])[0]
encoded_bowler = bowler_encoder.transform([[Link]])[0]

# Create input array


input_data = [Link]([[encoded_venue, encoded_batting_team, encoded_bowling_
input_scaled = [Link](input_data) # Scale input

# Select model for prediction


if model_choice.value == 'SVR':
predicted_score = svr_model.predict(input_scaled)[0]
model_name = 'SVR'
else:
predicted_score = rf_model.predict(input_scaled)[0]
model_name = 'Random Forest'

print(f"Predicted Score ({model_name}): {int(predicted_score)}")


except Exception as e:
print(f"Error in prediction: {str(e)}")

# Link button to prediction function


predict_button.on_click(predict_score)

# Display widgets
display(venue, batting_team, bowling_team, batsman, bowler, model_choice, predict_button

Select Venue: M Chinnaswamy Stadium

Select Battin… Chennai Super Kings

Select Bowlin… Royal Challengers Bangalore

Select Batsm… MS Dhoni

Select Bowler: PP Chawla

Select Model: Random Forest

Predict Score
Predicted Score (Random Forest): 175

You might also like