使用tensorflow 手动搭建线性分类器 对良/恶性乳腺癌肿瘤进行分类

使用tensorflow  手动搭建线性分类器 对良/恶性乳腺癌肿瘤进行分类

# -*- coding:utf-8 -*-

import tensorflow as tf
import numpy as np
import pandas as pd
if __name__ == '__main__':
    print "hello"
    train = pd.read_csv('./Breast-Cancer/breast-cancer-train.csv')
    test = pd.read_csv('./Breast-Cancer/breast-cancer-test.csv')

    X_train = np.float32(train[['Clump Thickness', 'Cell Size']].T)
    y_train = np.float32(train['Type'].T)
    X_test = np.float32(test[['Clump Thickness', 'Cell Size']].T)
    y_test = np.float32(test['Type'].T)

    b = tf.Variable(tf.zeros([1]))
    W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0))
    y = tf.matmul(W, X_train) + b

    # 最小化方差
    loss = tf.reduce_mean(tf.square(y - y_train))
    optimizer = tf.train.GradientDescentOptimizer(0.01)
    train = optimizer.minimize(loss)

    # 初始化变量
    init = tf.initialize_all_variables()

    # 启动图 (graph)
    sess = tf.Session()
    sess.run(init)

    # 拟合平面
    for step in xrange(0, 1000):
        sess.run(train)
        if step % 200 == 0:
            print step, sess.run(W), sess.run(b)

    test_negative = test.loc[test['Type'] == 0][['Clump Thickness', 'Cell Size']]
    test_positive = test.loc[test['Type'] == 1][['Clump Thickness', 'Cell Size']]

    import matplotlib.pyplot as plt

    plt.scatter(test_negative['Clump Thickness'], test_negative['Cell Size'], marker='o', s=200, c='red')
    plt.scatter(test_positive['Clump Thickness'], test_positive['Cell Size'], marker='x', s=150, c='black')

    plt.xlabel('Clump Thickness')
    plt.ylabel('Cell Size')

    lx = np.arange(0, 12)
    ly = (0.5 - sess.run(b) - lx * sess.run(W)[0][0]) / sess.run(W)[0][1]

    plt.plot(lx, ly, color='green')
    plt.show()

猜你喜欢

转载自blog.csdn.net/u011243684/article/details/84968251
今日推荐