理解切片表示法是基于另外两篇文章的另一个综合问题

2024-10-06 10:34:50 发布

您现在位置:Python中文网/ 问答频道 /正文

谢谢你抽出时间。 我们已经看到了对这种结构的多维数组进行切片的全面讨论:

newvariable1 = myvariable1[start:stop]
newvariable2 = myvariable2[start:stop:step]

这些问题在Understanding slice notation进行了全面讨论。你知道吗

“开始”、“停止”和“步骤”可以是正整数或负整数,也可以是数字列表。你知道吗

我对一个更全面的切片版本感兴趣,它包括切片操作符“:”,逗号操作符“:”,以及逗号操作符之前和/或之后的变量。你知道吗

newvariable1 = myarray1[var1,var2:var3,var4]
newvariable2 = myarray2[var1,var2:var3,var4:var5,var6:var7,var8]

请注意,并非所有变量都存在于操作中。它表明变量无论是整数还是逗号“,”前后的列表,以及切片运算符前后都有显著的结果。你知道吗

我正在寻找一种系统的方法,包括逗号和vars在逗号运算符“,”前后的效果。你知道吗

我甚至尝试在我的名字“安东尼考拉”下的页面底部将切片和选择相结合。你知道吗

下面是一个例子,有一个3x3矩阵叫做“doo”。我做了我自己的工作,在玩切片和使用索引。你知道吗

doo; # We could also say doo[:]
array([[[ 1,  2,  3,  4],
        [ 5,  6,  7,  8],
        [ 9, 10, 11, 12]],

       [[13, 14, 15, 16],
        [17, 18, 19, 20],
        [21, 22, 23, 24]],

       [[25, 26, 27, 28],
        [29, 30, 31, 32],
        [33, 34, 35, 36]]])

这是一个选择和切片的例子

#Specific columns of all submatrices 3x3 -please relate this to the 3x4x3 matrix
doo[:,:,0] #First column of all submatrices
array([[ 1,  5,  9],
       [13, 17, 21],
       [25, 29, 33]])

doo[:,:,1] #Second column of all submatrices
array([[ 2,  6, 10],
       [14, 18, 22],
       [26, 30, 34]])

还有

#Specific columns of all submatrices 3x3x3
doo[:,:,[0,1,3]]; #First, second and fourth cols of all submatrices
array([[[ 1,  2,  4],
        [ 5,  6,  8],
        [ 9, 10, 12]],

       [[13, 14, 16],
        [17, 18, 20],
        [21, 22, 24]],

       [[25, 26, 28],
        [29, 30, 32],
        [33, 34, 36]]])

如果没有这些逗号,结果就不同了。你知道吗

另一个例子是在http://ataspinar.com/2018/12/21/a-guide-for-using-the-wavelet-transform-in-machine-learning/ 我不是在看关于小波变换的教程。 该示例演示了切片、逗号和整数的使用。你知道吗

test_data_cwt = np.ndarray(shape=(test_size, 127, 127, 9))
for ii in range(0,test_size):
    if ii % 100 == 0:
        print(ii)
    for jj in range(0,9):
        signal = uci_har_signals_test[ii, :, jj]; # ii before ",", jj before ","
        coeff, freq = pywt.cwt(signal, scales, waveletname, 1)
        coeff_ = coeff[:,:127]; # The purpose of the ","
        test_data_cwt[ii, :, :, jj] = coeff_ ;# ii before ",", jj before ","

请注意,切片操作涉及整数,使用逗号运算符“,”之前和之后以及切片运算符“:”之前和之后的变量。你知道吗

我要求系统/全面地应用逗号前后变量位置的重要性:

signal = uci_har_signals_test[ii, :, jj]
What is "ii" and "jj" in respect of slicing and what happens if ii were after the comma and jj were before the comma.

coeff_ = coeff[:,:127]
What is the meaning of the ","?

test_data_cwt[ii, :, :, jj] = coeff_

同样,当我问原始问题时,我想得到原始矩阵的子集时,我对数组/矩阵结构的意义很感兴趣。你知道吗

newvariable1 = myarray1[var1,var2:var3,var4]
newvariable2 = myarray2[var1,var2:var3,var4:var5,var6:var7,var8]

总而言之:

虽然我知道切片,并且已经能够解决如何使用切片和逗号“,”运算符和整数和/或列表的有限使用来获得3x3矩阵的子集,但我仍然需要帮助来更全面地理解在逗号运算符之前和之后使用数/列表的概念“,“切片运算符前后”:。你知道吗

谢谢你, 悉尼的安东尼


Tags: ofthetest列表切片运算符整数all