我正在寻找一种潜入列表并直接访问其元素的方法.例如,以下是获取集合的笛卡尔乘积的常规方法.
>>> list(itertools.product((0,1), (0,1),(0,1),(0,1)))
[(0, 0, 0, 0), (0, 0, 0, 1), (0, 0, 1, 0), (0, 0, 1, 1), (0, 1, 0, 0), (0, 1, 0, 1), (0, 1, 1, 0), (0, 1, 1, 1), (1, 0, 0, 0), (1, 0, 0, 1), (1, 0, 1, 0), (1, 0, 1, 1), (1, 1, 0, 0), (1, 1, 0, 1), (1, 1, 1, 0), (1, 1, 1, 1)]
Run Code Online (Sandbox Code Playgroud)
但是在这种情况下,这四个套装是相同的,很快就会一次又一次地输入它而变得无聊和烦人,这让人想到这样做:
>>> list(itertools.product([(0,1)]*4)
Run Code Online (Sandbox Code Playgroud)
但当然它不起作用,因为itertools.product函数会将其视为一组而不是四组.所以,问题是,有没有办法做到这一点:
>>> list(itertools.product(delist([(0,1)]*4))
Run Code Online (Sandbox Code Playgroud) 这是我觉得难以理解的事情:
cl = makeCluster(rep("localhost", 8), "SOCK")
# This will not work, error: dat not found in the nodes
pmult = function(cl, a, x)
{
mult = function(s) s*x
parLapply(cl, a, mult)
}
scalars = 1:4
dat = rnorm(4)
pmult(cl, scalars, dat)
# This will work
pmult = function(cl, a, x)
{
x
mult = function(s) s*x
parLapply(cl, a, mult)
}
scalars = 1:4
dat = rnorm(4)
pmult(cl, scalars, dat)
# This will work
pmult = function(cl, a, x)
{
mult …Run Code Online (Sandbox Code Playgroud) 我有一个整数向量,我希望将其分成簇,以便任意两个簇之间的距离大于下限,并且在任何簇内,两个元素之间的距离小于上限.
例如,假设我们有以下向量:
1,4,5,6,9,29,32,36
并将上述下限和上限分别设置为19和9,下面的两个向量应该是一个可能的结果:
1,4,5,6,9
29,32,36
感谢@flodel的评论,我意识到这种聚类可能是不可能的.所以我想稍微修改一下这些问题:
如果我只在集群距离下限之间强加什么可能的聚类方法?如果我仅在 集群距离上限内施加了什么可能的聚类方法?
我正在尝试设置vim来编译和运行编辑器中的c和c ++程序,但这些似乎不起作用:
autocmd! filetype *.c nnoremap <leader>cc :!gcc -o %:p:r %<cr>
autocmd! filetype *.c nnoremap <leader>cr :!gcc -o %:p:r %<cr>:!%:p:r<cr>
autocmd! filetype *.cpp *.cc nnoremap <leader>cc :!g++ -o %:p:r %<cr>
autocmd! filetype *.cpp *.cc nnoremap <leader>cr :!g++ -o %:p:r %<cr>:!%:p:r<cr>
Run Code Online (Sandbox Code Playgroud) 这是从一个c ++教程中摘录的:
// vectors: overloading operators example
#include <iostream>
using namespace std;
class CVector {
public:
int x,y;
CVector () {};
CVector (int,int);
CVector operator + (CVector);
};
CVector::CVector (int a, int b) {
x = a;
y = b;
}
CVector CVector::operator+ (CVector param) {
CVector temp;
temp.x = x + param.x;
temp.y = y + param.y;
return (temp);
}
int main () {
CVector a (3,1);
CVector b (1,2);
CVector c;
c = a + b; …Run Code Online (Sandbox Code Playgroud) 我有以下vimscript .vim/ftplugin目录:
" change to header file from c file or vice versa
function! CppAlter()
python << endpy
import vim
import os
bufferNames = [os.path.basename(b.name) for b in vim.buffers]
currentBufName = vim.eval("expand('%:p:t')")
currentBufStem, currentBufExt = os.path.splitext(currentBufName)
if currentBufExt == ".cpp" or currentBufExt == ".c" or currentBufExt == ".cc":
altBufName1 = currentBufStem + ".h"
altBufName2 = currentBufStem + ".hpp"
if altBufName1 in bufferNames:
vim.command("b " + altBufName1)
elif altBufName2 in bufferNames:
vim.command("b " + altBufName2)
else:
raise ValueError("No header file corresponding …Run Code Online (Sandbox Code Playgroud) 我想在numpy中生成一个1D数组,如下所示:
In [181]: np.concatenate((np.arange(1, 4), np.arange(2, 4), np.arange(3, 4)))
Out[181]: array([1, 2, 3, 2, 3, 3])
Run Code Online (Sandbox Code Playgroud)
在更大范围内,伪代码:
concatenate(1:n, 2:n, 3:n, ..., n:n)
Run Code Online (Sandbox Code Playgroud)
是否有一种矢量化的方式在numpy和/或pandas中这样做?
pandas的parallel_coordinates函数非常有用:
import pandas
import matplotlib.pyplot as plt
from pandas.tools.plotting import parallel_coordinates
sampdata = read_csv('/usr/local/lib/python3.3/dist-packages/pandas/tests/data/iris.csv')
parallel_coordinates(sampdata, 'Name')
Run Code Online (Sandbox Code Playgroud)

但是当你有连续的数据时,它的行为并不是你所期望的:
mypos = np.random.randint(10, size=(100, 2))
mydata = DataFrame(mypos, columns=['x', 'y'])
myres = np.random.rand(100, 1)
mydata['res'] = myres
parallel_coordinates(mydata, 'res')
Run Code Online (Sandbox Code Playgroud)

我希望线条的颜色能够反映连续变量的大小,例如从白色到黑色的渐变,最好还有一些透明度(alpha值)的可能性,旁边还有一个颜色条.
假设我有一个返回ggplot对象的函数:
getplot = function() {
x = rnorm(16)
y = rnorm(16)
dat = data.frame(x, y)
myplot = ggplot(dat, aes(x, y)) + geom_point()
myplot
}
Run Code Online (Sandbox Code Playgroud)
在调用该函数之后
x = getplot()
Run Code Online (Sandbox Code Playgroud)
,我该如何改变磅值?
就像一个测试:
myclass = setRefClass("myclass",
fields = list(
x = "numeric",
y = "numeric"
))
myclass$methods(
dfunc = function(i) {
message("In dfunc, I save x and y...")
obj = .self
base::save(obj, file="/tmp/obj.rda")
}
)
myclass$methods(
print = function() {
if (.self$x > 10) {
stop("x is too large!")
}
message(paste("x: ", .self$x))
message(paste("y: ", .self$y))
}
)
myclass$methods(
initialize = function(x=NULL, y=NULL, obj=NULL) {
if(is.null(obj)) {
.self$x = x
.self$y = y
}
else {
.self$x = obj$x
.self$y = obj$y …Run Code Online (Sandbox Code Playgroud)