pyrhon3+tensorflow+ Titanic Data Set (data preprocessing + prediction)

Source: Internet
Author: User
Tags shuffle
Import TensorFlow as TF import numpy as NP import pandas as PD #数据预处理 def read_data (): Data=pd.read_csv (' train.csv ') #pandas read Data=data.fillna (0) #NAN fill in 0 datax=data[[' Sex ', ' age ', ' pclass ', ' sibsp ', ' parch ', ' Fare ', ' Emba Rked ']] #pandas选择列 datax=pd.get_dummies (datax) # one-hot encoded data[' deceased '] = data[' survived '].apply (
    Lambda s:1-s) #one-hot encoding Datay = data[[' survived ', ' deceased ']] return datax,datay if __name__== ' __main__ ':
    New_datax,new_datay=read_data () New_dataxx = New_datax.values.astype (np.float32) #把pandas矩阵 converted to a NP matrix no two-dimensional label becomes a deposit matrix New_datayy = New_datay.values.astype (np.float32) # Converts the pandas matrix to a NP matrix without a two-dimensional label becoming a deposit matrix Np.random.shuffle (New_dataxx) # Random scrambling sequence Np.random.shuffle (new_datayy) # random scrambling sequence sep = Int (0.7 * len (new_dataxx)) #拆分数据集 Train_datax = New_da
    TAXX[:SEP] # Training data (70%) train_datay=new_datayy[:sep] Test_datax = new_dataxx[sep:] # test data (30%) test_datay=new_datayy[Sep:] # new_datax.to_csv ("Taitan_onehot.csv", Index=false) #简便方法 an easy way to disrupt + split ' split ' from Sklearn.model
                                                  _selection import Train_test_split X_train, X_val, y_train, Y_val = Train_test_split (dataset_x, Dataset_Y, 
    test_size=0.2, random_state=42)
    "' #全链接神经层 input TF_INPUTX training Data's characteristic information tf_inputy training data label Tf_inputx=tf.placeholder (Tf.float32, [None, 11]) Tf_inputy=tf.placeholder (tf.float32,[none,2]) #搭建全链接神经网络 L1 = Tf.layers.dense (TF_INPUTX, 1000, Tf.nn.relu, NA Me= "L1") L2 = Tf.layers.dense (L1, 900, Tf.nn.relu, name= "L2") out = Tf.layers.dense (L2, 2, name= "L3") #计算 The probability is added to 1 Prediction=tf.nn.softmax (out, name= "pred") #计算误差 cost function loss = Tf.reduce_mean (tf.nn.softmax_cro Ss_entropy_with_logits (Labels=tf_inputy, logits=out)) #优化器 reduce error Train_step = Tf.train.AdamOptimizer (0.001). Minimi
    Ze (loss) #初始化变量init = Tf.global_variables_initializer () ' ' correct_prediction = tf.equal (Tf.argmax (tf_inputy, 1), Tf.argmax (pred Iction, 1) accuracy = Tf.reduce_mean (Tf.cast (correct_prediction, Tf.float32)) ' #创建会话 with TF.   Session () as Sess:sess.run (init) #初始化变量 to I in range: #控制迭代次数 # starts to optimize its  
           
            Reduce error start training Sess.run (Train_step, Feed_dict={tf_inputx:train_datax, Tf_inputy:train_datay}) # View current Training real-time data feedback print ("Number:" +str (i)) print (Sess.run, Feed_dict={tf_inputx:train_da Tax, Tf_inputy:train_datay})) # acc = sess.run (accuracy, feed_dict={tf_inputx:test_datax, Tf_inputy:test_da Tay}) # print ("Iter" + str (i) + ", testing accuracy" + STR (ACC)) #训练完成后 forecast View forecast results print ("test ") Print (Sess.run (Prediction,feed_dict={tf_inputx:test_datax}))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.