In [ ]:
import tflearn
from tflearn.datasets import titanic
titanic.download_dataset('titanic_dataset.csv')

from tflearn.data_utils import load_csv
data, labels = load_csv('titanic_dataset.csv', target_column=0,
                        categorical_labels=True, n_classes=2)

In [ ]:
# Preprocessing function
def preprocess(passengers, columns_to_delete):
    # Sort by descending id and delete columns
    for column_to_delete in sorted(columns_to_delete, reverse=True):
        [passenger.pop(column_to_delete) for passenger in passengers]
    for i in range(len(passengers)):
        # Converting 'sex' field to float (id is 1 after removing labels column)
        passengers[i][1] = 1. if passengers[i][1] == 'female' else 0.
    return np.array(passengers, dtype=np.float32)

In [ ]:
# Ignore 'name' and 'ticket' columns (id 1 & 6 of data array)
to_ignore=[1, 6]

# Preprocess data
data = preprocess(data, to_ignore)

In [ ]:
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)
# Define model
model = tflearn.DNN(net)
# Start training (apply gradient descent algorithm)
model.fit(data, labels, n_epoch=10, batch_size=16, show_metric=True)

In [ ]:
# Let's create some data for 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])

Toy Dataset 3 way


In [ ]:
# Make 10k examples
import tflearn
examples = 25000
data = pd.DataFrame(np.random.randn(examples,2), columns=['x1', 'x2'])

In [ ]:
labels = pd.DataFrame(np.where((data['x1'] + data['x2']) > 0, 1, 0), columns=['blue'])
labels['red'] = np.where(labels['blue'] == 1, 0, 1)
labels['green'] = np.where((data['x1'] > 1) & (data['x2'] > 1.5), 1, 0)
labels['yellow'] = np.where((data['x1']**2 + -data['x2']**3 - 4 > 0), 1, 0)
labels.loc[labels[labels['yellow'] == 1].index, ['blue', 'red', 'green']] = 0
labels.loc[labels[labels['green'] == 1].index, ['blue', 'red']] = 0

In [ ]:
# Check that 
labels.sum(axis=1).value_counts(dropna=False)

In [ ]:
df = pd.concat([data, labels], axis=1)

In [ ]:
colors = pd.Series(np.where(df['blue'] == 1, 1, np.where(df['red'] == 1, 0, np.where(df['green'] == 1, 2, 3))))

In [ ]:
fig, ax = plt.subplots()
colors_map = {1:'blue', 0:'red', 2:'green', 3:'yellow'}
ax.scatter(df['x1'], df['x2'], c=colors.apply(lambda x: colors_map[x]))
plt.show()

In [ ]:
from sklearn.model_selection import train_test_split

In [ ]:
train, test = train_test_split(df)
train_x, train_y = train[['x1', 'x2']].values, train[['blue', 'red', 'green', 'yellow']].values
test_x, test_y = test[['x1', 'x2']].values, test[['blue', 'red', 'green', 'yellow']].values

In [ ]:
net = tflearn.input_data(shape=[None, 2])
net = tflearn.fully_connected(net, 10)
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, 6)
net = tflearn.fully_connected(net, 6)
net = tflearn.fully_connected(net, 4, activation='softmax')
net = tflearn.regression(net, learning_rate=.0001)
# Define model
model = tflearn.DNN(net, tensorboard_verbose=3)
# Start training (apply gradient descent algorithm)
model.fit(train_x, train_y, n_epoch=60, batch_size=32, show_metric=True)

In [ ]: