小编tob*_*gue的帖子

如何获得scikit-learn分类器的最丰富的功能?

liblinear和nltk等机器学习包中的分类器提供了一种方法show_most_informative_features(),它对调试功能非常有用:

viagra = None          ok : spam     =      4.5 : 1.0
hello = True           ok : spam     =      4.5 : 1.0
hello = None           spam : ok     =      3.3 : 1.0
viagra = True          spam : ok     =      3.3 : 1.0
casino = True          spam : ok     =      2.0 : 1.0
casino = None          ok : spam     =      1.5 : 1.0
Run Code Online (Sandbox Code Playgroud)

我的问题是,如果在scikit-learn中为分类器实现类似的东西.我搜索了文档,但找不到类似的东西.

如果还没有这样的功能,有人知道如何获得这些值吗?

非常感谢!

python classification machine-learning scikit-learn

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

如何检查numpy矩阵列中的所有值是否相同?

我想检查numpy数组/矩阵的列中的所有值是否相同.我试图用reduce的的ufunc equal,但它似乎并没有在所有情况下的工作:

In [55]: a = np.array([[1,1,0],[1,-1,0],[1,0,0],[1,1,0]])

In [56]: a
Out[56]: 
array([[ 1,  1,  0],
       [ 1, -1,  0],
       [ 1,  0,  0],
       [ 1,  1,  0]])

In [57]: np.equal.reduce(a)
Out[57]: array([ True, False,  True], dtype=bool)

In [58]: a = np.array([[1,1,0],[1,0,0],[1,0,0],[1,1,0]])

In [59]: a
Out[59]: 
array([[1, 1, 0],
       [1, 0, 0],
       [1, 0, 0],
       [1, 1, 0]])

In [60]: np.equal.reduce(a)
Out[60]: array([ True,  True,  True], dtype=bool)
Run Code Online (Sandbox Code Playgroud)

为什么第二种情况下的中间列也要评估True,而它应该是False

谢谢你的帮助!

python numpy matrix

28
推荐指数
2
解决办法
4万
查看次数

直接绑定到服务属性有什么危害?

创建服务的方法是否有任何损害,该服务包含我的角度应用程序的某些状态,将该服务注入到状态调节的组件中,然后在其模板中直接绑定到该服务的相应属性?

我经常看到主题用于触发组件之间的状态更新,并且想知道是否存在上述方法不起作用且必须使用主题的情况.

angular

6
推荐指数
1
解决办法
94
查看次数

迭代Python文件对象线程是否安全?

在寻找这个问题的一个简洁的解决方案时,我想知道迭代Python文件对象是否是线程安全的.

from concurrent.futures import ThreadPoolExecutor
import sys, time

f = open("big_file")

def worker():
    running = True
    while running:
        try:
            line = next(f)
        except StopIteration:
            return
        # process line
        time.sleep(3)
        sys.stdout.write(line + "\n")

no_workers = 4
with ThreadPoolExecutor(max_workers=no_workers) as e:
    for _ in range(no_workers):
        e.submit(worker)

f.close()
Run Code Online (Sandbox Code Playgroud)

我的问题是,如果上面的示例是安全的,或者如果没有,那么获取线程安全文件对象的简单方法是什么(用于逐行读取文件,不需要写入).

python multithreading

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

如何在C++中有效地构建Python字典

出于性能原因,我想将部分python程序移植到C++中,因此我尝试为程序编写一个简单的扩展.C++部分将构建一个字典,然后需要将其传递给Python程序.

我发现的一种方法似乎是在C++中构建我的类似dict的对象,例如a boost::unordered_map,然后使用Py_BuildValue[1]方法将其转换为Python ,该方法能够生成Python dicts.但是这种方法包括将容器转换为字符串表示并返回看起来有点太多"即将到来"才能成为最高性能的解决方案!?

所以我的问题是:在C++中构建Python字典的最高效方法什么?我看到boost有一个Python库,它支持在C++和Python之间映射容器,但到目前为止我没有在文档中找到我需要的东西.如果有这样的方式我宁愿在C++中直接构建Python dict,那么就不需要复制等.但如果最有效的方法是另一种方式,我也很擅长.

这是(简化的)C++ - 我编译成.dll/.pyd的代码:

#include <iostream>
#include <string>
#include <Python.h>
#include "boost/unordered_map.hpp"
#include "boost/foreach.hpp"

extern "C"{
typedef boost::unordered_map<std::string, int> hashmap;

static PyObject*
_rint(PyObject* self, PyObject* args)
{
    hashmap my_hashmap; // DO I NEED THIS?
    my_hashmap["a"] = 1; // CAN I RATHER INSERT TO PYTHON DICT DIRECTLY??
    BOOST_FOREACH(hashmap::value_type i, my_hashmap) {
            // INSERT ELEMENT TO PYTHON DICT
    }
    // return PYTHON DICT
}

static PyMethodDef TestMethods[] = …
Run Code Online (Sandbox Code Playgroud)

c++ python performance dictionary python-extensions

4
推荐指数
1
解决办法
4926
查看次数