tensorflow泰坦尼克号沉船数据预测模型

2024-06-14 08:38

本文主要是介绍tensorflow泰坦尼克号沉船数据预测模型,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

首先下载数据
https://www.kaggle.com/c/titanic/data
kaggle上面的数据

import pandas as pd
import numpy as np
import os,sys
os.getcwd()
data = pd.read_csv(’./tt/train.csv’)
data.columns
data = data[[‘Survived’, ‘Pclass’, ‘Sex’, ‘Age’, ‘SibSp’,
‘Parch’, ‘Fare’, ‘Cabin’, ‘Embarked’]]
data[‘Age’] = data[‘Age’].fillna(data[‘Age’].mean())
data[‘Cabin’] = pd.factorize(data.Cabin)[0]
data.fillna(0,inplace = True)
data[‘p1’] = np.array(data[‘Pclass’] == 1).astype(np.int32)

data[‘p2’] = np.array(data[‘Pclass’] == 2).astype(np.int32)

data[‘p3’] = np.array(data[‘Pclass’] == 3).astype(np.int32)
del data[‘Pclass’]
data.Embarked.unique()

data[‘e1’] = np.array(data[‘Embarked’] == ‘S’).astype(np.int32)

data[‘e2’] = np.array(data[‘Embarked’] == ‘C’).astype(np.int32)

data[‘e3’] = np.array(data[‘Embarked’] == ‘Q’).astype(np.int32)

del data[‘Embarked’]

data[‘Sex’] = [1 if x == ‘male’ else 0 for x in data.Sex]

data.values.dtype
data_train = data[[ ‘Sex’, ‘Age’, ‘SibSp’,
‘Parch’, ‘Fare’, ‘Cabin’, ‘p1’,‘p2’,‘p3’,‘e1’,‘e2’,‘e3’]]
data_target = data[‘Survived’].values.reshape(len(data),1)

np.shape(data_train),np.shape(data_target)

import tensorflow as tf

x = tf.placeholder(“float”,shape=[None,12])
y = tf.placeholder(“float”,shape=[None,1])

weight = tf.Variable(tf.random_normal([12,1]))
bias = tf.Variable(tf.random_normal([1]))
output = tf.matmul(x,weight) + bias
pred = tf.cast(tf.sigmoid(output)>0.5,tf.float32)

loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels = y,logits = output))

loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels = y,logits = output))

train_step = tf.train.GradientDescentOptimizer(0.0003).minimize(loss)

accuracy = tf.reduce_mean(tf.cast(tf.equal(pred,y),tf.float32))

data_test = pd.read_csv(’./tt/test.csv’)

data_test.column

data_test.columns

In[42]:

date_test = data_test[[‘Pclass’, ‘Sex’, ‘Age’, ‘SibSp’, ‘Parch’,
‘Fare’, ‘Cabin’, ‘Embarked’]].copy()

In[43]:

data_test

In[44]:

data_test = data_test[[‘Pclass’, ‘Sex’, ‘Age’, ‘SibSp’, ‘Parch’,
‘Fare’, ‘Cabin’, ‘Embarked’]]

In[51]:

data_test

In[46]:

data_test[‘Age’] = data_test[‘Age’].fillna(data[‘Age’].mean())

In[47]:

data_test[‘Age’] = data_test[‘Age’].fillna(data_test[‘Age’].mean())

In[48]:

data_test

In[49]:

data_test[‘Age’] = data_test[‘Age’].fillna(data_test[‘Age’].mean())

In[50]:

data_test[‘Cabin’] = pd.factorize(data_test.Cabin)[0]

In[52]:

data_test = data_test[[‘Pclass’, ‘Sex’, ‘Age’, ‘SibSp’, ‘Parch’,
‘Fare’, ‘Cabin’, ‘Embarked’]].copy()
data_test[‘Cabin’] = pd.factorize(data_test.Cabin)[0]

In[53]:

data_test[‘Age’] = data_test[‘Age’].fillna(data_test[‘Age’].mean())

In[54]:

data_test.fillna(0,inplace = True)

In[55]:

data_test[‘Sex’] = [1 if x == ‘male’ else 0 for x in data_test.Sex]

In[56]:

