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

这篇博客通过已知数据x和y,运用paddle库计算2元一次方程的系数。以9组数据为例,展示了如何通过线性回归模型进行迭代学习,得出准确的方程系数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

已知一组数据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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值