我正在尝试此代码段.我正在使用scikits.learn 0.8.1
from scikits.learn import linear_model
import numpy as np
num_rows = 10000
X = np.zeros([num_rows,2])
y = np.zeros([num_rows,1])
# assume here I have filled in X and y appropriately with 0s and 1s from the dataset
clf = linear_model.LogisticRegression()
clf.fit(X, y)
Run Code Online (Sandbox Code Playgroud)
我得到了这个 - >
/usr/local/lib/python2.6/dist-packages/scikits/learn/svm/liblinear.so in scikits.learn.svm.liblinear.train_wrap (scikits/learn/svm/liblinear.c:992)()
ValueError: Buffer has wrong number of dimensions (expected 1, got 2)
Run Code Online (Sandbox Code Playgroud)
这里有什么问题?
我想强制模型中的一个系数小于或等于零.
nnls包允许您设置所有系数等于或大于零,但我不知道如何指定特定系数小于零.
任何帮助将不胜感激.
我在R中运行逻辑回归并进行"反向消除"以获得我的最终模型:
FulMod2 <- glm(surv~as.factor(tdate)+as.factor(tdate)+as.factor(sline)+as.factor(pgf)
+as.factor(weight5)+as.factor(backfat5)+as.factor(srect2)
+as.factor(bcs)+as.factor(loco3)+as.factor(fear3)
+as.factor(teats)+as.factor(preudder)+as.factor(postudder)
+as.factor(colos)+as.factor(tb5) +as.factor(respon3)
+as.factor(feed5)+as.factor(bwt5)+as.factor(sex)
+as.factor(fos2)+as.factor(gest3)+as.factor(int3),
family=binomial(link="logit"),data=sof)
Run Code Online (Sandbox Code Playgroud)
尝试运行向后消除脚本时:
step(FulMod2,direction="backward",trace=FALSE)
Run Code Online (Sandbox Code Playgroud)
我收到此错误消息:
Error in step(FulMod2, direction = "backward", trace = FALSE) :
number of rows in use has changed: remove missing values?
Run Code Online (Sandbox Code Playgroud)
这是我使用向后消除功能运行的第二个模型.当我做后退消除以获得我的最终模型时,第一个模型很好.
任何帮助将非常感谢!
巴兹
这是一个关于ngrams线性回归的问题,使用Tf-IDF(术语频率 - 逆文档频率).为此,我使用numpy稀疏矩阵和sklearn进行线性回归.
使用unigrams时我有53个病例和超过6000个功能.预测基于使用LeaveOneOut的交叉验证.
当我创建一个只有unigram分数的tf-idf稀疏矩阵时,我得到的预测比我创建unigram + bigram分数的tf-idf稀疏矩阵要好一些.我添加到矩阵的列越多(三元组,四元组,五元组等的列),回归预测的准确性就越低.
这是常见的吗?这怎么可能?我会认为功能越多越好.
我希望这不是一个天真的问题.我caret
在R 中的包中使用不同的模型执行一系列二项式回归.除了地球(MARS)之外,所有这些都是有效的.通常,earth
系列通过glm
函数传递给earth
函数glm=list(family=binomial)
.这似乎工作正常(如下所示).对于一般predict()
功能,我会使用它type="response'
来正确地缩放预测.以下示例显示了fit1
使用正确预测的非插入符方法pred1
. pred1a
是没有的不正确的缩放预测type='response'
. fit2
与该方法caret
和pred2
是预测; 它与非缩放预测相同pred1a
.通过fit2
对象挖掘,glm.list
组件中存在正确拟合的值.因此,该earth()
函数表现得如此.
问题是......因为caret
prediction()
函数只需要type='prob' or 'raw'
,我如何指示是根据响应的规模进行预测?
非常感谢你.
require(earth)
library(caret)
data(mtcars)
fit1 <- earth(am ~ cyl + mpg + wt + disp, data = mtcars,
degree=1, glm=list(family=binomial))
pred1 <- predict(fit1, newdata = mtcars, type="response")
range(pred1)
[1] 0.0004665284 0.9979135993 # Correct …
Run Code Online (Sandbox Code Playgroud) 以下代码mtcars
使用for循环或lapply 将4个不同的模型公式拟合到数据集.在这两种情况下,存储在结果该公式被称为formulas[[1]]
,formulas[[2]]
等等,而不是人类可读的公式.
formulas <- list(
mpg ~ disp,
mpg ~ I(1 / disp),
mpg ~ disp + wt,
mpg ~ I(1 / disp) + wt
)
res <- vector("list", length=length(formulas))
for (i in seq_along(formulas)) {
res[[i]] <- lm(formulas[[i]], data=mtcars)
}
res
lapply(formulas, lm, data=mtcars)
Run Code Online (Sandbox Code Playgroud)
有没有办法让结果中显示完整,可读的公式?
我正在研究大数据,我想找到重要的功能.因为我是一名生物学家,所以请原谅我缺乏的知识.
我的数据集有大约5000个属性和500个样本,它们具有二进制类0和1.此外,数据集有偏差 - 样本大约400 0和100 1.我想找到一些在决定课程时影响最大的特征.
A1 A2 A3 ... Gn Class
S1 1.0 0.8 -0.1 ... 1.0 0
S2 0.8 0.4 0.9 ... 1.0 0
S3 -1.0 -0.5 -0.8 ... 1.0 1
...
Run Code Online (Sandbox Code Playgroud)
当我从前一个问题得到一些建议时,我试图找到属性系数高的重要特征,使用L1惩罚使用Lasso回归,因为它使得不重要特征的得分为0.
我正在使用scikit-learn库进行这项工作.
所以,我的问题是这样的.
我可以使用Lasso回归来实现有偏见的二元类吗?如果不是,使用Logistic回归是否是一个很好的解决方案,尽管它不使用L1惩罚?
如何使用LassoCV找到alpha的最佳值?该文件称LassoCV支持它,但我找不到该功能.
这种分类还有其他好的方法吗?
非常感谢你.
python regression classification machine-learning scikit-learn
我看到了如何用pymc3进行变点分析的例子,但似乎我错过了一些东西,因为我得到的结果远非真正的值.这是一个玩具的例子.
数据:
脚本:
from pymc3 import *
from numpy.random import uniform, normal
bp_u = 30 #switch point
c_u = [1, -1] #intercepts before and after switch point
beta_u = [0, -0.02] #slopes before & after switch point
x = uniform(0,90, 200)
y = (x < bp_u)*(c_u[0]+beta_u[0]*x) + (x >= bp_u)*(c_u[1]+beta_u[1]*x) + normal(0,0.1,200)
with Model() as sw_model:
sigma = HalfCauchy('sigma', beta=10, testval=1.)
switchpoint = Uniform('switchpoint', lower=x.min(), upper=x.max(), testval=45)
# Priors for pre- and post-switch intercepts and slopes
intercept_u1 = Uniform('Intercept_u1', lower=-10, …
Run Code Online (Sandbox Code Playgroud) 为什么我有完全相同的模型,但运行不同网格大小的预测(0.001对比0.01)获得不同的预测?
set.seed(0)
n_data=2000
x=runif(n_data)-0.5
y=0.1*sin(x*30)/x+runif(n_data)
plot(x,y)
poly_df=5
x_exp=as.data.frame(cbind(y,poly(x, poly_df)))
fit=lm(y~.,data=x_exp)
x_plt1=seq(-1,1,0.001)
x_plt_exp1=as.data.frame(poly(x_plt1,poly_df))
lines(x_plt1,predict(fit,x_plt_exp1),lwd=3,col=2)
x_plt2=seq(-1,1,0.01)
x_plt_exp2=as.data.frame(poly(x_plt2,poly_df))
lines(x_plt2,predict(fit,x_plt_exp2),lwd=3,col=3)
Run Code Online (Sandbox Code Playgroud)
我正在尝试制作和测试线性模型如下:
lm_model <- lm(Purchase ~., data = train)
lm_prediction <- predict(lm_model, test)
Run Code Online (Sandbox Code Playgroud)
这会导致以下错误,表明该Product_Category_1
列具有test
数据框中存在的值,但不存在train
数据框中的值):
因子Product_Category_1具有新的级别7,9,14,16,17,18
但是,如果我检查这些,他们肯定会出现在两个数据框中:
> nrow(subset(train, Product_Category_1 == "7"))
[1] 2923
> nrow(subset(test, Product_Category_1 == "7"))
[1] 745
> nrow(subset(train, Product_Category_1 == "9"))
[1] 312
> nrow(subset(test, Product_Category_1 == "9"))
[1] 92
Run Code Online (Sandbox Code Playgroud)
同时显示表格train
并test
显示它们具有相同的因素:
> table(train$Product_Category_1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
110820 18818 15820 9265 118955 16159 …
Run Code Online (Sandbox Code Playgroud) regression ×10
r ×6
lm ×3
python ×3
scikit-learn ×3
lapply ×1
nlp ×1
numpy ×1
polynomials ×1
prediction ×1
pymc ×1
pymc3 ×1
r-caret ×1
scikits ×1
tf-idf ×1