concept one - dimensional logistic regression in category machine learning
appears as: one-dimensional logistic regression

This is an excerpt from Manning's book Machine Learning with TensorFlow, Second Edition.
Listing 5.5 Using one-dimensional logistic regression
import numpy as np #A import tensorflow as tf #A import matplotlib.pyplot as plt #A learning_rate = 0.01 #B training_epochs = 1000 #B def sigmoid(x): #C return 1. / (1. + np.exp(-x)) #C x1 = np.random.normal(-4, 2, 1000) #D x2 = np.random.normal(4, 2, 1000) #D xs = np.append(x1, x2) #D ys = np.asarray([0.] * len(x1) + [1.] * len(x2)) #D plt.scatter(xs, ys) #E X = tf.placeholder(tf.float32, shape=(None,), name="x") #F Y = tf.placeholder(tf.float32, shape=(None,), name="y") #F w = tf.Variable([0., 0.], name="parameter", trainable=True) #G y_model = tf.sigmoid(w[1] * X + w[0]) #H cost = tf.reduce_mean(-Y * tf.log(y_model) - (1 - Y) * tf.log(1 – y_model)) #I train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) #J with tf.Session() as sess: #K sess.run(tf.global_variables_initializer()) #K prev_err = 0 #L for epoch in range(training_epochs): #M err, _ = sess.run([cost, train_op], {X: xs, Y: ys}) #N print(epoch, err) if abs(prev_err - err) < 0.0001: #O break prev_err = err #P w_val = sess.run(w, {X: xs, Y: ys}) #Q all_xs = np.linspace(-10, 10, 100) #R plt.plot(all_xs, sigmoid((all_xs * w_val[1] + w_val[0]))) #R plt.show() #R