小编Chi*_*xus的帖子

空切片的平均值和自由度<= 0

下面的代码假设为完整的高斯高斯(http://courses.ee.sun.ac.za/Pattern_Recognition_813/lectures/lecture03/node2.html)运行Bayes分类器,但是当我运行码。他们是:

RuntimeWarning: Mean of empty slice.
  warnings.warn("Mean of empty slice.", RuntimeWarning)
Run Code Online (Sandbox Code Playgroud)

RuntimeWarning: Degrees of freedom <= 0 for slice
  warnings.warn("Degrees of freedom <= 0 for slice", RuntimeWarning)
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

def modelFull(train, test):
    err_train = 0
    err_test = 0
    x_train = []
    x_test = []
    labels = []
    train_labels = []
    test_labels = []
    for i in train:
        x_train.append(i[:-1]/255)
        labels.append(i[-1])
        train_labels.append(i[-1])
    for i in test:
        x_test.append(i[:-1]/255)
        labels.append(i[-1])
        test_labels.append(i[-1])
    x_train = np.array(x_train)
    x_0 = []
    x_1 = []
    for i …
Run Code Online (Sandbox Code Playgroud)

python numpy gaussian naivebayes

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

如何制作具有多个列表的词典字典

有一个索引列表将是父词典的关键字:

index = [1,2,3]
Run Code Online (Sandbox Code Playgroud)

然后多个列表将是孩子的词组:

triangles = [4,5,6]
circles = [7,8,9]
squares = [10,11,12]
Run Code Online (Sandbox Code Playgroud)

顺序元素是数据,导致:

{1:{'triangles':4, 'circles':7, 'squares': 10},
 2: {'triangles': 5, 'circles': 8, 'squares': 11},
 3: {'triangles': 6, 'circles': 9, 'squares': 12}}
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点 ?

你认为熊猫更容易吗?

python dictionary

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

反转32位整数

我试图解决的exercie leetcode.com与交易signed 32bit integers.

任务是:

返回带符号的32位整数的倒数,如果溢出32位有符号整数的范围则返回0.

维基百科:

32位寄存器可以存储32个不同的值.可以以32位存储的整数值范围取决于所使用的整数表示.使用两个最常见的表示,范围是0到4,294,967,295(2 ^ 32 - 1)表示为(无符号)二进制数,而-2,147,483,648(-2 ^ 31)到2,147,483,647(2 ^ 31 - 1)表示作为两个补充.

所以,如果我理解的是正确的,我应该在间隔之间进行测试0 to (2^31)-1,(-2^31) to 0否则,返回0.

这是我的代码:

def reverse_int(nums):
    a = str(nums)

    if 0 < nums <= (1 << 31)-1:
        return int(a[::-1])

    elif (-1 << 31) <= nums < 0:
        return -(int(a[:-len(a):-1]))
    else:
        return 0
Run Code Online (Sandbox Code Playgroud)

这是我的问题:当我在网站上测试我的代码时:

nums = 1534236469 # Fail
nums = 1463847412 # Success
nums = 9000000    # Success
Run Code Online (Sandbox Code Playgroud)

为什么我当前的代码失败 …

python integer 32-bit

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

ffmpeg通过python子进程无法找到摄像头

奇怪的问题,我使用此命令通过ffmpeg(通过Windows上的cmd)捕获我的网络摄像头:

ffmpeg -y -t 300 -rtbufsize 1024M -f dshow -i video="Lenovo EasyCamera" -c:v libx264 -preset veryslow -crf 25 Desktop.mkv
Run Code Online (Sandbox Code Playgroud)

一切正常.但是当我通过python作为子进程尝试相同的命令时,它失败了.这是python代码:

from subprocess import Popen
cmd = ['ffmpeg', '-y', '-t', '300', '-rtbufsize', '1024M', '-f', 'dshow', '-i', 'video="Lenovo EasyCamera"', '-c:v', 'libx264', '-preset', 'veryslow', '-crf', '25', 'Desktop.mkv']
p = Popen(cmd)
Run Code Online (Sandbox Code Playgroud)

输出以下错误并冻结:

[dshow @ 00000000023a2cc0] Could not find video device with name ["Lenovo EasyCamera"] among source devices of type video.
video="Lenovo EasyCamera": I/O error
Run Code Online (Sandbox Code Playgroud)

任何人都可以解决这个问题并告诉我我做错了什么?或者它是python或子进程模块中的一些已知错误(使用python 3.6.1,但没有附加到特定版本,如果它将帮助我解决这个问题)?

提前致谢!

PS这个问题是对这个问题的一个跟进,如果这是相关的:如何在Windows中使用ffmpeg抓取笔记本电脑摄像头视频

python subprocess directshow ffmpeg

3
推荐指数
1
解决办法
473
查看次数

如何在移位整数时绕过Go int64值限制?

我一起去试图得到的值KiB, MiB, ..., ZiB, Yib分别是KibiByte, MebiByte, ..., ZebiByte, YobiByte.

我在Golang的代码是:

package main 
import ( 
    "fmt"
)

func main() {
    s := []string{"KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}

    for k,v := range(s) {
        fmt.Printf("%s: %v\n", v, 1 << uint64(10 * (k+1)))
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,ZiB and YiB溢出的值Go uint64和这就是为什么我有这个输出:

KiB: 1024
MiB: 1048576
GiB: 1073741824
TiB: 1099511627776         // exceeds 1 << 32
PiB: 1125899906842624
EiB: 1152921504606846976
ZiB: 0                    // exceeds 1 << 64
YiB: …
Run Code Online (Sandbox Code Playgroud)

python go byte-shifting

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

如何将 re.split 用于逗号和句点?

我有多个字符串,其中单词用逗号或句点分割:

string = ['apple,pear,grapes,carrot.cabbage,veggies.fruit,yard']
Run Code Online (Sandbox Code Playgroud)

我想根据逗号和句点拆分它:

string = ['apple','pear','grapes','carrot','cabbage','veggies','fruit','yard']
Run Code Online (Sandbox Code Playgroud)

我只知道如何为 re.split 使用一个条件:

re.split(',',string)
Run Code Online (Sandbox Code Playgroud)

这不会拆分中间有句点的单词。如何拆分整个字符串,以便在中间有逗号或句点时拆分单词?

python regex split

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

在python中迭代两个字典

下面的代码给了我一个只有19项的mw_vs_sasa字典,即使amino_acid_dictionary和amino_acid_mw都有20个.请帮我调试一下.

  amino_acid_dictionary = {'ALA': ALA, 'ARG': ARG, 'ASN': ASN, 'ASP': ASP, 
                        'CYS': CYS, 'GLU': GLU, 'GLN': GLN, 'GLY': GLY,
                        'HIS': HIS, 'ILE': ILE, 'LEU': LEU, 'LYS': LYS, 
                        'MET': MET, 'PHE': PHE, 'PRO': PRO, 'SER': SER,
                        'THR': THR, 'TRP': TRP, 'TYR': TYR, 'VAL': VAL}

    amino_acid_mw = {'ALA': 89.09, 'ARG': 174.20, 'ASN': 132.12, 'ASP': 133.10, 
                         'CYS': 121.16, 'GLU': 147.13, 'GLN': 146.15, 'GLY': 75.07,
                         'HIS': 155.16, 'ILE': 131.18, 'LEU': 131.18, 'LYS': 146.19, 
                         'MET': 149.21, 'PHE': 165.19, 'PRO': 115.13, 'SER': 105.09,
                         'THR': 119.12, 'TRP': …
Run Code Online (Sandbox Code Playgroud)

python dictionary numpy

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