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()

你可能感兴趣的:(深度学习)