小编use*_*754的帖子

如何将3D Python/NumPy数组保存为文本文件?

我必须启动大量的计算,每次都要保存一个2D文件文本,所以我想将结果"实时"存储为3D文本文件,每个切片对应一个计算结果.

第一次计算是可以的,但是当我进行第二次计算时,在"np.loadtxt"步骤中,数组尺寸变为2D ......所以我无法实现我的目标......我无法进行重塑当我开始尺寸(...,...,1)

#MY FIRST RESULTS
test1 = open("C:/test.txt", "r")
test_r = np.genfromtxt(test, skip_header=1)
test_r = np.expand_dims(test_r, axis=2) #I create a new axis to save in 3D
test1.close()

#I test if the "Store" file to keep all my results is created.
try:
    Store= np.loadtxt('C:/Store.txt')
except:
    test=1

#If "Store" is not created, I do it or I concatenate in my file.
if test ==1:
    Store= test_r
    np.savetxt('C:/Store.txt', Store)
    test=2
else:
    Store = np.concatenate((Store,test_r), axis=2)
    np.savetxt('C:/Store.txt', Store)


#MY SECOND RESULTS
test2 = …
Run Code Online (Sandbox Code Playgroud)

python arrays text numpy concatenation

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

用于循环使用np.arctan2

我想在一个循环中使用np.arctan2,因为我有大量的切片要考虑(所以我有3D数组),但我有一个错误:"无效的参数数量"但我在二维数组上工作,因为我使用环...

import numpy as np

Lx=500.
Ly=400.

x0 = Lx/2. 
y0 = Ly/2.

#stockage des valeurs de x0 servant au calcul de x0 optimal
stockx0 = []
for i in range(0,300,1):
    stock = Lx/2. + i
    stockx0.append(stock)

stockx0 = np.array(stockx0)
stockx0 = stockx0[np.newaxis,:]

YA, XA = np.mgrid[0:Ly, 0:Lx]

XA = XA[:, :, np.newaxis]*np.ones((XA.shape[0],XA.shape[1],stockx0.shape[1]))

YA = YA[:, :, np.newaxis]*np.ones((XA.shape[0],XA.shape[1],stockx0.shape[1]))

XA2 = []

for i in range(XA.shape[2]):
    stock = XA[:,:,i] - stockx0[0,i]
    XA2.append(stock)

XA2 = np.array(XA2)

YA = YA - …
Run Code Online (Sandbox Code Playgroud)

python for-loop trigonometry numpy

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

Python - 掩盖多维

我想掩盖一些数组的值.阵列是3D,掩模是2D.

我想掩饰所有的coordonates方向frametemperature_reshape.shape[0].

我尝试了以下循环:

for i in range(frametemperature_reshape.shape[0]):
    frames_BPnegl = np.ma.array(frametemperature_reshape[i,:,:], mask=mask2)
Run Code Online (Sandbox Code Playgroud)

python numpy mask

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

Python-找到最接近的位置和值

我试图找到给定的 X 和 Y 的最接近点来访问其值。就我而言,在 X 方向 (np.arrange(0,X.max(),1) 上,我想从 Y = 0 中提取最接近的值以获取“值数组”中的值:

import numpy as np
import matplotlib.pyplot as plt
#My coordinates are given here :
X = np.array([0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4 ,5, 0, 1, 2, 3, 4 ,5])
Y = np.array([-2.5, -1.5, 0, 0, 2, 2.5, 2, 1.5, 1, -1, -1.2,-2.5, 0.2, 0.5, 0, -0.5, -0.1,0.05])
plt.scatter(X,Y);plt.show()
#The corresponding values are :
values = np.array([-1.1, -9, 10, 10, 20, 25, 21, …
Run Code Online (Sandbox Code Playgroud)

python indexing numpy

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

标签 统计

numpy ×4

python ×4

arrays ×1

concatenation ×1

for-loop ×1

indexing ×1

mask ×1

text ×1

trigonometry ×1