data_test[‘p1’] = np.array(data_test[‘Pclass’] == 1).astype(np.int32)
data_test[‘p2’] = np.array(data_test[‘Pclass’] == 2).astype(np.int32)
data_test[‘p3’] = np.array(data_test[‘Pclass’] == 3).astype(np.int32)
data_test[‘e1’] = np.array(data_test[‘Embarked’] == ‘S’).astype(np.int32)
data_test[‘e2’] = np.array(data_test[‘Embarked’] == ‘C’).astype(np.int32)
data_test[‘e3’] = np.array(data_test[‘Embarked’] == ‘Q’).astype(np.int32)
del data_test[‘Pclass’]
del data_test[‘Embarked’]

In[57]:

test_lable = pd.read_csv(’./tt/gender.csv’)
test_lable = np.reshape(test_lable.Survived.values.astype(np.float32),(418,1))

In[58]:

test_lable = pd.read_csv(’./tt/gender.csv’)
test_lable = np.reshape(test_lable.Survived.values.astype(np.float32),(418,1))

In[59]:

sess = tf.Session()
sess.run(tf.global_variables_initializer())
loss_train = []
train_acc = []
test_acc = []

In[61]:

for i in range(25000):
index = np.random.permutation(len(data_target))
data_train = data_train[index]
data_target = data_target[index]
for n in range(len(data_target)//100 + 1):
batch_xs = data_train[n100:n100 + 100]
batch_ys = data_target[n100:n100 + 100]
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})
if i % 1000==0:
loss_temp = sess.run(loss,feed_dict={x: batch_xs,y: batch_ys})
loss_train.append(loss_temp)
train_acc_temp = sess.run(accuracy,feed_dict={x: batch_xs,y: batch_ys})
train_acc.append(train_acc_temp)
print(loss_temp,train_acc_temp)

In[62]:

for i in range(25000):
index = np.random.permutation(len(data_target))
data_train = data_train[index]
data_target = data_target[index]
for n in range(len(data_target)//100 + 1):
batch_xs = data_train[n100:n100 + 100]
batch_ys = data_target[n100:n100 + 100]
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})
if i%1000 == 0:
loss_temp = sess.run(loss,feed_dict={x: batch_xs,y: batch_ys})
loss_train.append(loss_temp)
train_acc_temp = sess.run(accuracy,feed_dict={x: batch_xs,y: batch_ys})
train_acc.append(train_acc_temp)
print(loss_temp,train_acc_temp)

In[64]:

for i in range(25000):
index = np.random.permutation(len(data_target))
data_train = data_train[index]
data_target = data_target[index]
for n in range(len(data_target)//100 + 1):
batch_xs = data_train[n100:n100 + 100]
batch_ys = data_target[n100:n100 + 100]
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})
if i%1000 == 0:
loss_temp = sess.run(loss,feed_dict={x: batch_xs,y: batch_ys})
loss_train.append(loss_temp)
train_acc_temp = sess.run(accuracy,feed_dict={x: batch_xs,y: batch_ys})
train_acc.append(train_acc_temp)
print(loss_temp,train_acc_temp)

In[65]:

for i in range(25000):
index = np.random.permutation(len(data_target))
data_train = data_train[index]
data_target = data_target[index]
for n in range(len(data_target)//100 + 1):
batch_xs = data_train[n100:n100 + 100]
batch_ys = data_target[n100:n100 + 100]
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})
if i%1000 == 0:
loss_temp = sess.run(loss,feed_dict={x: batch_xs,y: batch_ys})
loss_train.append(loss_temp)
train_acc_temp = sess.run(accuracy,feed_dict={x: batch_xs,y: batch_ys})
train_acc.append(train_acc_temp)
print(loss_temp,train_acc_temp)

In[66]:

for i in range(25000):
#index = np.random.permutation(len(data_target))
#data_train = data_train[index]
#data_target = data_target[index]
for n in range(len(data_target)//100 + 1):
batch_xs = data_train[n100:n100 + 100]
batch_ys = data_target[n100:n100 + 100]
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})
if i%1000 == 0:
loss_temp = sess.run(loss,feed_dict={x: batch_xs,y: batch_ys})
loss_train.append(loss_temp)
train_acc_temp = sess.run(accuracy,feed_dict={x: batch_xs,y: batch_ys})
train_acc.append(train_acc_temp)
print(loss_temp,train_acc_temp)

In[67]:

import matplotlib.pyplot as plt

In[68]:

plt.plot(loss_train,‘k-’)
plt.title(‘train loss’)
plt.show()

In[69]:

