Hackerrank Day 9: Multiple Linear Regression

Multiple Regression in Python

from sklearn import linear_model
x = [[5, 7], [6, 6], [7, 4], [8, 5], [9, 6]]
y = [10, 20, 60, 40, 50]
lm = linear_model.LinearRegression()
lm.fit(x, y)
a = lm.intercept_
b = lm.coef_
print a, b[0], b[1]

Running the above code produces the following output:

51.9534883721 6.6511627907 -11.1627906977

CODING TEST:

Sample Input

2 7
0.18 0.89 109.85
1.0 0.26 155.72
0.92 0.11 137.66
0.07 0.37 76.17
0.85 0.16 139.75
0.99 0.41 162.6
0.87 0.47 151.77
4
0.49 0.18
0.57 0.83
0.56 0.64
0.76 0.18

Sample Output

105.22
142.68
132.94
129.71

line1=list(map(int,input().split()))
m=line1[0]
n=line1[1]
X=[]
Y=[]
for i in range(n):
    line=list(map(float,input().split()))
    X.append(line[:m])
    Y.append(line[m])

from sklearn import linear_model
lm = linear_model.LinearRegression()
lm.fit(X, Y)

a = lm.intercept_
b = lm.coef_
#print a, b[0], b[1]
import numpy as np

n1=int(input())


for j in range(n1):
    line=list(map(float,input().split()))
    y=a+np.dot(line,b) 
    print(round(y,2))
发布了163 篇原创文章 · 获赞 90 · 访问量 6292

猜你喜欢

转载自blog.csdn.net/weixin_45405128/article/details/104195610