Problem Statement:
Laptop Battery Life
Fred is a very predictable man. For instance, when he uses his laptop, all he does is watch TV
shows. He keeps on watching TV shows until his battery dies. Also, he is a very meticulous
man, i.e. he pays great attention to minute details. He has been keeping logs of every time he
charged his laptop, which includes how long he charged his laptop for and after that how long
was he able to watch the TV. Now, Fred wants to use this log to predict how long will he be
able to watch TV for when he starts so that he can plan his activities after watching his TV
shows accordingly.
You are given access to Fred’s laptop charging log by reading from the file “trainingdata.txt”.
The training data file will consist of 100 lines, each with 2 comma-separated numbers.
1. The first number denotes the amount of time the laptop was charged.
2. The second number denotes the amount of time the battery lasted.
The input for each of the test cases will consist of exactly 1 number rounded to 2 decimal
places. For each input, output 1 number: the amount of time you predict his battery will last.
Sample Input
1.50
Sample Output
3.00
Sample Testing of DataSet:
Source Code:
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split, cross_val_score
df = pd.read_csv("fred_charging_log.csv")
X = df[["Charge Time"]].values
y = df["Battery Life"].values
model = LinearRegression()
model.fit(X, y)
y_pred = model.predict(X)
average_battery_life = np.mean(y)
mse = mean_squared_error(y, y_pred)
print(f" Average Battery Life: {average_battery_life:.2f} hrs")
print(f" Mean Squared Error: {mse:.4f}")
biases = []
variances = []
for i in range(100):
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
model = LinearRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
bias_squared = (y_test - y_pred.mean())**2
variance = y_pred.var()
biases.append(bias_squared.mean())
variances.append(variance)
avg_bias = np.mean(biases)
avg_variance = np.mean(variances)
print(f"Estimated Bias²: {avg_bias:.4f}")
print(f" Estimated Variance: {avg_variance:.4f}")
try:
charge_time = float(input("Enter charge time in hours: "))
test_input = np.array([[charge_time]])
prediction = model.predict(test_input)
print(f" Predicted Battery Life for {charge_time:.2f} hrs charge: {prediction[0]:.2f} hrs")
except ValueError:
print(" Please enter a valid number.")
Output:
Difficulties Faced:
1. Dataset Preparation
• Creating a Structured Dataset:
o It was challenging to manually extract and organize the data into a clean
format (CSV), ensuring correct alignment of charge time and battery life
values.
• Missing or Duplicate Values:
o Ensuring there were no duplicate or inconsistent entries required extra
checking and cleaning.
2. Model Training & Evaluation
• Understanding Model Fit:
o Choosing and applying linear regression required us to understand how the
model works and ensure it fit our data well.
• Performance Metrics:
o Calculating metrics like Mean Squared Error (MSE) and interpreting their
meaning took time and required research.
3. Bias and Variance Estimation
• Conceptual Complexity:
o Bias-variance trade-off was conceptually difficult and needed repetition with
multiple train-test splits to estimate properly.
• Implementation Effort:
o Writing code for bias and variance estimation manually required careful
looping and averaging.
4. User Interaction
• Handling User Input:
o We had to validate and sanitize user input to avoid crashes from non-numeric
entries.
• Integrating Prediction Logic:
o Dynamically predicting based on input and showing clear, readable output
required clean code and formatting.
5. Data Visualization
• Choosing the Right Plots:
o Deciding between bar charts, scatter plots, and line plots to best represent the
data took some trial and error.
• Plot Readability:
o Making the plots informative and aesthetically clear (titles, labels, legends)
needed attention to detail.
Team Members & Contributions:
Malaravan S
• Collected and organized the raw data
• Assisted in data cleaning and formatting
Megala M
• Handled dataset preparation and removal of inconsistencies
• Verified data structure for model input
Mohamed Musthaba S M
• Developed and trained the linear regression model
• Implemented evaluation metrics like MSE
Mohanapriyan M
• Coded the user input system for dynamic predictions
• Validated and handled input errors gracefully
Monika A
• Managed file upload and execution on Google Colab
• Ensured compatibility of CSV with the environment
Mathivaanan V
• Conducted testing and debugging of the code
• Validated model predictions with sample data
Mouleesh Prasanna M
• Analyzed bias and variance of the model using cross-validation
• Suggested model improvements based on findings
NandhaGopal K
• Wrote and structured the project report
• Documented code with comments and explanations
Soundappan S
• Created data visualizations using matplotlib
• Led the team presentation and coordinated project tasks