用的是机器学习的LinearRegression,使用了numpy工具,不是tensorflow
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
def f1():
np.random.seed(28)
"""
产生N条数据,每条数据有一个特征属性,目标属性和特征属性之间的关系为线性关系
"""
N = 100
x = np.linspace(0, 6, N) + np.random.normal(0, 2.0, N)
y = 14 * x + 7 + np.random.normal(0, 5.0, N)
x.shape = -1, 1
y.shape = -1, 1
print((np.shape(x), np.shape(y)))
algo = LinearRegression()
algo.fit(x, y)
predict = algo.predict(x)
plt.plot(x, y, 'ro')
plt.plot(x, predict, 'g-')
plt.show()
print(x)
print(y)
def f2():
np.random.seed(28)
"""
产生N条数据,每条数据有dim个特征属性,目标属性和特征属性之间的关系为线性关系
"""
N = 100
dim = 3
x = np.random.uniform(low=-10, high=10, size=(N, dim))
y = np.dot(x, [[5], [-6], [1.5]]) + 12 + np.random.normal(0, 5.0, (N, 1))
x.shape = -1, dim
y.shape = -1, 1
print((np.shape(x), np.shape(y)))
algo = LinearRegression()
algo.fit(x, y)
predict = algo.predict(x)
if __name__ == '__main__':
f1()