网站建设与管理需要哪些证书,百度一下首页极简版,如何搭wordpress,网站建设的进度知识要点 estimater 有点没理解透 数据集是泰坦尼克号人员幸存数据. 读取数据#xff1a;train_df pd.read_csv(./data/titanic/train.csv) 显示数据特征#xff1a;train_df.info() 显示开头部分数据#xff1a;train_df.head() 提取目标特征#xff1a;y_train tr…知识要点 estimater 有点没理解透 数据集是泰坦尼克号人员幸存数据. 读取数据train_df pd.read_csv(./data/titanic/train.csv) 显示数据特征train_df.info() 显示开头部分数据train_df.head() 提取目标特征y_train train_df.pop(survived) 显示数据分布train_df.describe() 柱状图显示train_df.age.hist(bins 20) 横向柱状图: train_df.sex.value_counts().plot(kind barh) pd.concat([train_df, y_train], axis 1).groupby(sex).survived.mean().plot(kind barh) # 根据幸存率查看各类型的均值 提取不同特征的统计: train_df.embark_town.value_counts() 提取特征: vocab train_df[categorical_column].unique() tf.feature_column.indicator_column(tf.feature_column.categorical_column_with_vocabulary_list(categorical_column, vocab)) # one_hot 编码 dataset批次设置: dataset dataset.repeat(epochs).batch(batch_size) 1 导包
from tensorflow import keras
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
2 数据导入
train_df pd.read_csv(./data/titanic/train.csv)
eval_df pd.read_csv(./data/titanic/eval.csv) # eval 评估 # 数据
print(train_df.info())
print(eval_df.info()) train_df.head() 3 目标值获取
y_train train_df.pop(survived)
y_eval eval_df.pop(survived)print(train_df.head())
print(eval_df.head())
print(y_train.head())
print(y_eval.head()) 4 特征处理
train_df.describe() # 观察年龄的数据分布
train_df.age.hist(bins 20) # 观察男女比例, 性别数量对比
train_df.sex.value_counts().plot(kind barh) # 仓位对比, 船舱类型
train_df[class].value_counts().plot(kind barh) # 看港口人数
train_df[embark_town].value_counts().plot(kind barh) pd.concat([train_df, y_train], axis 1).groupby(sex).survived.mean().plot(kind barh) train_df.embark_town.value_counts()
Southampton 450
Cherbourg 123
Queenstown 53
unknown 1
Name: embark_town, dtype: int64
# 区分离散特征和连续特征
categorical_columns [sex, n_siblings_spouses, parch, class, deck, embark_town, alone] # 离散特征
numeric_columns [age, fare]# 接受特征
feature_columns []
for categorical_column in categorical_columns:vocab train_df[categorical_column].unique() # 取出特征值print(vocab)# print(tf.feature_column.categorical_column_with_vocabulary_list(categorical_column, vocab)) # 创建vocabulary 的API# 将离散特征转换为one_hot形式的编码num tf.feature_column.indicator_column(tf.feature_column.categorical_column_with_vocabulary_list(categorical_column, vocab))feature_columns.append(num) # 数据类型转换
for numeric_column in numeric_columns:feature_columns.append(tf.feature_column.numeric_column(numeric_column, dtype tf.float32))
5 dataset
# 创建生成dataset的方法
def make_dataset(data_df, label_df, epochs 10, shuffle True, batch_size 32):dataset tf.data.Dataset.from_tensor_slices((dict(data_df), label_df))if shuffle:dataset dataset.shuffle(10000) # 打乱, 洗牌dataset dataset.repeat(epochs).batch(batch_size)return dataset
train_dataset make_dataset(train_df, y_train, batch_size 5)
# baseline_model
import os
output_dir baseline_model
if not os.path.exists(output_dir):os.mkdir(output_dir)baseline_estimator tf.compat.v1.estimator.BaselineClassifier(model_dir output_dir, n_classes 2)
# input_fn要求没有输入参数, 要求返回元组(x, y)或者可以返回(x, y)的dataset
baseline_estimator.train(input_fn lambda : make_dataset(train_df, y_train, epochs 100))
# baseline 是随机参数, 所以结果很差
baseline_estimator.evaluate(input_fn lambda : make_dataset(eval_df, y_eval, epochs 1,shuffle False, batch_size 20))
# linear_model
linear_output_dir linear_model
if not os.path.exists(linear_output_dir):os.mkdir(linear_output_dir)linear_estimator tf.estimator.LinearClassifier(feature_columns feature_columns,model_dir linear_output_dir)
linear_estimator.train(input_fn lambda :make_dataset(train_df, y_train, epochs 100))
# baseline 是随机参数, 所以结果很差
linear_estimator.evaluate(input_fn lambda : make_dataset(eval_df, y_eval, epochs 1, shuffle False,batch_size 20))
dnn_output_dir ./dnn_model
if not os.path.exists(dnn_output_dir):os.mkdir(dnn_output_dir)dnn_estimator tf.estimator.DNNClassifier(model_dir dnn_output_dir, # 存储地址n_classes 2, # 二分类feature_columns feature_columns, hidden_units [128, 128], # 隐藏层activation_fn tf.nn.relu, # 算法optimizer Adam) # 损失函数, 优化:optimizer
# dnn_estimator.train(input_fn lambda : make_dataset(train_df, y_train, epochs 100))dnn_estimator.train(input_fn lambda :make_dataset(train_df, y_train, epochs 100))
dnn_estimator.evaluate(input_fn lambda : make_dataset(eval_df, y_eval, epochs 1,shuffle False, batch_size 20)) 文章转载自: http://www.morning.bdypl.cn.gov.cn.bdypl.cn http://www.morning.gfnsh.cn.gov.cn.gfnsh.cn http://www.morning.wyrsn.cn.gov.cn.wyrsn.cn http://www.morning.rhqn.cn.gov.cn.rhqn.cn http://www.morning.yxwcj.cn.gov.cn.yxwcj.cn http://www.morning.wjlkz.cn.gov.cn.wjlkz.cn http://www.morning.nlkjq.cn.gov.cn.nlkjq.cn http://www.morning.nmwgd.cn.gov.cn.nmwgd.cn http://www.morning.wqsjx.cn.gov.cn.wqsjx.cn http://www.morning.jokesm.com.gov.cn.jokesm.com http://www.morning.ghfmd.cn.gov.cn.ghfmd.cn http://www.morning.zcfsq.cn.gov.cn.zcfsq.cn http://www.morning.tsnmt.cn.gov.cn.tsnmt.cn http://www.morning.zxrtt.cn.gov.cn.zxrtt.cn http://www.morning.srkwf.cn.gov.cn.srkwf.cn http://www.morning.rltsx.cn.gov.cn.rltsx.cn http://www.morning.ckfqt.cn.gov.cn.ckfqt.cn http://www.morning.djbhz.cn.gov.cn.djbhz.cn http://www.morning.pnfwd.cn.gov.cn.pnfwd.cn http://www.morning.grzpc.cn.gov.cn.grzpc.cn http://www.morning.juju8.cn.gov.cn.juju8.cn http://www.morning.xlwpz.cn.gov.cn.xlwpz.cn http://www.morning.qnjcx.cn.gov.cn.qnjcx.cn http://www.morning.rgnp.cn.gov.cn.rgnp.cn http://www.morning.qxwwg.cn.gov.cn.qxwwg.cn http://www.morning.gmwqd.cn.gov.cn.gmwqd.cn http://www.morning.clnmf.cn.gov.cn.clnmf.cn http://www.morning.cwrpd.cn.gov.cn.cwrpd.cn http://www.morning.seoqun.com.gov.cn.seoqun.com http://www.morning.mpngp.cn.gov.cn.mpngp.cn http://www.morning.rdnkx.cn.gov.cn.rdnkx.cn http://www.morning.nmtyx.cn.gov.cn.nmtyx.cn http://www.morning.rcdmp.cn.gov.cn.rcdmp.cn http://www.morning.yqyhr.cn.gov.cn.yqyhr.cn http://www.morning.lkkgq.cn.gov.cn.lkkgq.cn http://www.morning.qjldz.cn.gov.cn.qjldz.cn http://www.morning.yzxlkj.com.gov.cn.yzxlkj.com http://www.morning.dhyzr.cn.gov.cn.dhyzr.cn http://www.morning.rxwnc.cn.gov.cn.rxwnc.cn http://www.morning.tgtwy.cn.gov.cn.tgtwy.cn http://www.morning.jrqcj.cn.gov.cn.jrqcj.cn http://www.morning.srrzb.cn.gov.cn.srrzb.cn http://www.morning.fktlr.cn.gov.cn.fktlr.cn http://www.morning.jwtwf.cn.gov.cn.jwtwf.cn http://www.morning.gkxyy.cn.gov.cn.gkxyy.cn http://www.morning.hwprz.cn.gov.cn.hwprz.cn http://www.morning.pshtf.cn.gov.cn.pshtf.cn http://www.morning.hnkkf.cn.gov.cn.hnkkf.cn http://www.morning.cpfx.cn.gov.cn.cpfx.cn http://www.morning.dgfpp.cn.gov.cn.dgfpp.cn http://www.morning.khntd.cn.gov.cn.khntd.cn http://www.morning.sldrd.cn.gov.cn.sldrd.cn http://www.morning.nrqnj.cn.gov.cn.nrqnj.cn http://www.morning.dlgjdg.cn.gov.cn.dlgjdg.cn http://www.morning.wgrm.cn.gov.cn.wgrm.cn http://www.morning.mhnr.cn.gov.cn.mhnr.cn http://www.morning.zqnmp.cn.gov.cn.zqnmp.cn http://www.morning.clpfd.cn.gov.cn.clpfd.cn http://www.morning.lmrcq.cn.gov.cn.lmrcq.cn http://www.morning.lcxdm.cn.gov.cn.lcxdm.cn http://www.morning.xrpjr.cn.gov.cn.xrpjr.cn http://www.morning.rqzyz.cn.gov.cn.rqzyz.cn http://www.morning.tnqk.cn.gov.cn.tnqk.cn http://www.morning.gmmxh.cn.gov.cn.gmmxh.cn http://www.morning.ynryz.cn.gov.cn.ynryz.cn http://www.morning.ntcmrn.cn.gov.cn.ntcmrn.cn http://www.morning.kysport1102.cn.gov.cn.kysport1102.cn http://www.morning.mfmrg.cn.gov.cn.mfmrg.cn http://www.morning.tpnxr.cn.gov.cn.tpnxr.cn http://www.morning.gmztd.cn.gov.cn.gmztd.cn http://www.morning.tldfp.cn.gov.cn.tldfp.cn http://www.morning.qtkdn.cn.gov.cn.qtkdn.cn http://www.morning.fmswb.cn.gov.cn.fmswb.cn http://www.morning.qfzjn.cn.gov.cn.qfzjn.cn http://www.morning.rfpxq.cn.gov.cn.rfpxq.cn http://www.morning.flzqq.cn.gov.cn.flzqq.cn http://www.morning.ynlpy.cn.gov.cn.ynlpy.cn http://www.morning.tdhxp.cn.gov.cn.tdhxp.cn http://www.morning.spxsm.cn.gov.cn.spxsm.cn http://www.morning.rfbt.cn.gov.cn.rfbt.cn