-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregression_example_with_pandas.py
More file actions
44 lines (34 loc) · 1.23 KB
/
regression_example_with_pandas.py
File metadata and controls
44 lines (34 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
x_data = np.linspace(0.0, 10.0, 1000000)
noise = np.random.randn(len(x_data))
y_data = (.5 * x_data) + 5 + noise
x_df = pd.DataFrame(data=x_data, columns=["X"])
y_df = pd.DataFrame(data=y_data, columns=["Y"])
df = pd.concat([x_df, y_df], axis=1)
print(df.head())
plt.scatter(x_df.sample(n=750, random_state=101), y_df.sample(n=750, random_state=101))
plt.show()
batch_size = 8
m = tf.Variable(0.81)
b = tf.Variable(0.17)
x_ph = tf.placeholder(tf.float32, [batch_size])
y_ph = tf.placeholder(tf.float32, [batch_size])
y_true = (m * x_ph) + b
error = tf.reduce_sum(tf.square(y_ph - y_true))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
train = optimizer.minimize(error)
init = tf.global_variables_initializer()
with tf.Session() as sess:
batches = 1000
sess.run(init)
for i in range(batches):
random_index = np.random.randint(len(x_data), size=batch_size)
sess.run(train, feed_dict={x_ph: df["X"].iloc[random_index], y_ph: df["Y"].iloc[random_index]})
true_m, true_b = sess.run([m, b])
y_true_plot = (true_m * x_data) + true_b
plt.scatter(x_df, y_df)
plt.plot(x_data, y_true_plot, "red")
plt.show()