小编Tob*_*ann的帖子

在样条拟合1d数据中找到拐点

我有一些一维数据,并与样条拟合.然后我想在其中找到拐点(忽略鞍点).现在我通过在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)

python numpy spline scipy

5
推荐指数
1
解决办法
3748
查看次数

具有多个函数参数的点自由符号

我正在尝试移植以下 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)

pointfree elm

5
推荐指数
1
解决办法
944
查看次数

获取列表中k个元素的所有可能组合

我需要一个与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)

有更优雅和有效的解决方案吗?

haskell combinatorics

5
推荐指数
2
解决办法
1万
查看次数

透视扭曲条形码的分割

有些图像中有透视扭曲的条形码.

在此输入图像描述

它们使用ZBar定位和解码.

zbar条形码位置

现在我不仅需要粗略的位置,而且需要条形码的四个真实角点,它们定义了封闭的4点多边形.

在此输入图像描述

我尝试了不同的方法,但还没有得到理想的结果.其中一个是:

  • 将图像转换为灰度
  • 阈值图像
  • 侵蚀图像
  • floodFill以一个已知为条形码一部分的像素开始
  • 获取floodFill结果周围的轮廓

但是在这个轮廓周围,我现在需要找到最小的最佳拟合4点多边形,这似乎并不那么容易.

你对更好的方法有什么想法吗?

opencv barcode image-segmentation

5
推荐指数
1
解决办法
444
查看次数

将std :: function <int(int)>赋值给std :: function <const int&(const int&x)>

以下代码编译但在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++ c++11

5
推荐指数
1
解决办法
127
查看次数

C++中的循环融合(如何帮助编译器?)

我试着理解在什么情况下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)

c++ optimization loops compiler-optimization

5
推荐指数
1
解决办法
822
查看次数

为什么在迭代数组时,`while`的性能比`for`慢?

第二版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不是这样的?

performance rust

5
推荐指数
1
解决办法
168
查看次数

最大池和平均池的混合

在使用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)

既然看起来好像没有现成的东西,那么如何以有效的方式实现它呢?

python-3.x keras tensorflow max-pooling spatial-pooling

5
推荐指数
1
解决办法
928
查看次数

成员函数装饰器和自我参数

以下关于成员函数的装饰器的最小示例:

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对象.

我怎样才能在那里访问它?

python decorator member-functions wrapper python-3.x

5
推荐指数
1
解决办法
197
查看次数

如何使 pylint 报告未使用的返回值

到下面的代码:

"""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 pylint python-3.x

5
推荐指数
1
解决办法
699
查看次数