Sort by

recency

|

214 Discussions

|

  • + 0 comments

    Auto locksmith training offers hands-on experience in key cutting, lock repairs, and security solutions for vehicles. Master the skills needed to tackle car lockouts, transponder key programming, and ignition repairs. With expert instructors and a comprehensive curriculum, this training prepares you for a successful career in the automotive locksmith industry. Enroll now in auto locksmith training to unlock new opportunities and enhance your skills in a high-demand field!

  • + 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
    
  • + 0 comments

    Even not strictly necessary for this challenge, i got back to theory and implentation of least square regression, as this method opens solutions for a lot of problems, including this one.

    I super recommned this book: https://pythonnumericalmethods.studentorg.berkeley.edu/notebooks/chapter16.04-Least-Squares-Regression-in-Python.html

    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    import pandas as pd
    import numpy as np
    from scipy import optimize
    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]
       
        # create basis functions for least square regression 
        def basis_func_lin (x, b1):
            return b1 * pow(x,1)
            
        # or
        def basis_func_quad (x, b1, b2):
            return b1 * pow(x,1) + b2 * pow(x,2)
    
        #choose func
        func = basis_func_lin
        
        # do training (least square regression)
        beta_params, covariance = optimize.curve_fit(func, x, y)
        
       # Make prediction
        if timeCharged > 4.01:  # Battery fully charged
            print(8)  
        else:
            predicted_time = func(timeCharged, *beta_params)
            print(predicted_time)  # Output the predicted battery life
            
        
        
    
  • + 0 comments
    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    import numpy as np  
    from sklearn.linear_model import LinearRegression
    
    data = []
    
    with open('trainingdata.txt', 'r') as file:
        for line in file:
            charging_time, battery_lasted = map(float, line.strip().split(','))
            data.append((charging_time, battery_lasted))
    
    
    data = np.array(data) 
    
    X = data[:, 0]
    y = data[:, 1]
    
    # determines the minimum charging time at which the battery life reaches its maximum possible value (8.00 hours).
    
    #  find the smallest charging time at which the battery life reaches 8.00.
    
    threshold = min(X[y== 8])
    
    
    filtered_X = X[X < threshold].reshape(-1, 1)
    filtered_y = y[X < threshold]
    
    lr = LinearRegression().fit(filtered_X, filtered_y)
    
    def predict_battery_life(charge_time):
        if charge_time >= threshold:
            return 8 
        return lr.predict(np.array([[charge_time]]))[0] 
    
    if __name__ == '__main__':
        timeCharged = float(input().strip())
        predicted_life = predict_battery_life(timeCharged)
        print(round(predicted_life, 2))
    
  • + 0 comments

    !/bin/python3

    import math import os import random import re import sys

    if name == 'main': timeCharged = float(input().strip()) timeLast = timeCharged * 2 if timeLast > 8: timeLast = 8 print(timeLast)