• + 0 comments

    Using least square regression through definition (matrix inversions, dot products and transpose), no library:

    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    import pandas as pd
    import numpy as np
    from scipy import optimize
    #import matplotlib.pyplot as plt
    from io import StringIO
    
    
    if __name__ == '__main__':
        timeCharged = float(input().strip())
    
        # Read the data using pandas
        data = pd.read_csv('trainingdata.txt', header=None, names=["TimeCharged", "TimeLasted"])
        
        # training data: Range when not fully charge range
        index_values = data.query("TimeCharged <= 4.01").index.tolist()
        x = data.TimeCharged[index_values]
        y = data.TimeLasted[index_values]
    
        # Equation to be solved from book https://pythonnumericalmethods.studentorg.berkeley.edu/notebooks/chapter16.02-Least-Squares-Regression-Derivation-Linear-Algebra.html
        # y = b1 * f1(x)
       
        # A Matrix
        # Only one linear basis function used. Important: Method allows any kind and number of basis function, as long as beta params are constants
        # Basis function is  evaluated at the input measured values. In this case f1(x) = x 
        A = np.array(x).reshape(-1,1)
    
        # Y Matrix: Output measured values
        Y = np.array(y).reshape(-1,1) 
    
        # Here is where the magic happends: Least square regression to find beta parameters
        beta_params = np.linalg.inv(A.T @ A) @ A.T @ Y
        
        
       # Make prediction
        if timeCharged > 4.01:  # Battery fully charged
            print(8)  
        else:
            predicted_time = timeCharged *  beta_params[0][0]
            print(predicted_time)  # Output the predicted battery life