sklearn(9):Xgboost和LightGBM

本文探讨了在不同场景下使用Xgboost和LightGBM进行回归任务的效果,包括人脸数据自动补齐、鸢尾花分类和回归。重点讨论了LightGBM在速度和内存占用上的优势,相比xgboost,LightGBM在保持高精度的同时,训练时间更短,资源消耗更低。
摘要由CSDN通过智能技术生成

使用多种回归方法自动补齐人脸

import numpy as np
import  matplotlib.pyplot as plt
#构建方程
from sklearn.linear_model import  LinearRegression,Ridge,Lasso
#
from sklearn.neighbors import  KNeighborsRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn import  datasets
from  sklearn.model_selection import  train_test_split

#data_home:保存的地址
faces=datasets.fetch_olivetti_faces()
# print(faces)
#获取数据
x=faces.data
images=faces.images
y=faces.target
print(x.shape,images.shape,y.shape)

plt.figure(figsize=(2,2))

#随即查看图片
# index=np.random.randint(0,400,size=1)[0]
# img=images[index]
# plt.imshow(img,cmap=plt.cm.gray)
# plt.show()

#将x分成上半张人脸和下半,并打印出来,水平方向一分为二
x_up=x[:,:2048]
x_down=x[:,2048:]
'''
index=np.random.randint(0,400,size=1)[0]
axes=plt.subplot(1,3,1)
img1=x_up[index].reshape(32,64)
axes.imshow(img1,cmap=plt.cm.gray)
axes=plt.subplot(1,3,2)
img2=x_down[index].reshape(32,64)
axes.imshow(img2,cmap=plt.cm.gray)
axes=plt.subplot(1,3,3)
img3=x[index].reshape(64,64)
axes.imshow(img3,cmap=plt.cm.gray)
plt.show()
'''
#根据上半张人脸(x)预测下班张人脸(y)
x=x_up.copy()
y=x_down.copy()
print(x.shape,y.shape)

#分割训练集和数据集
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=30,random_state=1)

estimators={}
estimators[0]=LinearRegression()
estimators[1]=Ridge(alpha=0.1)
estimators[2]=Lasso(alpha=1)
estimators[3]=KNeighborsRegressor(n_neighbors=5)
estimators[4]=DecisionTreeRegressor() #决策树做回归使用的criterion是mse

label=["linear","ridge","lasso","knn","tree"]
result={}
for key,model in estimators.items():
    model.fit(x_train,y_train)
    y_hat=model.predict(x_test) #预测的是下半张脸
    result[label[key]]=y_hat

plt.figure(figsize=(14,20))
#结果可视化
for i in range(0,10):
    #第一列,上半张人脸
    axes=plt.subplot(10,7,i*7+1)
    up_face=x_test[i].reshape(32,64) #上半张连
    axes.imshow(u
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值