机器学习计算一元一次和二元一次方程的系数(sklearn和paddle)

已知一组数据x

array([[1],
       [2],
       [3],
       [4],
       [5],
       [6],
       [7],
       [8],
       [9]])

对应y

array([[10],
       [20],
       [30],
       [40],
       [50],
       [60],
       [70],
       [80],
       [90]])
求系数a

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
from sklearn.externals import joblib

import numpy as np
# ax=y 求a
x = np.arange(1, 10)
x = x.reshape((9, 1))
y = np.arange(10, 100, 10)
y = y.reshape((9, 1))
# 数据划分
x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=22)


estimator = LinearRegression()  # 线性回归(正规方程)
estimator.fit(x_train, y_train)

# 模型保存
# joblib.dump(estimator, './test.pkl')
# 模型加载
# estimator = joblib.load('./test.pkl')

y_predict = estimator.predict(x_test)
print(y_predict[:3])
print(y_test[:3])
print("系数:", estimator.coef_)
print('偏置:', estimator.intercept_)

error = mean_squared_error(y_test, y_predict)
print("误差为:", error)

输出:

[[90.]
 [20.]
 [40.]]
[[90]
 [20]
 [40]]
系数: [[10.]]
偏置: [-7.10542736e-15]
误差为: 4.2072581611787294e-30

Process finished with exit code 0

利用paddle计算2元一次方程的系数,10条数据,迭代20000次可以计算正确

import paddle.fluid as fluid
import paddle
import numpy as np
import os
import matp

你可能感兴趣的:(python,python,线性回归)