[转载] sklearn学习之:(3)决策树回归算法

参考链接: Python | 使用sklearn进行决策树回归

使用决策树回归模型 DecisionTreeRegressor 来完成回归预测在波士顿房价数据集进行操作 

from sklearn.tree import DecisionTreeRegressor

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import StandardScaler,MinMaxScaler

from sklearn.datasets import load_boston

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

 

data = load_boston()

 

x = data.data

y = data.target

 

df = pd.DataFrame(x,columns=data.feature_names)

df['price'] = y

 

corr = df.corr()['price']

corr = corr.abs()

corr1 = corr.copy().drop('price')

 

features = []

n = 3            # 使用相关性最大的前三个特征

 

for i in range(n):

    index = corr1.argmax()

    features.append(corr1.keys()[index])

    corr1 = corr1.drop(corr1.keys()[index])

print(features)

 

 

new_x = df[features]

 

scale = MinMaxScaler()

new_x = scale.fit_transform(new_x)

 

x_train,x_test,y_train,y_test = train_test_split(new_x,y,test_size=0.33,random_state=17)

 

model = DecisionTreeRegressor()

model.fit(x_train,y_train)

score = model.score(x_test,y_test)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值