我想提示用户生成一些随机数并保存到文件中.他给了我们这一部分.我们要做的部分是打开该文件,将数字转换为列表,然后找到平均值,标准偏差等,而不使用简单的内置Python工具.
我已经尝试使用open,但它给了我无效的语法(我选择的文件名是"数字",它保存到"My Documents"自动,所以我尝试open(numbers, 'r')和open(C:\name\MyDocuments\numbers, 'r')并没有一个工作).
例如,如果我们有一个numpy数组A,并且我们想要一个具有相同元素的numpy数组B.
以下(见下文)方法有什么区别?什么时候分配额外的内存,什么时候不分配?
B = AB[:] = A(和B[:]=A[:]?一样)numpy.copy(B, A)我有一个numpy数组.我想创建一个新的数组,它是每个连续三元素的平均值.因此,新阵列将是原始阵列的三分之一.
举个例子:
np.array([1,2,3,1,2,3,1,2,3])
Run Code Online (Sandbox Code Playgroud)
应该返回数组:
np.array([2,2,2])
Run Code Online (Sandbox Code Playgroud)
有人能建议一种有效的方法吗?我画的是空白.
我一直在尝试更多地了解Python的multiprocessing模块,并评估不同的流程之间的通信技术.我写的比较的性能的基准Pipe,Queue和Array(都来自multiprocessing)用于传递numpy进程之间的阵列.完整的基准可以在这里找到.以下是测试的片段Queue:
def process_with_queue(input_queue, output_queue):
source = input_queue.get()
dest = source**2
output_queue.put(dest)
def test_with_queue(size):
source = np.random.random(size)
input_queue = Queue()
output_queue = Queue()
p = Process(target=process_with_queue, args=(input_queue, output_queue))
start = timer()
p.start()
input_queue.put(source)
result = output_queue.get()
end = timer()
np.testing.assert_allclose(source**2, result)
return end - start
Run Code Online (Sandbox Code Playgroud)
我在我的Linux笔记本电脑上运行了这个测试,并获得了数组大小为1000000的以下结果:
Using mp.Array: time for 20 iters: total=2.4869s, avg=0.12435s
Using mp.Queue: time for 20 iters: total=0.6583s, avg=0.032915s
Using mp.Pipe: time …Run Code Online (Sandbox Code Playgroud) 我想使用fixtures作为pytest.mark.parametrize的参数或具有相同结果的东西.
例如:
import pytest
import my_package
@pytest.fixture
def dir1_fixture():
return '/dir1'
@pytest.fixture
def dir2_fixture():
return '/dir2'
@pytest.parametrize('dirname, expected', [(dir1_fixture, 'expected1'), (dir2_fixture, 'expected2')]
def test_directory_command(dirname, expected):
result = my_package.directory_command(dirname)
assert result == expected
Run Code Online (Sandbox Code Playgroud)
夹具参数的问题在于夹具的每个参数都会在每次使用时运行,但我不希望这样.我希望能够根据测试选择使用哪种灯具.
我有一个极性(r,theta)网格(这意味着每个单元格是一个环形区域)包含一些物理量(例如温度)的值,我想重新网格化(或重新投影或重新采样)这些值到笛卡尔网格上.有没有可以做到这一点的Python包?
我对将细胞中心的坐标从极地转换为笛卡尔并不感兴趣 - 这很容易.相反,我正在寻找一个可以实际重新网格化数据的包.
谢谢你的任何建议!
我想将元组转换为以分号分隔的字符串.简单.
tup = (1,2)
';'.join(map(str,tup))
Run Code Online (Sandbox Code Playgroud)
输出:
'1;2'
Run Code Online (Sandbox Code Playgroud)
但是,如果其中一个元组条目本身就是一个元组,我会得到这样的结果:
'1;(2, 3)'
Run Code Online (Sandbox Code Playgroud)
我不想要那个逗号,我想要一个分号,而且我也想选择括号字符.
我要这个:
'1;{2;3}'
Run Code Online (Sandbox Code Playgroud)
有一种简单的方法来深加入嵌套到任意深度的元组的元组,同时指定分离器(;在本例中"")和(在上面的例子中"{"和"}")的parenthes?
请注意,我不想要这个,这个问题被标记为重复:
'1,2,3'
Run Code Online (Sandbox Code Playgroud)
我还需要处理带逗号的字符串,所以我不能使用replace:
flatten((1,('2,3',4)))
'1;{2,3;4}'
Run Code Online (Sandbox Code Playgroud) 我需要通过阈值化3D数据阵列来创建布尔掩码:数据小于可接受下限的位置处的掩码或大于可接受上限的数据必须设置为True(否则False).简洁:
mask = (data < low) or (data > high)
Run Code Online (Sandbox Code Playgroud)
我有两个版本的代码用于执行此操作:一个直接使用整个3D数组,numpy而另一个方法循环遍历数组的切片.与我的期望相反,第二种方法似乎比第一种方法更快.为什么???
In [1]: import numpy as np
In [2]: import sys
In [3]: print(sys.version)
3.6.2 |Continuum Analytics, Inc.| (default, Jul 20 2017, 13:14:59)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
In [4]: print(np.__version__)
1.14.0
In [5]: arr = np.random.random((10, 1000, 1000))
In [6]: def method1(arr, low, high):
...: """ Fully vectorized computations """
...: out = np.empty(arr.shape, dtype=np.bool)
...: np.greater_equal(arr, high, out)
...: np.logical_or(out, …Run Code Online (Sandbox Code Playgroud) 有没有更简单的命令来计算矢量投影?我改为使用以下内容:
x = np.array([ 3, -4, 0])
y = np.array([10, 5, -6])
z=float(np.dot(x, y))
z1=float(np.dot(x, x))
z2=np.sqrt(z1)
z3=(z/z2**2)
x*z3
Run Code Online (Sandbox Code Playgroud) 我想使用 Python 计算(任意数量)线段的长度。我使用了以下代码,但我遇到元组不能作为操作数的减法。我怎样才能克服它?我想知道我是否遗漏了任何重要的 Python 概念。
from itertools import starmap
import math
class Point(object):
def __init__(self,x,y):
self.x=x
self.y=y
def move(self,dx,dy):
self.x+=dx
self.y+=dy
class LineString(object):
def __init__(self,*args): # A method with any number of arguments, args
self.args=[Point(*args) for p in args] # A list of Points
def length(self):
pairs=zip(self.args, self.args[1:])
return sum(starmap(distance,pairs))
def distance(p1, p2):
a = p1.x,p1.y
b = p2.x,p2.y
print (math.sqrt((a[0]-b[0])**2-(a[1]-b[1])**2))
# calculates distance between two given points p1 and p2
return math.sqrt((a** 2)+ (b** 2))
if __name__ == '__main__': …Run Code Online (Sandbox Code Playgroud)