Sort by

recency

|

36 Discussions

|

  • + 0 comments
    def predict(x, y):
        ex = sum(x) / len(x)
        ey = sum(y) / len(y)
        numerator = sum([(xi - ex) * (yi - ey) for xi, yi in zip(x, y)])
        denominator = sum([(xi - ex)**2 for xi in x])
        slope = numerator / denominator
        intercept = ey - (slope * ex)
        predict = slope * 10 + intercept
        return predict
    
    if __name__ == '__main__':
        feature_1 = "Physics Scores  15  12  8   8   7   7   7   6   5   3"
        feature_2 = "History Scores  10  25  17  11  13  17  20  13  9   15"
        feature_1 = [int(x) for x in feature_1.split() if x.isnumeric()]
        feature_2 = [int(x) for x in feature_2.split() if x.isnumeric()]
        result = predict(feature_1, feature_2)
        print(f'{result:.3f}')
    
  • + 1 comment
    class LR:
        def __init__(self):
            self.slope = None
            self.intercept = None
            
        def fit(self, x, y):
            x_avg = sum(x)/x.__len__()
            y_avg = sum(y)/y.__len__()
            numerator = sum([(a - x_avg)*(b - y_avg) for a,b in zip(x,y)])
            denominator = sum([(a - x_avg)**2 for a in x])
            self.slope = round(numerator/denominator, 3)
            self.intercept = y_avg - self.slope*x_avg
        def predict(self, x):
            if self.slope and self.intercept:
                return self.slope*x + self.intercept
            else:
                raise ModuleNotFoundError('predict is not found, call fit() before predict()')
            
    
    if __name__ == '__main__':
        if __name__ == '__main__':
            feature_1 = "Physics Scores  15  12  8   8   7   7   7   6   5   3"
            feature_2 = "History Scores  10  25  17  11  13  17  20  13  9   15"
            feature_1 = [int(x) for x in input().split() if x.isnumeric()]
            feature_2 = [int(x) for x in input().split() if x.isnumeric()]
            model = LR()
            model.fit(feature_1, feature_2)
            out = str(model.predict(10))
            out = out.split('.')
            print(float(out[0] + '.' + out[1][0]))
    
  • + 0 comments

    Since we can't use any of imports, we will have to go basic and solve this using slope and intercept formulaes. Here is my code.

    physics = [15 , 12 , 8 , 8 , 7 , 7 , 7 , 6 , 5 , 3]
    history = [10 , 25 , 17 , 11 , 13 , 17 , 20 , 13 , 9 , 15]
    xy = sum([x*y for x,y in zip(physics , history)])
    xmean = sum(physics) / len(physics)
    ymean = sum(history) / len(history)
    x_2 = sum([x*x for x in physics])
    numerator = xy - ymean*(sum(physics))
    denominator = x_2 - xmean*(sum(physics))
    slope = numerator/denominator
    intercept = ymean - slope*xmean
    
    prediction = slope*10 + intercept
    
    print("%.2f" %prediction)
    
  • + 0 comments

    HINT: Select "Plain Text" as "Language" (my default was Python 3 and just writing the solution in there gave me an error).

  • + 0 comments

    no idea why not work pythonic

    import numpy as np
    
    P = np.array([15, 12, 8, 8, 7, 7, 7, 6, 5, 3])
    H = np.array([10, 25, 17, 11, 13, 17, 20, 13, 9, 15])
    
    fit = np.polyfit(P, H, deg=1)
    a = fit[0]
    b = fit[1]
    
    pred = a * 10 + b
    print(round(pred, 1))
    

    R

    x <- c(15, 12, 8, 8, 7, 7, 7, 6, 5, 3)
    y <- c(10, 25, 17, 11, 13, 17, 20, 13, 9, 15) 
    linearMod <- lm(y ~ x)
    pred <- as.vector(linearMod$coefficients)[1] + as.vector(linearMod$coefficients)[2]*10
    round(pred,1)