小编Aae*_*Aae的帖子

在numpy数组上使用inplace操作时生成的TypeError?

如果我运行以下代码:

import numpy as np

b = np.zeros(1)
c = np.zeros(1)
c = c/2**63

print b, c
b += c
Run Code Online (Sandbox Code Playgroud)

我收到此错误消息:

TypeError: ufunc 'add' output (typecode 'O') could not be coerced to provided
output parameter (typecode 'd') according to the casting rule ''same_kind''
Run Code Online (Sandbox Code Playgroud)

如果我b += c改为b = b + c,代码运行正常.为什么会这样?我在RHEL上运行Python 2.7.2.

NumPy版本:2.0.0.dev-a2a9dfb

GCC版本:4.1.2 20080704(Red Hat 4.1.2-52)

先感谢您.

python arrays numpy typeerror

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

使用列表中的随机元素创建numpy数组

有没有一种有效的方法来创建一个任意长的numpy数组,其中每个维度由从长度> = n的列表中提取的n个元素组成?列表中的每个元素只能为每个维度绘制一次.

例如,如果我有列表l = ['cat', 'mescaline', 'popcorn'],我希望能够,例如通过键入类似的东西np.random.pick_random(l, (3, 2), replace=false),创建一个数组array([['cat', 'popcorn'], ['cat', 'popcorn'], ['mescaline', 'cat']]).

谢谢.

python arrays random numpy list

3
推荐指数
2
解决办法
5767
查看次数

对方法的每个输出进行单独的单元测试

我有一个方法将文件作为输入,然后根据此文件返回N个输出。

我想通过以下方式测试此方法:假设我们有M个文件要测试。对于每个文件,我想向测试程序(或单独的文件)添加一行,该行由文件路径和N个预期输出组成。此数据应产生N * M个单独的测试,每对文件和预期输出一个。

是否有实现此目标的好方法?我希望每个测试运行最多解析一次每个文件。

以下是执行我想要的示例。如您所见,我必须为每个文件添加单独的测试类。我希望找到一个解决方案,在其中我可以仅添加带有测试数据的行(例如testData.Add(("thirdfile", 4), (348, 312));)来测试新文件。

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }

    public static class FileParser
    {
        private static int n = 0;

        public static void Init(int parameter)
        {
            n = parameter;
        }

        public static (int output1, int output2) ParseFile(string filename)
        {
            return (filename[0] * n, filename[1] * n);
        }
    }

    public class Tests
    {
        private Dictionary<(string, int), (int, int)> testData;

        public Tests()
        { …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing mstest xunit xunit.net

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

标签 统计

arrays ×2

numpy ×2

python ×2

c# ×1

list ×1

mstest ×1

random ×1

typeerror ×1

unit-testing ×1

xunit ×1

xunit.net ×1