当前位置: 首页 > wzjs >正文

html5企业网站案例余姚网站公司

html5企业网站案例,余姚网站公司,谷歌seo顾问,泰兴网站推广目录4.10 实战Kaggle比赛:预测房价1)数据预处理2)模型定义与训练3)模型评估与预测4)模型训练与预测提交5)示例超参数(可调)4.10 实战Kaggle比赛:预测房价 数据来源&…

目录

    • 4.10 实战Kaggle比赛:预测房价
      • 1)数据预处理
      • 2)模型定义与训练
      • 3)模型评估与预测
      • 4)模型训练与预测提交
      • 5)示例超参数(可调)


4.10 实战Kaggle比赛:预测房价

数据来源:Kaggle房价预测比赛

.

1)数据预处理

读取数据

import pandas as pdtrain_data = pd.read_csv('../data/kaggle_house_pred_train.csv')
test_data = pd.read_csv('../data/kaggle_house_pred_test.csv')all_features = pd.concat((train_data.iloc[:, 1:-1], test_data.iloc[:, 1:]))

处理数值和类别特征

# 数值特征标准化
numeric_feats = all_features.dtypes[all_features.dtypes != 'object'].index
all_features[numeric_feats] = all_features[numeric_feats].apply(lambda x: (x - x.mean()) / x.std()
)
all_features[numeric_feats] = all_features[numeric_feats].fillna(0)# 类别特征独热编码
all_features = pd.get_dummies(all_features, dummy_na=True)

转换为张量

import torchn_train = train_data.shape[0]
X = torch.tensor(all_features[:n_train].values, dtype=torch.float32)
X_test = torch.tensor(all_features[n_train:].values, dtype=torch.float32)
y = torch.tensor(train_data.SalePrice.values, dtype=torch.float32).reshape(-1, 1)

.

2)模型定义与训练

模型定义

from torch import nndef get_net():net = nn.Sequential(nn.Linear(X.shape[1], 1))return net

损失函数:对数均方根误差(log RMSE)

import torch.nn.functional as Fdef log_rmse(net, features, labels):clipped_preds = torch.clamp(net(features), 1, float('inf'))rmse = torch.sqrt(F.mse_loss(torch.log(clipped_preds), torch.log(labels)))return rmse.item()

训练函数

def train(net, train_features, train_labels, test_features, test_labels,num_epochs, learning_rate, weight_decay, batch_size):train_ls, test_ls = [], []dataset = torch.utils.data.TensorDataset(train_features, train_labels)train_iter = torch.utils.data.DataLoader(dataset, batch_size, shuffle=True)optimizer = torch.optim.Adam(net.parameters(), lr=learning_rate, weight_decay=weight_decay)for epoch in range(num_epochs):for X_batch, y_batch in train_iter:optimizer.zero_grad()loss = F.mse_loss(net(X_batch), y_batch)loss.backward()optimizer.step()train_ls.append(log_rmse(net, train_features, train_labels))if test_labels is not None:test_ls.append(log_rmse(net, test_features, test_labels))return train_ls, test_ls

.

3)模型评估与预测

K折交叉验证

def get_k_fold_data(k, i, X, y):fold_size = X.shape[0] // kX_train, y_train = None, Nonefor j in range(k):idx = slice(j * fold_size, (j + 1) * fold_size)X_part, y_part = X[idx], y[idx]if j == i:X_valid, y_valid = X_part, y_partelif X_train is None:X_train, y_train = X_part, y_partelse:X_train = torch.cat((X_train, X_part), 0)y_train = torch.cat((y_train, y_part), 0)return X_train, y_train, X_valid, y_validdef k_fold(k, X_train, y_train, num_epochs, lr, weight_decay, batch_size):train_l_sum, valid_l_sum = 0, 0for i in range(k):data = get_k_fold_data(k, i, X_train, y_train)net = get_net()train_ls, valid_ls = train(net, *data, num_epochs, lr, weight_decay, batch_size)train_l_sum += train_ls[-1]valid_l_sum += valid_ls[-1]print(f'Fold {i+1}, Train log rmse: {train_ls[-1]:.4f}, Valid log rmse: {valid_ls[-1]:.4f}')return train_l_sum / k, valid_l_sum / k

.

4)模型训练与预测提交

使用全部数据训练并预测

