我一直认为set.seed()只让随机变量生成器(例如,rnorm)为任何特定的输入值集生成唯一的序列。
但是,我想知道,为什么当我们设置set.seed(), 然后函数sample()不能正确完成它的工作?
具体来说,鉴于下面的例子,有没有一种方法可以set.seed在之前使用,rnorm但如果多次运行,sample仍然会从中产生新的随机样本?rnormsample
set.seed(123458)
x.y = rnorm(1e2)
sampled = sample(x = x.y, size = 20, replace = TRUE)
plot(sampled)
Run Code Online (Sandbox Code Playgroud) 我从数据馈送 (lmax) 中获取一次分钟数据 (OHLC),我想将其重新采样为五分钟数据,然后再将其重新采样为十分钟、十五分钟和三十分钟。
我正在使用以下逻辑:
开盘=每 5 根蜡烛的第一个值(第一根蜡烛打开)
高 = 5 根蜡烛的最高值
低 = 5 根蜡烛的最低值
Close=最后一根蜡烛的收盘价(第 5 根蜡烛)
这是重新采样数据的正确方法吗?我觉得这是合乎逻辑的,但出于某种原因,他们网站上的数据源和我的代码之间存在明显的差异。我觉得是这样,因为我重新采样错了;如果我使用 Python,我可以参考 Pandas,但不能使用 C#(据我所知)。
这是重新采样数据的函数:
private static List<List<double>> normalize_candles(List<List<double>> indexed_data, int n)
{
double open = 0;
double high = 0;
double low = 5;
double close = 0;
int trunc = 0;
if (indexed_data.Count() % n != 0)
{
trunc = indexed_data.Count() % n;
for (int i = 0; i < trunc; i++)
{
indexed_data.RemoveAt(indexed_data.Count() - 1);
}
}
{ …Run Code Online (Sandbox Code Playgroud) 我目前有一个需要更改采样率的文件列表。
我最近意识到这可以使用sox 但是当我尝试这样做时,我不断收到一条错误消息 sox wav: Premature EOF on .wav input file并导致音频文件为空..似乎 sox 无法重新采样输入的音频文件=输出...我有点需要,如果我必须转换音频文件的整个目录...
目前使用的命令:
~/kaldi-trunk/egs/yesno/s5_k_added$ sox 0_0_0_0_1_1_1_1.wav -r 8000 0_0_0_0_1_1_1_1.wav
sox WARN wav: Premature EOF on .wav input file
:~/kaldi-trunk/egs/yesno/s5_k_added$ play 0_0_0_0_1_1_1_1.wav
0_0_0_0_1_1_1_1.wav:
File Size: 44
Encoding: Signed PCM
Channels: 1 @ 16-bit
Samplerate: 8000Hz
Replaygain: off
Duration: unknown
In:0.00% 00:00:00.00 [00:00:00.00] Out:0 [ | ] Clip:0
Done.
Run Code Online (Sandbox Code Playgroud)
如何重新采样音频文件目录?
嗨,我正在尝试向后重新采样 Pandas DataFrame。这是我的数据框:
seconds = np.arange(20, 700, 60)
timedeltas = pd.to_timedelta(seconds, unit='s')
vals = np.array([randint(-10,10) for a in range(len(seconds))])
df = pd.DataFrame({'values': vals}, index = timedeltas)
Run Code Online (Sandbox Code Playgroud)
那么我有
In [252]: df
Out[252]:
values
00:00:20 8
00:01:20 4
00:02:20 5
00:03:20 9
00:04:20 7
00:05:20 5
00:06:20 5
00:07:20 -6
00:08:20 -3
00:09:20 -5
00:10:20 -5
00:11:20 -10
Run Code Online (Sandbox Code Playgroud)
和
In [253]: df.resample('5min').mean()
Out[253]:
values
00:00:20 6.6
00:05:20 -0.8
00:10:20 -7.5
Run Code Online (Sandbox Code Playgroud)
我想要的是
Out[***]:
values
00:01:20 6
00:06:20 valb
00:11:20 -5.8
Run Code Online (Sandbox Code Playgroud)
如果我回滚数据帧并计算从后到前的每个 bin 中的平均值,则每个新时间的值都是这些值。例如,在这种情况下,最后一个值应该是 …
我正在做文本分类,并且我有非常不平衡的数据,例如
Category | Total Records
Cate1 | 950
Cate2 | 40
Cate3 | 10
Run Code Online (Sandbox Code Playgroud)
现在我想对 Cate2 和 Cate3 进行过度采样,因此它至少有 400-500 条记录,我更喜欢使用 SMOTE 而不是随机采样,代码
from sklearn.model_selection import train_test_split
from imblearn.over_sampling import SMOTE
X_train, X_test, y_train, y_test = train_test_split(fewRecords['text'],
fewRecords['category'])
sm = SMOTE(random_state=12, ratio = 1.0)
x_train_res, y_train_res = sm.fit_sample(X_train, y_train)
Run Code Online (Sandbox Code Playgroud)
它不起作用,因为它无法生成示例合成文本,现在当我将其转换为矢量时
count_vect = CountVectorizer(analyzer='word', token_pattern=r'\w{1,}')
count_vect.fit(fewRecords['category'])
# transform the training and validation data using count vectorizer object
xtrain_count = count_vect.transform(X_train)
ytrain_train = count_vect.transform(y_train)
Run Code Online (Sandbox Code Playgroud)
当我想在分类后预测真实类别时,我不确定这是否是正确的方法以及如何将向量转换为真实文本
我正在尝试使用 MultinomialNB 进行一些文本分类,但我遇到了问题,因为我的数据不平衡。(为简单起见,下面是一些示例数据。实际上,我的数据要大得多。)我正在尝试使用过采样对我的数据进行重新采样,并且理想情况下我希望将其构建到此管道中。
下面的管道在没有过度采样的情况下工作正常,但同样,在现实生活中我的数据需要它。这是非常不平衡的。
使用此当前代码,我不断收到错误消息:“TypeError:所有中间步骤都应该是转换器并实现拟合和转换。”
如何将 RandomOverSampler 构建到此管道中?
data = [['round red fruit that is sweet','apple'],['long yellow fruit with a peel','banana'],
['round green fruit that is soft and sweet','pear'], ['red fruit that is common', 'apple'],
['tiny fruits that grow in bunches','grapes'],['purple fruits', 'grapes'], ['yellow and long', 'banana'],
['round, small, green', 'grapes'], ['can be red, green, or purple', 'grapes'], ['tiny fruits', 'grapes'],
['small fruits', 'grapes']]
df = pd.DataFrame(data,columns=['Description','Type'])
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state = 0)
text_clf = Pipeline([('vect', …Run Code Online (Sandbox Code Playgroud) 我使用过一个小数据集,并使用了 mlr 包的嵌套交叉验证。然而,插入符在测试不同模型方面有一些优势。所以,我想知道:是否有人对如何使用插入符实现嵌套交叉验证有任何好的建议(如果可能的话,还有一个例子)?非常感谢。
我需要重新采样数据来计算每周的 pct_change()。我怎样才能获得每周的变化?
有点像,data['pct_week'] = data['Adj Close'].resample('W').ffill().pct_change()但数据需要分组data.groupby(['month', 'week'])
这样每个月都会产生 4 个每周变化的值。然后我可以绘制
我所做的是df['pct_week'] = data['Adj Close'].groupby(['week', 'day']).pct_change()但我收到了这个错误TypeError: 'type' object does not support item assignment
我正在使用 FFmpeg 并尝试使用内置的 FFmpeg“opus”编解码器将原始 PCM 声音编码和解码为 Opus。我的输入样本是 AV_SAMPLE_FMT_S16 格式的原始 PCM 8000 Hz 16 位单声道。由于 Opus 只需要采样格式 AV_SAMPLE_FMT_FLTP 和采样率 48000 Hz,所以我在编码之前重新采样我的样本。
我有两个ResamplerAudio类的实例,它们执行重采样音频样本的工作,并且有一个成员SwrContext,我使用第一个实例ResamplerAudio在编码之前重采样原始 PCM 输入音频,第二个实例用于重采样解码音频以获得它的格式和采样率与输入原始音频的源值相同。
ResamplerAudio 类有一个函数来初始化它的 SwrContext 成员,如下所示:
void ResamplerAudio::init(AVCodecContext *codecContext, int inSampleRate, int outSampleRate, AVSampleFormat inSampleFmt, AVSampleFormat outSampleFmt)
{
swrContext = swr_alloc();
if (!swrContext)
{
LOGE(TAG, "[init] Couldn't allocate swr context");
return;
}
av_opt_set_int(swrContext, "in_channel_layout", (int64_t) codecContext->channel_layout, 0);
av_opt_set_int(swrContext, "out_channel_layout", (int64_t) codecContext->channel_layout, 0);
av_opt_set_int(swrContext, "in_channel_count", codecContext->channels, 0);
av_opt_set_int(swrContext, "out_channel_count", codecContext->channels, 0);
av_opt_set_int(swrContext, "in_sample_rate", inSampleRate, …Run Code Online (Sandbox Code Playgroud) 我发现 xarray 执行变量重采样以及平均值和标准差计算的惊人能力。
是否有像 .mean() 方法一样在数据时间重采样后直接计算偏度的方法?
谢谢