线性回归案例心得

机器学习一般步骤

1、导入数据集
2、查看数据集信息,处理缺失值
3、特征工程
4、特征非数值类型的编码,特征缩放
5、划分训练集和测试集,或训练集、验证集和测试集
6、选择模型
7、模型评估

线性回归案例

1、数据集信息:在这里插入图片描述
2、数据预处理

  • 缺失值为0个
    #查看缺失值 data.isnull().sum()
  • 对非数值类型数据使用labelEncoder

#这里对全部类型都使用了LabelEncoder编码,除了需预测值charges

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
firstdata = pd.DataFrame()
for col in data.columns:
    if col == 'charges':
        firstdata[col] = data[col]
        continue
    firstdata[col]=le.fit_transform(data[col])
firstdata.head()
  • 划分数据集,使用sklearn的train_test_split()函数
from sklearn.model_selection import train_test_split

# 划分自变量, 因变量
X = firstdata.iloc[:,:-1]
y = firstdata.iloc[:,-1]
# 划分测试集和训练集
x_train, x_test, y_train, y_test = train_test_split(X,y, test_size=0.2, random_state = 42)
  • 模型训练即评估
from sklearn.linear_model import LinearRegression
linear_model=LinearRegression()
linear_model.fit(x_train,y_train)
from sklearn.metrics import r2_score
y_pred=linear_model.predict(x_test)
print(r2_score(y_test,y_pred))
  • 问题:
    本次案例问题,之后尝试使用one_hot Encoder之后结果一致。但发现如果对目标值charges使用了labelEncoder之后,效果会提高,使得r2_score增加,不知道为何且是否合理
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值