python 线性回归编码
一,使用 scikit-learn 编码练习
# coding=utf-8
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
# import scikit-learn as sl
'''
数据分布
均匀分布
正态分布 高斯分布 直方图 散点图
线性回归: R平方 评估相关度 ,是否适合线性回归模型
'''
# plt.ion()
'''
distribution_arr = np.random.uniform(0.0, 5.0, 250)
plt.hist(distribution_arr, 5) # 数据分布,个数分布范围
plt.show()
normal_arr = np.random.normal(5.0, 1.0, 1000)
plt.hist(normal_arr, 100)
plt.show()
'''
'''
x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6]
y = [99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]
plt.scatter(x, y)
plt.show()
'''
# x = np.random.normal(5.0, 1.0, 1000)
# y = np.random.normal(10.0, 2.0, 1000)
x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6]
y = [99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]
slope, intercept, r, p, std_err = stats.linregress(x, y) # 调用了线性回归函数
def myfunc(x):
return slope * x + intercept
mymodel = list(map(myfunc, x))
plt.scatter(x, y)
plt.plot(x, mymodel)
plt.show()
---一------步-----一 ------个-----脚--------印----------
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异