sklearn中的线性回归

时间:2025-05-10 08:05:52
  • #一元线型回归模型
  • print("一元线型回归模型")
  • import sklearn.linear_model as lm
  • import numpy as np
  • x=[1,3,5,7]
  • x=(x).reshape(-1,1)
  • print(x)
  • y = ([2, 4, 6, 8])
  • model=()
  • (x,y)
  • print("Intercept:",model.intercept_)
  • print("Coefficients:",model.coef_)
  • print("Prediction for 60:",(([60]).reshape((-1, 1))))
  • #二元线型回归模型(多元线型回归与此类似)
  • print("二元线型回归模型")
  • x1=[1,2,3,4]
  • x2=[2,4,6,8]
  • #矩形转置
  • x=([x1,x2]).T
  • print(x)
  • x=([x1,x2]).reshape(-1,2)
  • y = ([2, 4, 6, 8])
  • model=()
  • (x,y)
  • print("Intercept:",model.intercept_)
  • print("Coefficients:",model.coef_)
  • print("Prediction for [60,60]:",(([60,60]).reshape((-1, 2))))