- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试进行多变量线性回归。但我发现 sklearn.linear_model 工作起来很奇怪。这是我的代码:
import numpy as np
from sklearn import linear_model
b = np.array([3,5,7]).transpose() ## the right answer I am expecting
x = np.array([[1,6,9], ## 1*3 + 6*5 + 7*9 = 96
[2,7,7], ## 2*3 + 7*5 + 7*7 = 90
[3,4,5]]) ## 3*3 + 4*5 + 5*7 = 64
y = np.array([96,90,64]).transpose()
clf = linear_model.LinearRegression()
clf.fit([[1,6,9],
[2,7,7],
[3,4,5]], [96,90,64])
print clf.coef_ ## <== it gives me [-2.2 5 4.4] NOT [3, 5, 7]
print np.dot(x, clf.coef_) ## <== it gives me [ 67.4 61.4 35.4]
最佳答案
为了找到您的初始系数,您需要在构建线性回归时使用关键字 fit_intercept=False
。
import numpy as np
from sklearn import linear_model
b = np.array([3,5,7])
x = np.array([[1,6,9],
[2,7,7],
[3,4,5]])
y = np.array([96,90,64])
clf = linear_model.LinearRegression(fit_intercept=False)
clf.fit(x, y)
print clf.coef_
print np.dot(x, clf.coef_)
使用 fit_intercept=False
可以防止 LinearRegression
对象与 x - x.mean(axis=0)
一起工作,否则它会做(并使用常量偏移量 y = xb + c
捕获平均值)- 或者等效地通过将 1
的列添加到 x
。
附带说明一下,在一维数组上调用 transpose
没有任何效果(它颠倒了轴的顺序,而你只有一个)。
关于Python:Sklearn.linear_model.LinearRegression 工作异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24393518/
我需要限制以下命令的 CPU 使用率,因为它使用了 100% 的 CPU。 from sklearn.linear_model import LinearRegression mode
如果在 sklearn.linear_model 中的任何线性模型中将归一化参数设置为 True,是否会在评分步骤中应用归一化? 例如: from sklearn import linear_mode
scikit-learn具有两个逻辑回归函数: sklearn.linear_model.LogisticRegression sklearn.linear_model.LogisticRegress
我正在尝试运行此回归代码。 import matplotlib import matplotlib.pyplot as plt import numpy as np import pandas as
我正在使用 sklearn.linear_model 和模块 LinearRegression 执行一个简单的自回归模型。你可以在下面看到我的代码。训练数据集位于 train 中,X 包含目标的滞后版
我知道我可能会将损失函数更改为以下之一: loss : str, 'hinge' or 'log' or 'modified_huber' The loss function to be used.
我对 ML 和 sklearn 比较陌生,我正在尝试使用具有不同值的 linear_model.Lasso 为具有 6 个不同特征的输入数据训练线性模型正则化参数。鉴于 X 和 y 是我的模型输入参数
这是什么意思?它在我使用 sklearn.linear_model.LassoLarsIC 时弹出:“提前停止 lars 路径,因为残差很小并且 alpha 的当前值不再得到很好的控制” 最佳答案 s
我认为下面的 Lasso 问题有简单的解决方案 [1,2,0,0] 因为 np.dot(x, [1,2,0,0]) 返回 [[5,7,6,8]]。 clf = linear_model.Lasso(a
scikit-learn 的 sklearn.linear_model.LogisticRegression 类如何处理回归和分类问题? 如 Wikipedia page 上所示以及许多来源,由于逻辑
我正在尝试进行多变量线性回归。但我发现 sklearn.linear_model 工作起来很奇怪。这是我的代码: import numpy as np from sklearn import line
在 OLS 形式的 StatsModels 中,results.summary 显示回归结果的汇总(例如 AIC、BIC、R 平方、...) 有什么办法可以在 sklearn.linear_model
我对 sklearn.linear_model 中 RidgeCV 中 normalized= 的具体作用感到困惑。 文档说: normalize : bool, default=False This
我正在研究多类分类(10 类)。我正在使用 sklearn.linear_model.SGDClassifier。我发现这个模型使用了一对一的方法。 SGDClassifier 有一个参数 class
我使用“statsmodels.regression.linear_model”来做 WLS。 但我不知道如何为我的回归赋予权重。 有谁知道权重是如何给出的以及它是如何工作的? import nump
我有以下使用 linear_model.Lasso 的代码: X_train, X_test, y_train, y_test = cross_validation.train_test_split(
是否可以根据在不同实现(例如 Java)中计算出的现有系数创建这样的实例? 我尝试创建一个实例,然后直接设置 coef_ 和 intercept_,这似乎可行,但我不确定这里是否有缺点,或者我是否会破
我正在尝试加载保存为 .joblib 的模型文件。我试过 pickle , sklearn.externals.joblib和 joblib本身。都是一样的错误。下面是我正在尝试做的一个例子。 cl
我想使用pickle模块,序列化我电脑上学习到的模型: pickle.dump(clf, open(os.path.join(dest, 'classifier.pkl'), 'wb'), proto
我想使用pickle模块,序列化我电脑上学习到的模型: pickle.dump(clf, open(os.path.join(dest, 'classifier.pkl'), 'wb'), proto
我是一名优秀的程序员,十分优秀!