我想知道是否有人可以查看以下代码和最小示例并提出改进建议 - 特别是关于使用非常大的数据集时代码的效率.
该函数采用data.frame并将其按分组变量(因子)拆分,然后计算每个组中所有行的距离矩阵.
我不需要保持距离矩阵 - 只有一些统计数据,即均值,直方图......,然后它们可以被丢弃.
我对内存分配等知之甚少,我想知道最好的方法是什么,因为我将使用每组10.000-100,000个案例.任何想法将不胜感激!
此外,在遇到严重的内存问题时,将bigmemory或其他大型数据处理包包含在函数中的最不痛苦的方法是什么?
FactorDistances <- function(df) {
# df is the data frame where the first column is the grouping variable.
# find names and number of groups in df (in the example there are three:(2,3,4)
factor.names <- unique(df[1])
n.factors <-length(unique(df$factor))
# split df by factor into list - each subset dataframe is one list element
df.l<-list()
for (f in 1:n.factors) {df.l[[f]]<-df[which(df$factor==factor.names[f,]),]}
# use lapply to go through list and calculate distance matrix for …Run Code Online (Sandbox Code Playgroud) 有一个包含字符串的大型数据集.我只是想通过read_fwf使用widths打开它,如下所示:
widths = [3, 7, ..., 9, 7]
tp = pandas.read_fwf(file, widths=widths, header=None)
Run Code Online (Sandbox Code Playgroud)
它可以帮助我标记数据,但系统崩溃(使用nrows = 20000).然后我决定用chunk(例如20000行)来做,像这样:
cs = 20000
for chunk in pd.read_fwf(file, widths=widths, header=None, chunksize=ch)
...: <some code using chunk>
Run Code Online (Sandbox Code Playgroud)
我的问题是:在对块进行一些处理(标记行,删除或修改列)之后,我应该在循环中做什么来合并(连接?)块.csv文件?还是有另一种方式?
使用 Eloquent,如何根据chunk函数闭包内的条件终止分块?我试过返回,但这似乎只终止当前块而不是所有块。此时,我想停止从数据库中检索记录。
$query->chunk(self::CHUNK_SIZE, function ($objects) {
if (someCondition) {
// terminate chunking here
return;
}
});
Run Code Online (Sandbox Code Playgroud) 我遇到一种情况,我想将初始化nils为的数组分成空段和包含连续数字的段。
我发现的高阶chunk函数Array为此提供了一个优雅的解决方案:
<< [nil,nil,1,2,nil,3].chunk { |e| !e.nil? }.each { |e| p e }
>> [false, [nil, nil]]
>> [true, [1, 2]]
>> [false, [nil]]
>> [true, [3]]
Run Code Online (Sandbox Code Playgroud)
但是,假设我希望输出也将起始索引包括在每个块中的原始数组中,即将上面的输出增加到类似以下内容:
>> [false, 0, [nil, nil]]
>> [true, 2, [1, 2]]
>> [false, 4, [nil]]
>> [true, 5, [3]]
Run Code Online (Sandbox Code Playgroud)
是否有一种解决方案,可以保留上述片段的表现力?
提前致谢。
我试图建立一个nltk来获取单词的上下文。我有两个句子
sentences=pd.DataFrame({"sentence": ["The weather was good so I went swimming", "Because of the good food we took desert"]})
Run Code Online (Sandbox Code Playgroud)
我想找出“好”这个词是什么意思。我的想法是对句子进行分块(来自此处的教程代码),然后查看单词“ good”和一个名词是否在同一节点中。如果不是,则表示该名词之前或之后的名词。
首先,按照本教程中的说明构建块
from nltk.corpus import conll2000
test_sents = conll2000.chunked_sents('test.txt', chunk_types=['NP'])
train_sents = conll2000.chunked_sents('train.txt', chunk_types=['NP'])
class ChunkParser(nltk.ChunkParserI):
def __init__(self, train_sents):
train_data = [[(t,c) for w,t,c in nltk.chunk.tree2conlltags(sent)]
for sent in train_sents]
self.tagger = nltk.TrigramTagger(train_data)
def parse(self, sentence):
pos_tags = [pos for (word,pos) in sentence]
tagged_pos_tags = self.tagger.tag(pos_tags)
chunktags = [chunktag for (pos, chunktag) in tagged_pos_tags]
conlltags = [(word, pos, chunktag) …Run Code Online (Sandbox Code Playgroud) 所以程序会读取一个带字符串的文件.然后该字符串将保存到另一个文件中,但该字符串将被拆分为5个组.
例.
鉴于其内容file1.txt将是thecatsatonthemat,内容file2.txt将是theca tsato nthem at.
我正在努力在我的絮凝功能中找到错误.
该函数的目标是获取一个列表并将每组连续值组合成一个值.例如...
[1, 4, 4, 2, 0, 3, 3, 3] => [1, 4, 2, 0, 3]
现在的功能是......
def flocculate(array):
for index1, val1 in enumerate(array):
if val1 == 0 or not not val1:
new_array = array[index1+1:]
for index2, val2 in enumerate(new_array):
if array[index1] == val2:
array[index1 + index2 + 1] = False
else:
break
return [value for value in array if type(value) is not bool]
Run Code Online (Sandbox Code Playgroud)
但是,它似乎没有很好地处理零.
例如,下面显示的输入得到一些零正确,但是错过了其他一些......
[2, 4, 4, 0, 3, 7, 0, 2, 2, 2, 8, 0, 0, 0] …
是否有一种方法可以使用预先存在的Linq函数从项目列表中创建任意大小的组?
例如:
[1,2,3,4,5,6,7]
Run Code Online (Sandbox Code Playgroud)
当执行类似list.Group(3)之类的操作时,会产生一个IEnumberable的IEnumebles,看起来像下面的序列.
[[1,2,3],[4,5,6],[7]]
Run Code Online (Sandbox Code Playgroud) 让我说我有这个
var array =[23,345,6,765,54423,45654,7657,43,43,5,765,3456,768,897,545,645,87,4556,432,543,534];
var chunkEachBy = [3,2,1,5,6,4];
var chunked = [];
for(var i =0, l = chunkEachBy.length; i < l; i++)
{
chunked.push(array.splice(0, chunkEachBy[i]));
}
Run Code Online (Sandbox Code Playgroud)
输出:
[Array[3], Array[2], Array[1], Array[5], Array[6], Array[4]]
Run Code Online (Sandbox Code Playgroud)
它只是简单地array进入长度chunkEachBy,然后为每个值组合每个值chunkEachBy.
我的问题是,在这种情况下,不是将结果推送到一个单独的数组中,而是chunked将这一切都完成到原始数组并产生相同的结果吗?
JSFiddle - https://jsfiddle.net/b7twnurm/1/