我只是举了一个例子,它产生了四个与该layout
功能相结合的图.但是,我无法弄清楚内部矩阵如何layout()
连接到这些图的布局.
layout(matrix(c(1, 1, 1,
2, 3, 4,
2, 3, 4), nr=3, byrow=T))
hist(rnorm(25), col="VioletRed")
hist(rnorm(25), col="VioletRed")
hist(rnorm(25), col="VioletRed")
hist(rnorm(25), col="VioletRed")
Run Code Online (Sandbox Code Playgroud) 传统上,sliderInput函数是
sliderInput(inputId, label, min, max, value, step = NULL, round = FALSE, format = NULL,
locale = NULL, ticks = TRUE, animate = FALSE, width = NULL, sep = ",",
pre = NULL, post = NULL, timeFormat = NULL, timezone = NULL, dragRange = TRUE)
Run Code Online (Sandbox Code Playgroud)
如果我指定
sliderInput("range","range",min=-3,max=3,value=c(-1,1))
Run Code Online (Sandbox Code Playgroud)
它输入一个从-1到1的范围.但是,有一个函数采用参数格式为days_included = c(-2,-1,0,1,2).在这种情况下,如果我设置,我不能使这个功能工作
sliderInput("range","range",min=-3,max=3,value=c(-2,2))
Run Code Online (Sandbox Code Playgroud)
因为它与所需的格式不匹配.所以这是我的问题,无论如何要使这个功能起作用?我在sliderInput中尝试了"step"参数但是失败了.
为了加速R包中的某些功能,我使用Rcpp在cpp函数中重新编码它们,并成功地将这些cpp函数嵌入到这个包中.下一步是测试cpp函数是否可以输出与R中的原始函数相同的结果.因此编写测试是必要的.
但是,我坚持这一步.我已经阅读了Hadley Wickham的测试,R包的一些链接
我所做的是我运行devtools::use_testthat()
创建一个tests/testthat目录.然后,运行use_catch(dir = getwd())
以添加测试文件tests/testthat/test-cpp.R.在这一点上,我认为expect_cpp_tests_pass()
可能会奏效,但只是坚持下去.如果我有原始函数调用add_inflow
和add_inflow_Cpp
.如何测试这两个函数是否相等?
通常, ann.Module
可以被子类继承,如下所示。
def init_weights(m):
if type(m) == nn.Linear:
torch.nn.init.xavier_uniform(m.weight) #
class LinearRegression(nn.Module):
def __init__(self):
super(LinearRegression, self).__init__()
self.fc1 = nn.Linear(20, 1)
self.apply(init_weights)
def forward(self, x):
x = self.fc1(x)
return x
Run Code Online (Sandbox Code Playgroud)
我的第一个问题是,为什么我可以简单地运行下面的代码,即使我__init__
没有任何 positinoal 参数,training_signals
并且它看起来像training_signals
传递给forward()
方法。它是如何工作的?
model = LinearRegression()
training_signals = torch.rand(1000,20)
model(training_signals)
Run Code Online (Sandbox Code Playgroud)
第二个问题是self.apply(init_weights)
内部如何运作?它是在调用forward
方法之前执行吗?
我对反向引用的使用感到非常困惑
strings <- c("^ab", "ab", "abc", "abd", "abe", "ab 12")
gsub("(ab) 12", "\\1 34", strings)
[1] "^ab" "ab" "abc" "abd" "abe" "ab 12"
gsub("(ab)12", "\\2 34", strings)
[1] "^ab" "ab" "abc" "abd" "abe" "ab 12"
Run Code Online (Sandbox Code Playgroud)
我知道\ 1表示第一个子模式(从左侧读取),\ 2表示第二个子模式,依此类推。但是我不知道这个子模式是什么意思。为什么\ 1和\ 2给出不同的输出
gsub("(ab)", "\\1 34", strings)
[1] "^ab 34" "ab 34" "ab 34c" "ab 34d" "ab 34e" "ab 34 12"
Run Code Online (Sandbox Code Playgroud)
另外,为什么我在(ab)之后删除12,然后得到这样的结果?
gsub("ab", "\\1 34", strings)
[1] "^ 34" " 34" " 34c" " 34d" " 34e" " 34 12"
Run Code Online (Sandbox Code Playgroud)
此外,如果ab没有括号怎么办?它表示什么?
我真的搞砸了反向引用,希望有人可以清楚地解释逻辑
我有一个函数调用特定的日期格式.例如,仅当格式为
2002-09-04 16:45:40
Run Code Online (Sandbox Code Playgroud)
然后该功能将起作用.否则,它将返回错误消息,例如
"Format incorrect"
Run Code Online (Sandbox Code Playgroud)
我想知道如何实现它?
>>> a = np.arange(12).reshape(3,4)
>>> a[0:3:2, :][:, [0,2]] = 100 ### the first time
>>> a
array([[100, 1, 100, 3],
[ 4, 5, 6, 7],
[100, 9, 100, 11]])
>>> a[:, [0, 2]][0:3:2, :] = 0 ### second time
>>> a
array([[100, 1, 100, 3],
[ 4, 5, 6, 7],
[100, 9, 100, 11]])
Run Code Online (Sandbox Code Playgroud)
我真的对 python 中的视图和副本感到困惑。上面的代码显示,数组a中给定的行和列第一次更改为100,从而更改了原始数组a。
但是,第二次原始数组没有改变。这是为什么?
我在R中有一些函数,我用Rcpp对它们进行了重新编码.每个函数都有一个独立的.cpp文件.一个叫做的功能add_inflow()
.以前,我将所有cpp函数放在我的桌面上并使用Rcpp::sourceCpp("add_inflow.cpp")
.然后,这个c ++函数可以通过插入参数给我一个输出值.
然后我想stormwindmodel
通过遵循Compiled Code,R Packages,Hadley将它们嵌入我的R包中
首先,我运行devtools::use_rcpp()
,然后将所有cpp函数移动到src
文件下.然后,我点击了构建和重新加载按钮,它已成功完成.在这一点上,我发现原始的R功能在环境面板中,但没有看到我的cpp功能.然后我跑了load_all
,这次cpp functinos出现了.但是,当我运行add_inflow_Cpp()
函数时,Rstudio给了我这个输出:
Error in .Call("stormwindmodel_add_forward_speed_Cpp", PACKAGE = "stormwindmodel", :
"stormwindmodel_add_forward_speed_Cpp" not available for .Call() for package "stormwindmodel"
Run Code Online (Sandbox Code Playgroud)
我错过了任何一步吗?对我有什么建议吗?
如果问题的质量不够好,请给我反馈,我会尽快编辑.
基本上,我想将列索引视为超参数。然后调整这个超参数以及管道中的其他模型超参数。在下面的示例中,这col_idx
是我的超参数。我自己定义了一个函数调用log_columns
,可以对某些列执行日志转换,该函数可以传入FunctionTransformer
. 然后将 FunctionTransformer 和模型放入管道中。
from sklearn.svm import SVC
from sklearn.decomposition import PCA
from sklearn.datasets import load_digits
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import FunctionTransformer
def log_columns(X, col_idx = None):
log_func = np.vectorize(np.log)
if col_idx is None:
return X
for idx in col_idx:
X[:,idx] = log_func(X[:,idx])
return X
pipe = make_pipeline(FunctionTransformer(log_columns, ), PCA(), SVC())
param_grid = dict(functiontransformer__col_idx = [None, [1]],
pca__n_components=[2, 5, 10],
svc__C=[0.1, 10, 100],
)
grid_search = GridSearchCV(pipe, param_grid=param_grid) …
Run Code Online (Sandbox Code Playgroud) 在car
包,我想预测称为响应变量prestige
也叫数据集中的Prestige
基础上income
,education
和因子type
的lm
功能.但在我适应数据之前,我想扩展education
和income
.如果您在R stuido中复制并运行它,下面的代码,控制台会说Error: variables ‘income’, ‘I(income^2)’, ‘education’, ‘I(education^2)’ were specified with different types from the fit
library(car)
summary(Prestige)
Prestige$education <- scale(Prestige$education)
Prestige$income <- scale(Prestige$income)
fit <- lm(prestige ~ income + I(income^2) + education + I(education^2)
+ income:education + type + type:income + type:I(income^2)
+ type:education + type:I(education^2)+ type:income:education, Prestige)
summary(fit)
pred <- expand.grid(income = c(1000, 20000), education = c(10,20),type = levels(Prestige $ …
Run Code Online (Sandbox Code Playgroud) 我知道交叉熵/互信息如何作为损失函数在分类决策中发挥作用。但我想知道为什么 0/1 损失不是一个好的选择。
statistics machine-learning decision-tree random-forest scikit-learn
我遇到了以下格式的字符串输入:
'1:[2,3],4:[1],3:[4],2:[4]'
Run Code Online (Sandbox Code Playgroud)
如何将其转换为以下格式:
{1:[2,3],4:[1],3:[4],2:[4]}
Run Code Online (Sandbox Code Playgroud)
谢谢!
我下面有一些代码,choice
只接受整数输入,但如果输入不是整数,则输出一些特殊的代码.但是,下面的代码来处理这个问题似乎有点冗长.无论如何要解决它?
from sys import exit
def gold_room():
print "This room is full of gold. How much do you take?"
choice = raw_input("> ")
if "0" in choice or "1" in choice or "2" in choice or "3" in choice or "4" in choice or "5" in choice or "6" in choice or "7" in choice or "8" in choice or "9" in choice:
how_much = int(choice)
else:
dead("Man, learn to type a number.")
if how_much < 50:
print …
Run Code Online (Sandbox Code Playgroud) r ×7
python ×5
rcpp ×2
scikit-learn ×2
arrays ×1
copy ×1
indexing ×1
numpy ×1
plot ×1
python-2.7 ×1
pytorch ×1
r-package ×1
rstudio ×1
shiny ×1
shiny-server ×1
statistics ×1
unit-testing ×1