标签: linear-regression

python中的分段线性回归

python中有一个库可以进行分段线性回归吗?我想自动为我的数据添加多行来得到这样的东西: 分段回归

顺便说一句.我知道细分的数量.

python linear-regression

6
推荐指数
1
解决办法
3008
查看次数

从数据集SQL创建趋势线

下面的代码返回已解决的票证数量以及一段时间(期间为YYYY,WW)的特定天数的已打开票证数量.例如,如果@NoOfDays为7:

解决了| 打开| 一周| 一年| 期

56 | 30 | 13 | 2012 | 2012年,13

237 | 222 | 14 | 2012 | 2012年,14

"已解决"和"已打开"在时间段(x)上的行(y)上绘制.我想添加另一个列'趋势',它将返回一个数字,当绘制一段时间时,将成为趋势线(简单线性回归).确实希望将这两组值用作趋势的一个数据源.

这是我的代码:

SELECT a.resolved, b.opened, a.weekClosed AS week, a.yearClosed AS year,
    CAST(a.yearClosed as varchar(5)) + ', ' + CAST(a.weekClosed as varchar(5)) AS period
FROM 
    (SELECT TOP (100) PERCENT COUNT(DISTINCT TicketNbr) AS resolved, { fn WEEK(date_closed) } AS weekClosed, { fn YEAR(date_closed) } AS yearClosed
    FROM v_rpt_Service
    WHERE (date_closed >= DateAdd(Day, DateDiff(Day, 0, GetDate()) …
Run Code Online (Sandbox Code Playgroud)

sql math linear-regression visual-studio

6
推荐指数
2
解决办法
5912
查看次数

用于求解线性回归的梯度下降和正规方程方法给出了不同的解

我正在研究机器学习问题,并希望使用线性回归作为学习算法.我已经实现了两种不同的方法来查找theta线性回归模型的参数:梯度(最陡)下降和法线方程.在相同的数据上,它们都应该给出近似相等的theta向量.但他们没有.

两个theta向量在所有元素上非常相似,但第一个.这是用于将添加到数据中的所有1的向量相乘的那个.

这是thetas的样子(第一列是Gradient下降的输出,是Normal方程的第二输出):

Grad desc Norm eq
-237.7752 -4.6736
-5.8471   -5.8467
9.9174    9.9178
2.1135    2.1134
-1.5001   -1.5003
-37.8558  -37.8505
-1.1024   -1.1116
-19.2969  -19.2956
66.6423   66.6447
297.3666  296.7604
-741.9281 -744.1541
296.4649  296.3494
146.0304  144.4158
-2.9978   -2.9976
-0.8190   -0.8189
Run Code Online (Sandbox Code Playgroud)

有什么可以导致差异theta(1, 1)的梯度下降返回相比,theta(1, 1)通过正常的公式返回?我的代码中有错误吗?

这是我在Matlab中实现的正规方程:

function theta = normalEque(X, y)
    [m, n] = size(X);
    X = [ones(m, 1), X];
    theta = pinv(X'*X)*X'*y;
end
Run Code Online (Sandbox Code Playgroud)

这是梯度下降的代码:

function theta = gradientDesc(X, y)
    options = optimset('GradObj', …
Run Code Online (Sandbox Code Playgroud)

matlab machine-learning linear-regression gradient-descent

6
推荐指数
1
解决办法
2万
查看次数

postgres的线性回归

我使用Postgres,我有大量的行,每个站点有值和日期.(日期可以分开几天.)

id      | value | idstation | udate
--------+-------+-----------+-----
1       |  5    | 12        | 1984-02-11 00:00:00
2       |  7    | 12        | 1984-02-17 00:00:00
3       |  8    | 12        | 1984-02-21 00:00:00
4       |  9    | 12        | 1984-02-23 00:00:00
5       |  4    | 12        | 1984-02-24 00:00:00
6       |  8    | 12        | 1984-02-28 00:00:00
7       |  9    | 14        | 1984-02-21 00:00:00
8       |  15   | 15        | 1984-02-21 00:00:00
9       |  14   | 18 …
Run Code Online (Sandbox Code Playgroud)

postgresql datetime regression linear-regression

6
推荐指数
1
解决办法
7822
查看次数

在散点图中标记异常值

我已经绘制了这个图形来识别我的线性模型中的图形高杠杆点. 在此输入图像描述

鉴于我的模型使用的数据集的变量"NOMBRES",我试图绘制我的图形的所有点,但它变得难以辨认.这是我运行的代码:

> plot(hatvalues(tmodel),residuals(tmodel))
> text(hatvalues(tmodel),residuals(tmodel),labels=DSET$NOMBRES)
Run Code Online (Sandbox Code Playgroud)

因此,我想使用标签"DSET $ NOMBRES"绘制杠杆(帽值)高于0.05的点.

statistics plot analytics r linear-regression

6
推荐指数
1
解决办法
7763
查看次数

python线性回归按日期预测

我想用简单的线性回归预测未来某个日期的值,但我不能因为日期格式.

这是我的数据框:

data_df = 
date          value
2016-01-15    1555
2016-01-16    1678
2016-01-17    1789
...  

y = np.asarray(data_df['value'])
X = data_df[['date']]
X_train, X_test, y_train, y_test = train_test_split             
(X,y,train_size=.7,random_state=42)

model = LinearRegression() #create linear regression object
model.fit(X_train, y_train) #train model on train data
model.score(X_train, y_train) #check score

print (‘Coefficient: \n’, model.coef_)
print (‘Intercept: \n’, model.intercept_) 
coefs = zip(model.coef_, X.columns)
model.__dict__
print "sl = %.1f + " % model.intercept_ + \
     " + ".join("%.1f %s" % coef for coef in coefs) #linear model
Run Code Online (Sandbox Code Playgroud)

我试图将日期转换为失败 …

python date linear-regression pandas

6
推荐指数
2
解决办法
2万
查看次数

具有系数约束的线性回归

我正在尝试执行线性回归,对于这样的模型:

Y = aX1 + bX2 + c
Run Code Online (Sandbox Code Playgroud)

所以, Y ~ X1 + X2

假设我有以下响应向量:

set.seed(1)
Y <- runif(100, -1.0, 1.0)
Run Code Online (Sandbox Code Playgroud)

以下预测变量矩阵:

X1 <- runif(100, 0.4, 1.0)
X2 <- sample(rep(0:1,each=50))
X <- cbind(X1, X2)
Run Code Online (Sandbox Code Playgroud)

我想对系数使用以下约束:

a + c >= 0  
c >= 0
Run Code Online (Sandbox Code Playgroud)

所以对b没有约束.

我知道glmc包可以用来应用约束,但是我无法确定如何将它应用于我的约束.我也知道可以使用contr.sum,例如,所有系数总和为0,但这不是我想要做的.solve.QP()似乎是另一种可能性,meq=0可以使用设置使所有系数> = 0(同样,这里不是我的目标).

注意:解决方案必须能够处理响应向量Y中的NA值,例如:

Y <- runif(100, -1.0, 1.0)
Y[c(2,5,17,56,37,56,34,78)] <- NA
Run Code Online (Sandbox Code Playgroud)

r linear-regression quadratic-programming

6
推荐指数
1
解决办法
4353
查看次数

如何计算多项式线性回归中的误差?

我正在尝试计算我正在使用的训练数据的错误率.

我相信我错误地计算错误.公式如下:在此输入图像描述

y 计算如下:

在此输入图像描述

fitPoly(M)在线上的函数中计算这个49.我相信我的计算不正确y(x(n)),但我不知道还能做什么.


以下是最小,完整和可验证的示例.

import numpy as np
import matplotlib.pyplot as plt

dataTrain = [[2.362761180904257019e-01, -4.108125266714775847e+00],
[4.324296163702689988e-01,  -9.869308732049049127e+00],
[6.023323504115264404e-01,  -6.684279243433971729e+00],
[3.305079685397107614e-01,  -7.897042003779912278e+00],
[9.952423271981121200e-01,  3.710086310489402628e+00],
[8.308127402955634011e-02,  1.828266768673480147e+00],
[1.855495407116576345e-01,  1.039713135916495501e+00],
[7.088332047815845138e-01,  -9.783208407540947560e-01],
[9.475723071629885697e-01,  1.137746192425550085e+01],
[2.343475721257285427e-01,  3.098019704040922750e+00],
[9.338350584099475160e-02,  2.316408265530458976e+00],
[2.107903139601833287e-01,  -1.550451474833406396e+00],
[9.509966727520677843e-01,  9.295029459100994984e+00],
[7.164931165416982273e-01,  1.041025972594300075e+00],
[2.965557300301902011e-03,  -1.060607693351102121e+01]]

def strip(L, xt):
    ret = []
    for i in L:
        ret.append(i[xt])
    return ret

x1 = strip(dataTrain, 0)
y1 = strip(dataTrain, 1)

# HELP HERE

def getY(m, w, D): …
Run Code Online (Sandbox Code Playgroud)

python numpy polynomial-math linear-regression

6
推荐指数
1
解决办法
1282
查看次数

为什么numpy.linalg.pinv()比numpy.linalg.inv()更喜欢在线性回归中创建矩阵的逆矩阵

如果我们想通过使用正规方程式搜索线性回归模型的最佳参数θ:

theta = inv(X ^ T*X)*X ^ T*y

一步是计算inv(X ^ T*X).因此numpy提供了np.linalg.inv()np.linalg.pinv()

虽然这会导致不同的结果:

X=np.matrix([[1,2104,5,1,45],[1,1416,3,2,40],[1,1534,3,2,30],[1,852,2,1,36]])
y=np.matrix([[460],[232],[315],[178]])

XT=X.T
XTX=XT@X

pinv=np.linalg.pinv(XTX)
theta_pinv=(pinv@XT)@y
print(theta_pinv)

[[188.40031946]
 [  0.3866255 ]
 [-56.13824955]
 [-92.9672536 ]
 [ -3.73781915]]

inv=np.linalg.inv(XTX)
theta_inv=(inv@XT)@y
print(theta_inv)

[[-648.7890625 ]
 [   0.79418945]
 [-110.09375   ]
 [ -74.0703125 ]
 [  -3.69091797]]
Run Code Online (Sandbox Code Playgroud)

第一个输出,即pinv的输出是正确的输出,另外在numpy.linalg.pinv()文档中推荐.但是为什么这个以及inv()和pinv()之间的差异/优点/缺点在哪里.

python numpy matrix linear-algebra linear-regression

6
推荐指数
2
解决办法
8820
查看次数

ValueError:预期的2D数组,而是1D数组:

在练习简单线性回归模型时,我遇到了这个错误,我认为我的数据集有问题。

这是我的数据集:

这是自变量X:

这是因变量Y:

这是X_train

这是Y_train

这是错误体:

ValueError: Expected 2D array, got 1D array instead:
array=[ 7.   8.4 10.1  6.5  6.9  7.9  5.8  7.4  9.3 10.3  7.3  8.1].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

import pandas as pd
import matplotlib as pt

#import data set

dataset = pd.read_csv('Sample-data-sets-for-linear-regression1.csv')
x = dataset.iloc[:, 1].values
y = dataset.iloc[:, 2].values

#Spliting the dataset into Training set …
Run Code Online (Sandbox Code Playgroud)

linear-regression python-3.x scikit-learn

6
推荐指数
4
解决办法
1万
查看次数