广义线性模型python

一.常用线性模型:

  • 线性回归
  • 正则化线性模型-岭回归
  • 套索回归

二.线性模型的图形化表示:

  • coef_ 求系数
  • intercept_ 求截距
##糖尿病测试
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import numpy as np

X,y = load_diabetes()['data'],load_diabetes()['target']
X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=8)

lr = LinearRegression()
lr.fit(X,y)

print("训练数据得分:{:.2f}".format(lr.score(X_test,y_test)))



print(load_diabetes().keys())
print(load_diabetes()['data'][1])
print(load_diabetes()['feature_names'])
print(load_diabetes()['target'])
print(load_diabetes()['target_filename'])
'''
'''
newdia=[[18,1,110,100,0,0,0,0,0,0]]
predict = lr.predict(newdia)
print('测试结果:{}'.format(load_diabetes()['target'][predict]))

三.实例:

通过广义线性模型测试身高体重

print(load_diabetes().keys())
print(load_diabetes()['data'][1])
print(load_diabetes()['feature_names'])
print(load_diabetes()['target'])
print(load_diabetes()['target_filename'])
'''
'''
newdia=[[18,1,110,100,0,0,0,0,0,0]]
predict = lr.predict(newdia)
print('测试结果:{}'.format(load_diabetes()['target'][predict]))

###身高体重
from sklearn.neighbors import KNeighborsClassifier
from bunch import *
book = Bunch()
book.data = [[170,120],[175,115],[180,135],[160,120],[165,115],[160,105],[185,130],[170,130]]
book.target =[1,0,1,2,1,1,0,2]
book.target_name = ['正常','瘦','正常','胖','正常','正常','瘦','胖']

X,y = book['data'],book['target']
X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=8)
lr = LinearRegression()
lr.fit(X,y)

print("训练数据得分:{:.2f}".format(lr.score(X_test,y_test)))
newdia=np.array([[160,123]])
predict = lr.predict(newdia)
print('测试结果:{}'.format(np.array(book['target_name'])[int(np.round(predict))]))

后面为自己组建的数据测试,由于数据过少只作为参考,数据格式使用Bunch对象。

你可能感兴趣的:(python机器学习,python机器学习)