Python3.1网格模拟问题
根据答案,我从一维数组创建了一个网格,它修改了一个值及其周围的邻居;如果在另一个数组中找到该值。主值由特定的%转换,周围的元素由另一个%转换 但是,如何确保在样本的下一次迭代期间不会再次转换更改的值 下面是示例代码。谢谢你抽出时间Python3.1网格模拟问题,python,numpy,python-3.x,Python,Numpy,Python 3.x,根据答案,我从一维数组创建了一个网格,它修改了一个值及其周围的邻居;如果在另一个数组中找到该值。主值由特定的%转换,周围的元素由另一个%转换 但是,如何确保在样本的下一次迭代期间不会再次转换更改的值 下面是示例代码。谢谢你抽出时间 import numpy as np def grid(array,samples,details): #Sides of the square (will be using a squarable number Width = (len(arr
import numpy as np
def grid(array,samples,details):
#Sides of the square (will be using a squarable number
Width = (len(array)) ** 0.5
#Convert to grid
Converted = array.reshape(Width,Width)
#Conversion details
Change = [details[1]] + [details[2]]
nrows, ncols = Converted.shape
for value in samples:
#First instance where indexing returns it
i,j = np.argwhere(Converted == value)[0]
#Prevent indexing outside the boudaries of the
#array which would cause a "wraparound" assignment
istart, istop = max(i-1, 0), min(i+2, nrows)
jstart, jstop = max(j-1, 0), min(j+2, ncols)
#Set the value within a 3x3 window to their "new_value"
for elem in Converted[istart:istop, jstart:jstop]:
Converted[elem] = elem + (elem * (value * ((Change[1]/100))
#Set the main value to the new value
Converted[i,j] = value + (value * ((Change[0])/100))
#Convert back to 1D list
Converted.tolist()
return (Converted)
a = [16,2,20,4,14,6,70,8,9,100,32,15,7,14,50,20,17,10,9,20,7,17,50,2,19,20,21,22,23,24,25]
samples = [2, 7]
grid_details = [10,50,100]
result = grid(a,samples,grid_details)
首先,让我们一节一节地浏览您的代码。。。你现在写的东西有几个问题 顺便说一句,python中有一个松散的约定,为类保留大写和CamelCased名称。(您经常会看到类似
foo=FooBar(baz)
的代码,其中foo
是类FooBar
的实例)调用变量Converted
而不是Converted
,没有什么错,但您将看到的大多数代码都使用后一种形式
让我们从这里开始:
import numpy as np
def grid(array,samples,details):
...
您稍后将以array
的形式传递列表。因此,需要将列表array
转换为numpy.ndarray
。(否则,像“代码>数组。改写”将不起作用。)您也可以考虑将变量名改为“<代码>数组< /COD>”不太通用的东西。不过,就目前而言,让我们保持变量名不变。我还将假设您需要浮点值,因为稍后将乘以百分比。。。让我们将此更改为:
import numpy as np
def grid(array,samples,details):
array = np.asarray(array, dtype=np.float)
...
继续
...
#Sides of the square (will be using a squarable number
Width = (len(array)) ** 0.5
#Convert to grid
Converted = array.reshape(Width,Width)
...
你有一道数学题!!2x2阵列有4个元素,3x3阵列有9个元素,4x4阵列有16个元素,依此类推。。。如果要将序列重塑为正方形网格,则需要取平方根,而不是除以2!让我们将其更改为:
...
#Sides of the square (will be using a squareable number)
Width = int(np.sqrt(array.size))
#Convert to grid
Converted = array.reshape(Width,Width)
...
归咎于睡眠不足。。。显然,x**0.5
与sqrt(x)
相同,我只是没有看到第二个*
。对不起
下一步:
这与只是这样做是一样的:
Change = details[1:3]
后面的部分很好,让我们跳到下一个问题:
...
#Set the value within a 3x3 window to their "new_value"
for elem in Converted[istart:istop, jstart:jstop]:
Converted[elem] = elem + (elem * (value * ((Change[1]/100))
#Set the main value to the new value
Converted[i,j] = value + (value * ((Change[0])/100))
...
首先,当您迭代它时,elem
是值,而不是索引!您不能根据获得的值对数组进行索引,否则您将尝试通过0.01
、pi
,甚至可能是像5.6+98.44j
这样的复数对数组进行索引。第二,你使用numpy是有原因的。。。没有理由像这样遍历每个元素。第三,更改中心值两次,这几乎肯定不是您想要的。相反,我们可以这样做:
...
#--Set the value within a 3x3 window to their "new_value"
# Save the "center" value for later use
center = Converted[i,j]
# Adjust the pixels around the center by a percentage
Converted[istart:istop] *= 1 + Change[1] / 100.0
# Adjust the center pixel by a different percentage
Converted[i,j] = center * (1 + Change[0] / 100.0)
...
最后,您传递的列表“数组
”的长度有问题
a = [16,2,20,4,14,6,70,8,9,100,32,15,7,14,50,20,17,10,9,20,7,17,50,2,19,20,21,22,23,24,25]
这是一个31元素的数组。。。如果不截断或添加值,就无法生成正方形。当前代码将尝试将其转换为15x15数组,这将导致错误(15x15矩阵需要255个值(15**2
)。我假设您想要一个25个元素的5x5数组。让我们将其替换为:
a = [16,2,20,4,14,6,70,8,9,100,32,15,7,14,50,20,17,10,9,20,7,17,50,2,19]
好的,让我们把所有这些建议组合成一段可行的代码:
import numpy as np
def grid(array,samples,details):
array = np.asarray(array, dtype=np.float)
#Sides of the square (will be using a squarable number
Width = int(np.sqrt(array.size))
#Convert to grid
Converted = array.reshape(Width,Width)
#Conversion details
Change = details[1:3]
nrows, ncols = Converted.shape
for value in samples:
#First instance where indexing returns it
i,j = np.argwhere(Converted == value)[0]
#Prevent indexing outside the boudaries of the
#array which would cause a "wraparound" assignment
istart, istop = max(i-1, 0), min(i+2, nrows)
jstart, jstop = max(j-1, 0), min(j+2, ncols)
#Set the value within a 3x3 window to their "new_value"
center_value = Converted[i,j]
Converted[istart:istop, jstart:jstop] *= 1 + Change[1] / 100.0
Converted[i,j] = center_value * (1 + Change[0] / 100.0)
#Convert back to 1D list
Converted.tolist()
return Converted
a = [16,2,20,4,14,6,70,8,9,100,32,15,7,14,50,20,17,10,9,20,7,17,50,2,19]
samples = [2, 7]
grid_details = [10,50,100]
result = grid(a,samples,grid_details)
print(result)
因此,这将转换原始阵列:
[[ 16. 2. 20. 4. 14.]
[ 6. 70. 8. 9. 100.]
[ 32. 15. 7. 14. 50.]
[ 20. 17. 10. 9. 20.]
[ 7. 17. 50. 2. 19.]]
进入:
好的。现在我想你最初问的是。。。在这种情况下,第二行第二列中的项将被修改两次。。。一次是由于第一行第二列中的2,一次是由于第三行第三列中的7
这是你想要避免的吗??如果是这样,您希望在这种情况下发生什么
是否希望仅在第一次匹配时对其进行修改?第二场比赛?修改了两次,但只修改了百分比的总和?你需要定义你想要发生什么
无论如何,希望这能有所帮助
编辑
如果要避免匹配新修改的值,可以在开始修改之前找到所有匹配项。例如,如果我们更改此部分:
for value in samples:
#First instance where indexing returns it
i,j = np.argwhere(Converted == value)[0]
为此:
locations = [np.argwhere(Converted == value)[0] for value in samples]
for i,j in locations:
...
它应该做你想做的。希望这是清楚的 我不想避免修改网格中以前修改过的任何值,无论是主值还是周围的值。首先感谢您!我的意思是,现在修改的值与以后的样本值不匹配。i、 e.2增加100%至4,然后样本列表中的下一个值为4。我是否需要创建某种类型的(I,j)修改位置的保留列表,以便如果找到的第一个值是以前修改过的值,它将继续移动?@jimy-请参阅编辑。(我之前的评论是不正确的……我忘记了“匹配项”在代码中的计算位置……)请注意,
**0.5
是平方根!我刚回到家测试“编辑”部分,它并没有解决修改问题。@Andrew-对,我不知何故没有看到第二个*
。。。我要怪昨天累了!:)
for value in samples:
#First instance where indexing returns it
i,j = np.argwhere(Converted == value)[0]
locations = [np.argwhere(Converted == value)[0] for value in samples]
for i,j in locations:
...