plt.plot(train_acc,‘b–’,label = ‘train_acc’)
plt.plot(test_acc,‘r–’,label = ‘test_acc’)
plt.title(‘acc’)
plt.legend()
plt.show()

这篇关于tensorflow泰坦尼克号沉船数据预测模型的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1059913

相关文章

详谈redis跟数据库的数据同步问题

《详谈redis跟数据库的数据同步问题》文章讨论了在Redis和数据库数据一致性问题上的解决方案,主要比较了先更新Redis缓存再更新数据库和先更新数据库再更新Redis缓存两种方案,文章指出,删除R... 目录一、Redis 数据库数据一致性的解决方案1.1、更新Redis缓存、删除Redis缓存的区别二

Redis事务与数据持久化方式

《Redis事务与数据持久化方式》该文档主要介绍了Redis事务和持久化机制,事务通过将多个命令打包执行,而持久化则通过快照(RDB)和追加式文件(AOF)两种方式将内存数据保存到磁盘,以防止数据丢失... 目录一、Redis 事务1.1 事务本质1.2 数据库事务与redis事务1.2.1 数据库事务1.

Oracle Expdp按条件导出指定表数据的方法实例

《OracleExpdp按条件导出指定表数据的方法实例》:本文主要介绍Oracle的expdp数据泵方式导出特定机构和时间范围的数据,并通过parfile文件进行条件限制和配置,文中通过代码介绍... 目录1.场景描述 2.方案分析3.实验验证 3.1 parfile文件3.2 expdp命令导出4.总结

更改docker默认数据目录的方法步骤

《更改docker默认数据目录的方法步骤》本文主要介绍了更改docker默认数据目录的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1.查看docker是否存在并停止该服务2.挂载镜像并安装rsync便于备份3.取消挂载备份和迁

不删数据还能合并磁盘? 让电脑C盘D盘合并并保留数据的技巧

《不删数据还能合并磁盘?让电脑C盘D盘合并并保留数据的技巧》在Windows操作系统中,合并C盘和D盘是一个相对复杂的任务,尤其是当你不希望删除其中的数据时,幸运的是,有几种方法可以实现这一目标且在... 在电脑生产时,制造商常为C盘分配较小的磁盘空间,以确保软件在运行过程中不会出现磁盘空间不足的问题。但在

Python基于火山引擎豆包大模型搭建QQ机器人详细教程(2024年最新)

《Python基于火山引擎豆包大模型搭建QQ机器人详细教程(2024年最新)》:本文主要介绍Python基于火山引擎豆包大模型搭建QQ机器人详细的相关资料,包括开通模型、配置APIKEY鉴权和SD... 目录豆包大模型概述开通模型付费安装 SDK 环境配置 API KEY 鉴权Ark 模型接口Prompt

Java如何接收并解析HL7协议数据

《Java如何接收并解析HL7协议数据》文章主要介绍了HL7协议及其在医疗行业中的应用,详细描述了如何配置环境、接收和解析数据,以及与前端进行交互的实现方法,文章还分享了使用7Edit工具进行调试的经... 目录一、前言二、正文1、环境配置2、数据接收:HL7Monitor3、数据解析:HL7Busines

Mybatis拦截器如何实现数据权限过滤

《Mybatis拦截器如何实现数据权限过滤》本文介绍了MyBatis拦截器的使用,通过实现Interceptor接口对SQL进行处理,实现数据权限过滤功能,通过在本地线程变量中存储数据权限相关信息,并... 目录背景基础知识MyBATis 拦截器介绍代码实战总结背景现在的项目负责人去年年底离职,导致前期规

Redis KEYS查询大批量数据替代方案

《RedisKEYS查询大批量数据替代方案》在使用Redis时,KEYS命令虽然简单直接,但其全表扫描的特性在处理大规模数据时会导致性能问题,甚至可能阻塞Redis服务,本文将介绍SCAN命令、有序... 目录前言KEYS命令问题背景替代方案1.使用 SCAN 命令2. 使用有序集合(Sorted Set)

SpringBoot整合Canal+RabbitMQ监听数据变更详解

《SpringBoot整合Canal+RabbitMQ监听数据变更详解》在现代分布式系统中,实时获取数据库的变更信息是一个常见的需求,本文将介绍SpringBoot如何通过整合Canal和Rabbit... 目录需求步骤环境搭建整合SpringBoot与Canal实现客户端Canal整合RabbitMQSp