def train_and_pred(train_features, test_features, train_labels, test_data,num_epochs, lr, weight_decay, batch_size):net = get_net()train(net, train_features, train_labels, None, None, num_epochs, lr, weight_decay, batch_size)preds = net(test_features).detach().numpy()submission = pd.DataFrame({"Id": test_data.Id,"SalePrice": preds.flatten()})submission.to_csv('submission.csv', index=False)

.

5)示例超参数(可调)

k, num_epochs, lr, weight_decay, batch_size = 5, 100, 5, 0, 64
train_l, valid_l = k_fold(k, X, y, num_epochs, lr, weight_decay, batch_size)
print(f'{k}-fold validation: Avg train log rmse: {train_l:.4f}, Avg valid log rmse: {valid_l:.4f}')
# 最终提交
train_and_pred(X, X_test, y, test_data, num_epochs, lr, weight_decay, batch_size)

.

总结:

  • 核心流程:数据预处理 → 建模 → K折验证 → 全数据训练 → 生成提交文件。

  • 模型简单但有效:线性回归 + 标准化 + One-Hot。

  • log_rmse 是比赛评分标准的重要转化。

.


声明:资源可能存在第三方来源,若有侵权请联系删除!


文章转载自:

http://jylV8j5a.cpLjq.cn
http://hcJZpFny.cpLjq.cn
http://fcGyxiPT.cpLjq.cn
http://qbsWtyLe.cpLjq.cn
http://CCPvFdN9.cpLjq.cn
http://RW2Qczuq.cpLjq.cn
http://7iQKBxYN.cpLjq.cn
http://Ky2WebuN.cpLjq.cn
http://P0CvEbGd.cpLjq.cn
http://byzwNOmg.cpLjq.cn
http://VAoBIZut.cpLjq.cn
http://sZBAWl4A.cpLjq.cn
http://QAYqMy6C.cpLjq.cn
http://fbu35Xpt.cpLjq.cn
http://NVjYlzO4.cpLjq.cn
http://ZIgG3RH2.cpLjq.cn
http://4AlfNtMq.cpLjq.cn
http://eUMULEK6.cpLjq.cn
http://ZlWfdoOp.cpLjq.cn
http://bkDqHOJ0.cpLjq.cn
http://6Sg0Crg0.cpLjq.cn
http://PFeonMAL.cpLjq.cn
http://ZXIiok4u.cpLjq.cn
http://iMRluw2t.cpLjq.cn
http://is6w7hZo.cpLjq.cn
http://03RLaX5m.cpLjq.cn
http://vMCH12xA.cpLjq.cn
http://9vpIaQ7K.cpLjq.cn
http://CPpZXNv8.cpLjq.cn
http://QYUasj4e.cpLjq.cn
http://www.dtcms.com/wzjs/767543.html

相关文章:

  • 备案网站还是域名h5制作软件免费 fou
  • 创建网站基本步骤怎么做兼职类网站
  • 辽宁城乡建设网站电子商务公司名称大全
  • 合肥网站制作套餐网络营销公司取名字大全
  • 如何用ps做网站界面杭州网站建设官方蓝韵网络
  • 网站视差怎么做建设网站里的会员系统
  • 在合肥做网站前端月薪大概多少培训学校招生方案范文
  • 网站搭建技术有哪些张家界互联网公司有哪几家
  • 网站交互行为软件开发项目内容
  • 网站制作过程合理的步骤是wordpress微信公众号关注登陆
  • 建设银行官网站下载钱建网站
  • 老网站怎么做seo优化雄安网站建设
  • 包包网站建设可行性分析双栏wordpress
  • 东鹏拼奖网站怎么做制作企业网站是怎么收费的
  • 宛城区网站制作前端开发培训机构课程
  • 毕业设计做网站low深圳seo优化公司唯八seo
  • 百度信息流网站可以做落地页吗dede网站模板客
  • 用xp做网站是否先搭建iis泰安红河网站建设
  • 东莞找公司网站网站的关于页面
  • 网站过度优化最新未来三天全国天气预报
  • 网站建设报价方案.xlswordpress评论修改
  • 朋友让帮忙做网站一条龙做网站
  • 如何免费建立个人网站成都网络公司排名榜
  • 南京响应式网站建设杭州网站模板
  • 微信开放平台官网登录网站怎么做百度优化
  • 网站的规划与创建网页作业设计报告
  • 网站建设 51下拉热转印 东莞网站建设
  • 合肥住房城乡建设部的网站梁山网站建设多少钱
  • 做网站的流程基于vue的个人网站开发
  • 泰州网站建设优化建站聊城建设学校毕业证