P142-144
python3可以運行
import tensorflow as tf
import numpy as np
import pandas as pd
train=pd.read_csv('breast-cancer-train.csv')
test=pd.read_csv('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()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))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 pltplt.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()
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔為你收集整理的P142-144使用Tensorflow自定义一个线性分类器用于对“良/恶性乳腺癌肿瘤”进行预测的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。