我有一些一维数据,并与样条拟合.然后我想在其中找到拐点(忽略鞍点).现在我通过在splev生成的很多值上使用scipy.signal.argrelmin(和argrelmax)来搜索其第一个派生的极值.
import scipy.interpolate
import scipy.optimize
import scipy.signal
import numpy as np
import matplotlib.pyplot as plt
import operator
y = [-1, 5, 6, 4, 2, 5, 8, 5, 1]
x = np.arange(0, len(y))
tck = scipy.interpolate.splrep(x, y, s=0)
print 'roots', scipy.interpolate.sproot(tck)
# output:
# [0.11381478]
xnew = np.arange(0, len(y), 0.01)
ynew = scipy.interpolate.splev(xnew, tck, der=0)
ynew_deriv = scipy.interpolate.splev(xnew, tck, der=1)
min_idxs = scipy.signal.argrelmin(ynew_deriv)
max_idxs = scipy.signal.argrelmax(ynew_deriv)
mins = zip(xnew[min_idxs].tolist(), ynew_deriv[min_idxs].tolist())
maxs = zip(xnew[max_idxs].tolist(), ynew_deriv[max_idxs].tolist())
inflection_points = sorted(mins + maxs, key=operator.itemgetter(0))
print …Run Code Online (Sandbox Code Playgroud) 离开作用域时自动调用函数的最优雅的解决方案是什么?我目前的方法(见下文)有效,但我想应该有一些更通用的东西,比如为此编写自定义类。
#include <iostream>
#include <functional>
using namespace std;
class DoInDtor
{
public:
typedef function<void()> F;
DoInDtor(F f) : f_(f) {};
~DoInDtor() { f_(); }
private:
F f_;
};
void foo()
{
DoInDtor byeSayerCustom([](){ cout << "bye\n"; });
auto cond = true; // could of course also be false
if ( cond )
return;
return;
}
int main()
{
foo();
}
Run Code Online (Sandbox Code Playgroud)
当然,人们可能会滥用 std::unique_ptr 及其自定义删除器,但由于我并没有真正在这里获取资源,因此就代码可读性而言,这对我来说听起来也不太好。有什么建议?
以下代码编译但在VC++ 2015(发行版)中产生未定义的输出,并与其他编译器产生运行时错误.
#include <functional>
#include <iostream>
int main()
{
std::function<int(int)> f = [](int x) { return x; };
std::function<const int&(const int& x)> g = f;
std::cout << g( 42 ) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
为什么g = f;允许分配?
我试着理解在什么情况下C++编译器能够执行循环融合,何时不能.
以下代码测量两种不同方法的性能,以计算f(x) = (2*x)^2向量中所有值的平方双精度().
#include <chrono>
#include <iostream>
#include <numeric>
#include <vector>
constexpr int square( int x )
{
return x * x;
}
constexpr int times_two( int x )
{
return 2 * x;
}
// map ((^2) . (^2)) $ [1,2,3]
int manual_fusion( const std::vector<int>& xs )
{
std::vector<int> zs;
zs.reserve( xs.size() );
for ( int x : xs )
{
zs.push_back( square( times_two( x ) ) );
}
return zs[0];
}
// map (^2) . …Run Code Online (Sandbox Code Playgroud) 在使用Keras(带有TensorFlow后端)调整深层卷积网络时,我想尝试一下MaxPooling2D和之间的混合AveragePooling2D,因为这两种策略似乎都可以改善我的目标的两个不同方面。
我正在考虑这样的事情:
-------
|8 | 1|
x = ---+---
|1 | 6|
-------
average_pooling(x) -> 4
max_pooling(x) -> 8
hybrid_pooling(x, alpha_max=0.0) -> 4
hybrid_pooling(x, alpha_max=0.25) -> 5
hybrid_pooling(x, alpha_max=0.5) -> 6
hybrid_pooling(x, alpha_max=0.75) -> 7
hybrid_pooling(x, alpha_max=1.0) -> 8
Run Code Online (Sandbox Code Playgroud)
或等式:
hybrid_pooling(x, alpha_max) =
alpha_max * max_pooling(x) + (1 - alpha_max) * average_pooling(x)
Run Code Online (Sandbox Code Playgroud)
既然看起来好像没有现成的东西,那么如何以有效的方式实现它呢?
这段代码
#include <iostream>
#include <optional>
struct foo
{
explicit operator std::optional<int>() {
return std::optional<int>( 1 );
}
explicit operator int() {
return 2;
}
};
int main()
{
foo my_foo;
std::optional<int> my_opt( my_foo );
std::cout << "constructor: " << my_opt.value() << std::endl;
my_opt = static_cast<std::optional<int>>(my_foo);
std::cout << "static_cast: " << my_opt.value() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
constructor: 2
static_cast: 2
Run Code Online (Sandbox Code Playgroud)
在Clang 4.0.0和MSVC 2017(15.3)中.(现在让我们忽略GCC,因为在这种情况下它的行为似乎是错误的.)
为什么输出2?我期待1.尽管可以使用外部类型()的std::optional强制转换,但构造函数似乎更倾向于转换为内部类型(int)std::optional<int>.根据C++标准,这是正确的吗?如果是这样,那么标准是否有理由不希望尝试转换为外部类型?我会发现这更合理,并且可以想象如果可以转换为外部类型,则使用enable_if和is_convertible禁用ctor 来实现它.否则 …
在内部 GitLab 服务器上,有一个项目的 CI 脚本不支持多个管道的并发执行(Kubernetes 中的外部副作用)。因此,如果连续推送两次提交,且两次提交之间的时间少于第一个管道需要完成的时间,则这两个管道将同时运行,这会导致两者都失败。
在这种情况下,对 CI 运行器进行全局设置concurrent = 1(跨多个存储库使用一个 K8s 运行器)是不切实际的,因为应允许使用该运行器的其他项目的管道同时运行。
是否可以仅禁止一个项目使用 CI 并发?取消旧的管道或对新的管道进行排队都可以。
假设我有两个主题(都有两个分区和无限保留):
my_topic_amy_topic_b和一个消费者群体:
my_consumer在某些时候,它正在消耗这两个主题,但由于一些变化,它不再对 感兴趣my_topic_a,因此它停止消耗它,现在正在累积滞后:
kafka-consumer-groups.sh --bootstrap-server=kafka.core-kafka.svc.cluster.local:9092 --group my_consumer --describe
Run Code Online (Sandbox Code Playgroud)
TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
my_topic_a 0 300000 400000 100000 - - -
my_topic_a 1 300000 400000 100000 - - -
my_topic_b 0 500000 500000 0 - - -
my_topic_b 1 500000 500000 0 - - -
Run Code Online (Sandbox Code Playgroud)
这种滞后让我很恼火,因为:
因此,我想摆脱my_topic_aof的偏移量my_consumer,达到好像my_consumer从未消耗过的状态my_topic_a。
以下尝试失败:
TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
my_topic_a 0 300000 …Run Code Online (Sandbox Code Playgroud) 在以下最小示例中:
int main()
{
const int foo = 1;
const auto a = foo == 1 ? [](){return 42;} : [](){return 4;};
const auto b = foo == 1 ? [foo](){return 42;} : [foo](){return 4;};
}
Run Code Online (Sandbox Code Playgroud)
a很好 b但是不是,因为:
<source>:5:29: error: incompatible operand types ('(lambda at <source>:5:31)' and '(lambda at <source>:5:53)')
const auto b = foo == 1 ? [foo](){return 42;} : [foo](){return 4;};
^ ~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
为什么会这样?以及如何b获得预期的?
c++ ×5
c++11 ×3
apache-kafka ×1
barcode ×1
c++14 ×1
c++17 ×1
casting ×1
gitlab ×1
keras ×1
lambda ×1
loops ×1
max-pooling ×1
numpy ×1
opencv ×1
optimization ×1
python ×1
python-3.x ×1
raii ×1
scipy ×1
scope ×1
spline ×1
static-cast ×1
tensorflow ×1