mysql 回归分析,线性回归,在MySQL中寻找斜率

博主正在尝试为具有DATETIME作为x轴和数值作为y轴的数据集计算斜率。他们在Excel中得到的结果与使用SQL查询得出的斜率相差甚远。尝试了多种方法,包括不同的SQL查询公式,但结果不是过小就是不同。问题在于如何正确转换时间值以匹配期望的单位,可能是秒、小时或天。寻求帮助以确定最准确的计算方法。
摘要由CSDN通过智能技术生成

I'm trying to find the slope of a dataset that has DATETIME as the x axis, and a number on the y axis.

I've tried the a number of approaches, and nothing will match the slope of the line when I plug the data into Excel, it's off by multiple orders of magnitude.

This is what I have right now, but it's giving me a slope of -1.13e-13 instead of -0.008

SELECT (SUM((x-xBar)*(y-yBar)))/(SUM((x-xBar))*SUM((x-xBar)))) as slope

from (select unix_timestamp(date) as x,

(select avg(unix_timestamp(date)) from datatable) as xBar,

value as y,

(select avg(value) from datatable) as yBar from datatable) as d;

Any help would be greatly appreciated, thanks.

Update:

I've also tried

SELECT effortId,

( COUNT(*)*SUM(unix_timestamp(date)*value) -SUM(unix_timestamp(date))*SUM(value) ) /

(COUNT(*)*SUM(unix_timestamp(date)^2)-SUM(unix_timestamp(date))^2) AS Slope

FROM datatable;

and get a completely different answer (-0.0019), is this more accurate? Anyone know?

解决方案

What are the X units? You'll probably have to convert the time values explicitly to what you expect, whether it is seconds, hours, or days.

要使用MySQL数据库进行线性回归预测模型,可以按照以下步骤进行操作: 1. 首先,导入所需的库。在Python,可以使用pandas库连接和操作MySQL数据库,使用sklearn库进行数据建模和预测。例如,使用import pandas as pd导入pandas库。 2. 连接到MySQL数据库。可以使用pandas库的read_sql_query函数来执行SQL语句,并将数据库的数据读取到DataFrame对象。例如,使用df = pd.read_sql_query('SELECT * FROM table', connection)连接到数据库并将表的数据读取到DataFrame。 3. 数据预处理。对于线性回归模型,需要进行数据清洗和准备。可以使用pandas库对数据进行处理,例如删除空值、处理异常值等。 4. 拆分数据集。将数据集分为训练集和测试集,以便建立和评估模型的性能。可以使用sklearn库的train_test_split函数来拆分数据集。 5. 创建线性回归模型。使用sklearn库的LinearRegression函数创建线性回归模型。例如,使用regr = LinearRegression()创建模型对象。 6. 训练模型。使用模型对象的fit方法来训练模型。例如,使用regr.fit(X_train, y_train)来训练模型,其X_train是训练集的属性数据,y_train是其对应的目标值。 7. 进行预测。使用模型对象的predict方法对测试集进行预测。例如,使用y_pred = regr.predict(X_test)来预测测试集的目标值。 8. 评估模型性能。使用评估指标(如均方误差、决定系数等)来评估模型的性能。可以使用sklearn库的相关函数来计算这些指标。 请注意,上述步骤仅为一般性的示例,实际操作可能因具体场景和数据而有所不同。具体的代码实现还需根据具体情况进行调整。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [[Python从零到壹] 十二.机器学习之回归分析万字总结全网首发(线性回归、多项式回归、逻辑回归)](https://blog.csdn.net/Eastmount/article/details/118435877)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [【数据挖掘】Python提取MySQL数据进行线性回归全流程](https://blog.csdn.net/qq_43018832/article/details/128356461)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值