Sort by

recency

|

221 Discussions

|

  • + 0 comments
    import numpy as np
    from sklearn.preprocessing import PolynomialFeatures
    from sklearn.linear_model import LinearRegression
    
    def findRegressionOutput(x_test:float):
        data = []
        # Ensure trainingdata.txt is in the same directory
        with open('trainingdata.txt','r') as file:
            for line in file:
                charging_time, hour = map(float, line.strip().split(','))
                data.append((charging_time, hour))
            
            data = np.array(data)
            x_raw = data[:, 0]
            y_raw = data[:, 1]
            
            # 1. Identify the threshold where battery is fully charged
            threshold = min(x_raw[y_raw == 8])
            
            # 2. Filter correctly using a mask to keep shapes aligned
            mask = x_raw < threshold
            x = x_raw[mask].reshape(-1, 1)
            y = y_raw[mask]
            
            # 3. Model training
            features = PolynomialFeatures(degree=2)
            x_poly = features.fit_transform(x)
            
            model = LinearRegression()
            model.fit(x_poly, y)
            
            # 4. Prediction Logic
            if x_test >= threshold:
                print(8.00)
            else:
                # Fixed np.array syntax and 2D shape
                x_test_feature = features.transform(np.array([[x_test]]))
                y_pred = model.predict(x_test_feature)
                print(round(float(y_pred[0]), 2))
    
    if __name__ == '__main__':
        try:
            timeCharged = float(input().strip())
            findRegressionOutput(timeCharged)
        except EOFError:
            pass
    
  • + 0 comments

    #!/bin/python3

    import math import os import random import re import sys import numpy as np from sklearn.linear_model import LinearRegression from sklearn.preprocessing import PolynomialFeatures

    def prediction(inp):

    data1 = []
    
    with open('trainingdata.txt', 'r') as file:
        for line in file:
            charging_time, battery_lasted = map(float, line.strip().split(','))
            data1.append((charging_time, battery_lasted))
    
    
    data = np.array(data1) 
    
    datax = data[:, 0]
    
    datay = data[:, 1]
    
    # data set contains different charging time for battery to perform 8 hours. So, it is better to select only one value so that model does not get confused. 
    threshold = min(datax[datay== 8])
    
    datay = datay[datax<threshold]
    datax = datax[datax<threshold]
    
    datax = datax.reshape(-1,1)
    
    degree = 2
    poly = PolynomialFeatures(degree)
    datax = poly.fit_transform(datax)
    model = LinearRegression()
    model.fit(datax,datay)
    
    if inp >= threshold:
        print(8.0)
    else:
        dataxtest = poly.transform(np.array([[inp]]))
    
        result = model.predict(dataxtest)
        print(round(result[0],2))    
    

    if name == 'main': timeCharged = float(input().strip()) prediction(timeCharged)

  • + 0 comments

    I saw alot of the conversations here and none of them used IA to solve it, so I will give you a clue: It looks like an inverted ReLU function

  • + 0 comments

    Check out this video for full code with explanation to solve battery life prediction in python using linear regressio, https://youtu.be/48L4LIwW1Tg?si=KWfgSUgkDf3oWRDm

  • + 1 comment
    print([8 if timeCharged >= 4 else timeCharged * 2][0])