tflearn学习1

2019-04-28 17:38:49 浏览数 (1)

一.安装

pip 下载tensorflow和tflearn

二.泰坦尼克号船员分类

  1. 创建py文件(我这里是titanic.py)
代码语言:javascript复制
import numpy as np
import tflearn

# 下载 Titanic 数据并保存
from tflearn.datasets import titanic
titanic.download_dataset('titanic_dataset.csv')

# 加载 CSV 文件
from tflearn.data_utils import load_csv
data, labels = load_csv('titanic_dataset.csv', target_column=0,
                        categorical_labels=True, n_classes=2)

# 数据预处理函数,男性为1,女性为0。
def preprocess(data, columns_to_ignore):
    # Sort by descending id and delete columns
    for id in sorted(columns_to_ignore, reverse=True):
        [r.pop(id) for r in data]
    for i in range(len(data)):
      # Converting 'sex' field to float (id is 1 after removing labels column)
      data[i][1] = 1. if data[i][1] == 'female' else 0.
    return np.array(data, dtype=np.float32)

# 忽略姓名和票号 (id 1 & 6 )
to_ignore=[1, 6]

# 数据预处理
data = preprocess(data, to_ignore)

# 构建三层神经网络
net = tflearn.input_data(shape=[None, 6])
net = tflearn.fully_connected(net, 32)
net = tflearn.fully_connected(net, 32)
net = tflearn.fully_connected(net, 2, activation='softmax')
net = tflearn.regression(net)

# 选择 model
model = tflearn.DNN(net)
# 开始训练 (apply gradient descent algorithm)
model.fit(data, labels, n_epoch=10, batch_size=16, show_metric=True)

# 判断主角生还率 DiCaprio and Winslet
dicaprio = [3, 'Jack Dawson', 'male', 19, 0, 0, 'N/A', 5.0000]
winslet = [1, 'Rose DeWitt Bukater', 'female', 17, 1, 2, 'N/A', 100.0000]
# Preprocess data
dicaprio, winslet = preprocess([dicaprio, winslet], to_ignore)
# Predict surviving chances (class 1 results)
pred = model.predict([dicaprio, winslet])
print("DiCaprio Surviving Rate:", pred[0][1])
print("Winslet Surviving Rate:", pred[1][1])

输出结果:

0 人点赞