TensorFlow: Linear Regression generate data
Jump to navigation
Jump to search
# Import libraries (Numpy, matplotlib)
import numpy as np
import matplotlib.pyplot as plt
# Create 1000 points following a function y=0.1 * x + 0.4 (i.e. y \= W * x + b) with some normal random distribution:
num_points = 1000
vectors_set = []
for i in range(num_points):
W = 0.1 # W
b = 0.4 # b
x1 = np.random.normal(0.0, 1.0)
nd = np.random.normal(0.0, 0.05)
y1 = W * x1 + b
# Add some impurity with some normal distribution -i.e. nd:
y1 = y1+nd
# Append them and create a combined vector set:
vectors_set.append([x1, y1])
# Separate the data point across axises:
x_data = [v[0] for v in vectors_set]
y_data = [v[1] for v in vectors_set]
# Plot and show the data points in a 2D space
plt.plot(x_data, y_data, 'r*', label='Original data')
plt.legend()
plt.show()