我有一些一维数据,并与样条拟合.然后我想在其中找到拐点(忽略鞍点).现在我通过在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) 我正在尝试移植以下 Haskell 代码(http://codepad.org/MMydRCxo)
foo :: Int -> Int -> Int -> Maybe Bool
foo a b c = if a == 1 then Just True else Nothing
bar :: Int -> Int -> Bool
bar b c = maybe False id $ foo 1 b c
-- point free
bar' :: Int -> Int -> Bool
bar' = ((maybe False id $) .) . foo 1
main = do
print $ bar 2 3
print $ bar' 2 …Run Code Online (Sandbox Code Playgroud) 我需要一个与python相同的功能itertools.combinations(iterable, r)
到目前为止,我想出了这个:
{-| forward application -}
x -: f = f x
infixl 0 -:
{-| combinations 2 "ABCD" = ["AB","AC","AD","BC","BD","CD"] -}
combinations :: Ord a => Int -> [a] -> [[a]]
combinations k l = (sequence . replicate k) l -: map sort -: sort -: nub
-: filter (\l -> (length . nub) l == length l)
Run Code Online (Sandbox Code Playgroud)
有更优雅和有效的解决方案吗?
以下代码编译但在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) 第二版The Rust Programming Language 声明了以下关于迭代数组的while循环:
fn main() {
let a = [10, 20, 30, 40, 50];
let mut index = 0;
while index < 5 {
println!("the value is: {}", a[index]);
index = index + 1;
}
}
Run Code Online (Sandbox Code Playgroud)
[...]它也很慢,因为编译器添加了运行时代码,通过循环对每次迭代的每个元素执行条件检查.
作为一种更有效的替代方法,您可以使用for循环并为集合中的每个项执行一些代码.
fn main() {
let a = [10, 20, 30, 40, 50];
for element in a.iter() {
println!("the value is: {}", element);
}
}
Run Code Online (Sandbox Code Playgroud)
在C++中,我希望编译器/优化器能够生成具有相同运行时性能的东西.
为什么Rust不是这样的?
在使用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)
既然看起来好像没有现成的东西,那么如何以有效的方式实现它呢?
以下关于成员函数的装饰器的最小示例:
def wrap_function(func):
def wrapper(*args, **kwargs):
print(args)
print(kwargs)
return wrapper
class Foo:
@wrap_function
def mem_fun(self, msg):
pass
foo = Foo()
foo.mem_fun('hi')
Run Code Online (Sandbox Code Playgroud)
输出:
(<__main__.Foo object at 0x7fb294939898>, 'hi')
{}
Run Code Online (Sandbox Code Playgroud)
所以,self是的一个args.
但是在使用包装类时:
class WrappedFunction:
def __init__(self, func):
self._func = func
def __call__(self, *args, **kwargs):
print(args)
print(kwargs)
def wrap_function(func):
return WrappedFunction(func)
class Foo:
@wrap_function
def mem_fun(self, msg):
pass
foo = Foo()
foo.mem_fun('hi')
Run Code Online (Sandbox Code Playgroud)
输出是:
('hi',)
{}
Run Code Online (Sandbox Code Playgroud)
因此self,在Foo对象的主体中无法访问引用该对象__call__的WrappedFunction对象.
我怎样才能在那里访问它?
到下面的代码:
"""Test with unused return value"""
from typing import List
def sorted_int_list(values: List[int]) -> List[int]:
"""Returns a new, sorted list"""
return sorted(values)
def main() -> None:
"""Ignoring value returned by sorted_int_list"""
values: List[int] = [3, 1, 2]
sorted_int_list(values) # Should create some kind or error
print(values)
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
pylint说:
Your code has been rated at 10.00/10
Run Code Online (Sandbox Code Playgroud)
但是我想让它报告未使用的返回值sorted_int_list。能做到吗?
python ×3
python-3.x ×3
c++ ×2
barcode ×1
c++11 ×1
decorator ×1
elm ×1
haskell ×1
keras ×1
loops ×1
max-pooling ×1
numpy ×1
opencv ×1
optimization ×1
performance ×1
pointfree ×1
pylint ×1
rust ×1
scipy ×1
spline ×1
tensorflow ×1
wrapper ×1