大数据实训kaggle比赛-房价预测(下)

接着开始表演

下面介绍模型:Sequential 序贯模型

它是函数式模型的简略版,为最简单的线性、从头到尾的结构顺序,不分叉,是多个网络层的线性堆叠。

实现方法:

模型需要知道它所期待的输入的尺寸。出于这个原因,序贯模型中的第一层需要接收关于其输入尺寸的信息,后面的各个层则可以自动的推导出中间数据的尺寸,因此不需要为每个层都指定这个参数。有以下几种方法来做到这一点:

  • 传递一个input_shape参数给第一层。它是一个表示尺寸的元组(一个整数或None的元组,其中None表示可能为任何正整数)。在input_shape中不包含数据的batch大小。
  • 某些 2D 层,例如 Dense,支持通过参数 input_dim 指定输入尺寸,某些 3D 时序层支持 input_dim 和 input_length 参数。
  • 如果你需要为你的输入指定一个固定的 batch 大小(这对 stateful RNNs 很有用),你可以传递一个 batch_size 参数给一个层。如果你同时将 batch_size=32 和 input_shape=(6, 8) 传递给一个层,那么每一批输入的尺寸就为 (32,6,8)

基本命令 

  • 1,model.add()           添加层
  • 2,model.compile()     模型训练的BP模式设置
  • 3,model.fit()         模型训练参数设置+训练
  • 4,model.evaluate()         模型评估
  • 5,model.predict()     模型预测
  • 6, activation ()激活函数通过设置单独的激活层实现,也可以在构造层对象时通过传递
  • 7,relu()      整流线性单元      keras.activations.relu(x, alpha=0.0, max_value=None, threshold=0.0),返回值是一个张量
  • 8,model.compile()方法用于在配置训练方法时,告知训练时用的优化器、损失函数和准确率评测标准,

     model.compile(optimizer = 优化器,

                            loss = 损失函数,

                            metrics = ["准确率”])

 简介结束,代码如下:

开始组装神经网络:

model = Sequential()

model.add(Dense(20,activation='relu'))
model.add(Dense(20,activation='relu'))
model.add(Dense(20,activation='relu'))
model.add(Dense(20,activation='relu'))
model.add(Dense(1))
model.compile(optimizer='adam',loss='mse')

调整模型:model.fit()方法用于执行训练过程

model.fit(x=X_train,y=y_train,
          validation_data=(X_test,y_test),
          batch_size=128,epochs=400)

结果大致如下:

对模型进行概述:

model.summary()

 结果显示,所有数据均为可训练数据。 

导入数据:

test_path = 'C:/Users/ZXY13/Desktop/大数据练习/房价预测/test.csv'
test_data = pd.read_csv(test_path)
test_data.head()
test_data_respaldo = test_data.copy()

准备训练数据:

best_cols = [
    'OverallQual', 'GrLivArea', 'GarageCars', 'GarageArea', 'TotalBsmtSF', '1stFlrSF',
    'GarageFinish', 'KitchenQual', 'BsmtQual', 'ExterQual'
]

test_data = test_data[best_cols]

扫描测试数据:

test_data.info()

处理空值:


test_data['GarageCars'].fillna((test_data['GarageCars'].mean()), inplace=True)
test_data['GarageArea'].fillna((test_data['GarageArea'].mean()), inplace=True)
test_data['TotalBsmtSF'].fillna((test_data['TotalBsmtSF'].mean()), inplace=True)


test_data['GarageFinish'].fillna((test_data['GarageFinish'].value_counts().index[0]), inplace=True)
test_data['KitchenQual'].fillna((test_data['KitchenQual'].value_counts().index[0]), inplace=True)
test_data['BsmtQual'].fillna((test_data['BsmtQual'].value_counts().index[0]), inplace=True)

将选中的几列转化为类:

test_data[['GarageFinish', 'KitchenQual', 'BsmtQual', 'ExterQual']] = test_data[['GarageFinish', 'KitchenQual', 'BsmtQual', 'ExterQual']].astype('category')

编码:

encoder = OrdinalEncoder()
test_data[['GarageFinish', 'KitchenQual', 'BsmtQual', 'ExterQual']] = encoder.fit_transform(test_data[['GarageFinish', 'KitchenQual', 'BsmtQual', 'ExterQual']])

用更高的值缩放列:

cols = ['GrLivArea', 'GarageArea', 'TotalBsmtSF', '1stFlrSF']
standard_scaler = StandardScaler()
test_data[cols] = standard_scaler.fit_transform(test_data[cols])

定义我们的‘X’数据:

X_test = test_data
X_test

检查要插入到我们训练模型中的数据的类型:

print("X_test - ",X_test.shape)

做预测:

predictions = model.predict(X_test)
predictions[0][0]
newdf = pd.DataFrame()
for i, k in enumerate(predictions):
    newdf = newdf.append({'SalePrice' : k[0]},ignore_index=True)
newdf

预测结果:

final_df = pd.concat([test_data_respaldo['Id'], newdf], axis=1, join="inner")
final_df.to_csv('submission.csv', index=False)

得到结果:

final_df = pd.concat([test_data_respaldo['Id'], newdf], axis=1, join="inner")
final_df.to_csv('submission.csv', index=False)

大功告成!

欢迎关注我们的公众号一起讨论,一起学习

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进步小白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值