TF之LiR:利用TF自定义一个线性分类器LiR对乳腺癌肿瘤数据集进行二分类预测(良/恶性)

TF之LiR:利用TF自定义一个线性分类器LiR对乳腺癌肿瘤数据集进行二分类预测(良/恶性)


输出结果

设计思路

核心代码

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)  

sess = tf.Session()
sess.run(init)      

for step in range(0, 1000):
    sess.run(train)
    if step % 200 == 0:
        print(step, sess.run(W), sess.run(b))
(0)

相关推荐