我有一个包含记录列表的文件,我一次解析一行。每条记录都以换行符分隔,每个值以空格分隔。这只是一个简化的例子,但它具有与真实数据相似的结构。
Bob blue pizza
Sally red sushi
Run Code Online (Sandbox Code Playgroud)
第一个值是名称,然后是他们最喜欢的颜色,然后是他们最喜欢的食物。假设这是在一个处理循环中,我想为每个值设置变量。对于第一行,我的值应该是这样的。
friendsName = "Bob";
favoriteColor = "blue";
favoriteFood = "pizza";
Run Code Online (Sandbox Code Playgroud)
我在行中阅读并开始
lineInFile = "Bob blue pizza";
Run Code Online (Sandbox Code Playgroud)
strsplit 似乎是个好主意,但它输出一个单元格数组而不是一个字符串矩阵,我最终得到
strsplit(lineInFile, " ") =
{
[1,1] = Bob
[1,2] = blue
[1,3] = pizza
}
Run Code Online (Sandbox Code Playgroud)
我想要类似的东西
{friendsName,favoriteColor,favoriteFood} = strsplit(lineInFile, " ");
Run Code Online (Sandbox Code Playgroud)
这给了我 error: invalid lvalue function called in expression
数组可以用作左值,所以我试过了
cell2mat(strsplit(lineInFile, " "))
ans = Bobbluepizza
Run Code Online (Sandbox Code Playgroud)
那不是我想要的。
所以我在 MATLAB 中编写了一个 k-means 脚本,因为原生函数似乎不是很有效,而且它似乎是完全可操作的。它似乎适用于我正在使用的小型训练集(通过文本文件提供的 150x2 矩阵)。但是,我的目标数据集(一个 3924x19 矩阵)的运行时间呈指数级增长。
我不是最擅长矢量化的,所以任何建议都将不胜感激。到目前为止,这是我的 k-means 脚本(我知道我将不得不调整我的收敛条件,因为它正在寻找精确匹配,对于这么大的数据集,我可能需要更多的迭代,但我希望它能够先在合理的时间内完成,然后再提高这个数字):
clear all;
%take input file (manually specified by user
disp('Please type input filename (in working directory): ')
target_file = input('filename: ', 's');
%parse and load into matrix
data = load(target_file);
%prompt name of output file for later) UNCOMMENT BELOW TWO LINES LATER
% disp('Please type output filename (to be saved in working directory): ')
% output_name = input('filename:', 's')
%prompt number of clusters
disp('Please type desired number of …Run Code Online (Sandbox Code Playgroud) 问题在标题中,非常简单。
我有一个文件f,我正在从中读取一个ubyte数组:
arr = numpy.fromfile(f, '>u1', size * rows * cols).reshape((size, rows, cols))
max_value = 0xFF # max value of ubyte
Run Code Online (Sandbox Code Playgroud)
目前我正在 3 次重新规范化数据,如下所示:
arr = images.astype(float)
arr -= max_value / 2.0
arr /= max_value
Run Code Online (Sandbox Code Playgroud)
由于数组有点大,这需要明显的几分之一秒。
如果我能在 1 或 2 次数据传递中做到这一点,那就太好了,因为我认为这会更快。
有什么方法可以让我执行“复合”矢量操作来减少传递次数?
或者,有没有其他方法可以让我加快速度?
问题:在 Octave 中进行特征归一化时,零方差输入会导致 div-zero 错误。
问题:在处理矢量化数据时,是否有一种很好的(r)方法来处理 div-zero?
示例: 输入是一个包含列中多个数据集的矩阵:
X = [1 3.5 7.5 9 ;
1 4 8 9 ;
1 4.5 8.5 9]
Run Code Online (Sandbox Code Playgroud)
因此,X包含三个系列:x_1 = [1,1,1],x_2 = [7.5, 8, 8.5],和x_3 = [9,9,9]。为了使用矢量化对每个集合进行归一化,以下方法似乎是明智的:
X = [1 3.5 7.5 9 ;
1 4 8 9 ;
1 4.5 8.5 9]
Run Code Online (Sandbox Code Playgroud)
然而,上述方法将失败,因为双方x_1并x_3具有零方差等会发生分裂,零错误。
我对零方差数据的首选处理是将 sigma 设置为 1。目前我正在使用以下 kludge:
mu = mean(X);
sigma = std(X);
X_norm = (1 ./ …Run Code Online (Sandbox Code Playgroud) 我正在尝试采用现有的 DataFrame 并附加一个新列。
假设我有这个 DataFrame(只是一些随机数):
a b c d e
0 2.847674 0.890958 -1.785646 -0.648289 1.178657
1 -0.865278 0.696976 1.522485 -0.248514 1.004034
2 -2.229555 -0.037372 -1.380972 -0.880361 -0.532428
3 -0.057895 -2.193053 -0.691445 -0.588935 -0.883624
Run Code Online (Sandbox Code Playgroud)
我想创建一个新列“f”,将每一行乘以“成本”向量,例如 [1,0,0,0,0]。因此,对于第 0 行,f 列中的输出应为 2.847674。
这是我目前使用的功能:
def addEstimate (df, costs):
row_iterator = df.iterrows()
for i, row in row_iterator:
df.ix[i, 'f'] = np.dot(costs, df.ix[i])
Run Code Online (Sandbox Code Playgroud)
我正在用一个 15 个元素的向量来做这个,超过约 20k 行,我发现这超级慢(半小时)。我怀疑使用iterrows和ix效率低下,但我不确定如何纠正这个问题。
有没有一种方法可以一次将它应用于整个 DataFrame,而不是遍历行?或者您有其他建议可以加快速度吗?
我有一个包含 4 列的大型数据框 df:
id period ret_1m mkt_ret_1m
131146 CAN00WG0 199609 -0.1538 0.047104
133530 CAN00WG0 199610 -0.0455 -0.014143
135913 CAN00WG0 199611 0.0000 0.040926
138334 CAN00WG0 199612 0.2952 0.008723
140794 CAN00WG0 199701 -0.0257 0.039916
143274 CAN00WG0 199702 -0.0038 -0.025442
145754 CAN00WG0 199703 -0.2992 -0.049279
148246 CAN00WG0 199704 -0.0919 -0.005948
150774 CAN00WG0 199705 0.0595 0.122322
153318 CAN00WG0 199706 -0.0337 0.045765
id period ret_1m mkt_ret_1m
160980 CAN00WH0 199709 0.0757 0.079293
163569 CAN00WH0 199710 -0.0741 -0.044000
166159 CAN00WH0 199711 0.1000 -0.014644
168782 CAN00WH0 199712 …Run Code Online (Sandbox Code Playgroud) scipy.special 中的 expit 函数是一个向量化的 sigmoid 函数。它计算 1 / (1+e^(-x)),这很复杂,可能涉及泰勒级数。
我了解了“快速 sigmoid”,1 / (1 + abs(x)),它应该快得多——但是内置的 expit 函数大大优于它,即使我将它作为 lambda 表达式交给 numpy.vectorize .
这是测试它们的一种方法:
from scipy.special import expit
data = np.random.rand(1000000)
Run Code Online (Sandbox Code Playgroud)
内置的复杂 sigmoid 很快:
%prun expit(data)
3 function calls in 0.064 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.064 0.064 0.064 0.064 <string>:1(<module>)
1 0.000 0.000 0.064 0.064 {built-in method builtins.exec}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
Run Code Online (Sandbox Code Playgroud)
更简单的 sigmoid 大约慢 20 倍:
%prun …Run Code Online (Sandbox Code Playgroud) 假设我创建了这个表和一组值:
names = {'a'; 'b'; 'c'; 'd'} ; values = {'1'; '2'; '3'; '4'};
originalTable = table(names, values, 'VariableNames', {'names', 'values'});
nRepeat = [10, 50, 100, 2] ;
Run Code Online (Sandbox Code Playgroud)
我想创建一个新表,它将包含每行重复nRepeat对应索引的次数,即我将第一行或原始表重复10次,然后原始表的第二行重复50次,等等...此外,我想用重复索引向新表添加一列.
我做了什么:
% Initialize newTable to allocate memory space
totalRepetitions = sum(nRepeat) ;
% Repeated first row of the original table the same number of times as the totalRepetitions that will happen, also adding the new column with the index of repetition
newTable = repmat([originalTable(1,:), array2table(1, 'VariableNames', {'idxRepetition'})], totalRepetitions , 1) ; …Run Code Online (Sandbox Code Playgroud) 如何避免在3D数组中从多个页面访问不同的行,同时避免for-loop?
假设我有一个10x5x3矩阵(mat1),我想将三个页面(例如第一,第二和第三页的第四,第二和第五行)中不同的单独行复制到另一个10x5x3矩阵的第一行(mat2)。
我的解决方案使用for-loop。矢量化呢?
mat1 = randi(100, 10, 5, 3)
mat2 = nan(size(mat1))
rows_to_copy = [4, 2, 5]
for i = 1 : 3
mat2(1, :, i) = mat1(rows_to_copy(i), :, i)
end
Run Code Online (Sandbox Code Playgroud) 我正在关注本文档集群教程。作为输入,我提供了一个txt文件,可以在此处下载。它是3个其他txt文件的组合文件,并使用\ n进行了分隔。创建tf-idf矩阵后,我收到此警告:
,, UserWarning:您的stop_words可能与您的预处理不一致。标记停用词会生成标记['abov','afterafter','alon','alreadi','always','ani','anoth','anyon','anyth','anywher','becam' ,'becaus','becom','befor','besid','cri','describ','dure','els','elsewher','empti','everi','everyon',' Everyth”,“ everywher”,“ fifti”,“ forti”,“ henc”,“ hereaft”,“ herebi”,“ howev”,“ hundr”,“ inde”,“ mani”,“ meanwhil”,“ moreov” ,“ nobodi”,“ noon”,“ noth”,“ nowher”,“ onc”,“ onli”,“ otherwis”,“ ourselv”,“ perhap”,“ pleas”,“ sever”,“ sinc”,“ sincer”,“ sixti”,“ someon”,“ someth”,“ sometim”,“ somewher”,“ themselv” ,“ thenc”,“ thereaft”,“ therebi”,“ therefor”,“ togeth”,“ twelv”,“ twenti”,“ veri”,“ whatev”,“ whenc”,“ whenev”,“ wherea”,“ whereaft”,“ wherebi”,“ wherev”,“ whi”,“ yourselv”]不在stop_words中。“ stop_words”。%sorted(不一致))”。'thereaft','therebi','therefor','togeth','twelv','twenti','veri','whatev','whenc','whenev','wherea','whereaft','wherebi ','wherev','whi','yourselv']不在stop_words中。“ stop_words”。%sorted(不一致))”。'thereaft','therebi','therefor','togeth','twelv','twenti','veri','whatev','whenc','whenev','wherea','whereaft','wherebi ','wherev','whi','yourselv']不在stop_words中。“ stop_words”。%sorted(不一致))”。
我想这与复词和停用词的顺序有关,但是由于这是我在txt处理中的第一个项目,我有点迷路,而且我不知道该如何解决...
import pandas as pd
import nltk
from nltk.corpus import stopwords
import re
import os …Run Code Online (Sandbox Code Playgroud) vectorization ×10
matlab ×4
python ×4
numpy ×2
octave ×2
optimization ×2
pandas ×2
apply ×1
arrays ×1
beta ×1
dataframe ×1
dot-product ×1
ipython ×1
k-means ×1
lvalue ×1
scipy ×1
statistics ×1
stemming ×1
stop-words ×1
strsplit ×1
tf-idf ×1