逻辑回归练习

题目

The task is to determine whether a tumor will be benign (harmless) or malignant (harmful) based on leukocyte (white blood cells) count and blood pressure. Note that this is a synethic dataset that has no clinical relevance.

Dataset file: tumors.csv

Requirements:

Write your steps and code in a jupyter notebook(.ipynb file).

After run the notebook succesfully.

Print or Save the notebook as PDF file.

Submit the .ipynb file and pdf file.

环境

[https://openbayes.com/]使用jupyter notebook编写。

代码

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
%matplotlib inline
import tensorflow as tf
from tensorflow import keras
import pandas as pd
//读取数据
data = pd.read_csv('tumors.csv')
x = data.iloc[:,[0,1]]
y = data.iloc[:,[-1]].replace(['malignant','benign'], [0,1])
//建立神经网络并训练
model = keras.Sequential([
    keras.layers.Dense(1, input_dim=2, activation='sigmoid')
])
model.compile(optimizer='sgd', loss='binary_crossentropy', metrics=['acc'])
model.fit(x, y, epochs=500)
//结果可视化
print('
Testing ------------')
cost = model.evaluate(x, y)
print('test cost:', cost)
W, b = model.layers[0].get_weights()
print('Weights=', W[0], '
biases=', b)

# 将训练结果绘出
Y_pred = model.predict(x)
Y_pred = (Y_pred*2).astype(np.int32)  # 将概率转化为类标号,概率在0-0.5时,转为0,概率在0.5-1时转为1
y = y.astype(np.int32)
# 绘制散点图 参数:x横轴 y纵轴
plt.subplot(2,1,1).scatter(x.values[:,0], x.values[:,1], c=Y_pred)
plt.subplot(2,1,2).scatter(x.values[:,0], x.values[:,1], c=y.values)
plt.show()

.ipynb转为.pdf

参考[https://blog.csdn.net/bing_bing_bing_/article/details/88732012]

  • 下载miktex
  • ipynb转为tex
  • tex转为pdf

小结

初步学习了keras。后面的练习,希望使用pytorch。

原文地址:https://www.cnblogs.com/chenshaowei/p/12829834.html