簡體   English   中英

數據類型為numpy.ndarray但預期為numpy.int64

[英]data type is numpy.ndarray but expected numpy.int64

我正在使用python為我的計算攝影類分配作業。 該方法需要一個numpy.int64 ,我不確定該怎么做。 我嘗試了numpy.astype(int64)但是它給了我一個未定義的全局名稱。 我不確定該怎么做。 我得到的錯誤是ValueError: Error - x_locs values have type <type 'numpy.ndarray'>. Expected value type is <type 'numpy.int64'> ValueError: Error - x_locs values have type <type 'numpy.ndarray'>. Expected value type is <type 'numpy.int64'>

def getYXLocations(image, intensity_value):
    """ This function gets the Y, X locations of an image at a certain intensity
    value.

    It's easy to describe how to do this visually. Imagine you have a grayscale
    image that is 4x4.
    my_image = [ [ 17, 200,  48,  10],
                 [ 98, 151,  41, 182],
                 [128, 190,  98, 209],
                 [249,  27, 129, 182]]

    Now assume I ask you to return getYXLocations(my_image, 98)

    You have to return the y and x locations of where 98 appears, so in this
    case, 98 appears at 1, 0 and at 2, 2, so your function should return
    y_locs = [1, 2] and x_locs = [0, 2].

    Hint: There is a numpy function that will essentially allow you to do this
    efficiently & succintly. May be worth looking into ;).

    The less efficient but equally valid way of doing this:
    1. Iterate through the rows (y) and columns (x) of the image.
    2. if image[y, x] == intensity_value, append y to y_locs and x to x_locs.
    3. return y_locs, x_locs.

    Args:
        image (numpy.ndarray): Input grayscale image.
        intensity_value (numpy.uint8): Assume a value from 0->255.

    Returns:
        y_locs (numpy.ndarray): Array containing integer values for the y
                                locations of input intensity. Type np.int64.
        x_locs (numpy.ndarray): Array containing integer values for the x
                                locations of input intensity. Type np.int64.
    """
    # WRITE YOUR CODE HERE.

    # dim of the image
    dim = image.shape

    # axis
    xax = dim[1]
    yax = dim[0]

    count = 0
    # loopings for count
    for x in range (0, xax):
        for y in range (0, yax):
            if image[x][y] == intensity_value:
                count = count + 1

    # creates x loc and y loc
    x_locs = np.empty([1, count], dtype=np.int64)
    y_locs = np.empty([1, count], dtype=np.int64)

    # loops for location
    place = 0
    for x in range (0, xax):
        for y in range (0, yax):
            if image[x][y] == intensity_value:
                x_locs[0][place] = x
                y_locs[0][place] = y
                place = place + 1

    print np.array_str(x_locs)
    print np.array_str(y_locs)

    # x_locs = x_locs.astype(int64)
    # y_locs = y_locs.astype(int64)

    return x_locs, y_locs
    # END OF FUNCTION

這是測試該方法是否有用的代碼。

def test_getYXLocations():

    test_input = np.array([[ 41, 200,  190,  41],
                           [ 98, 151,   41, 182],
                           [128, 190,   98, 209],
                           [ 41,  27,  129, 190]], dtype=np.uint8)
    test_intensities = [41, 190, 98, 182]

    test_answers = [[np.array([0, 0, 1, 3], dtype=np.int64),
                     np.array([0, 3, 2, 0], dtype=np.int64)], # 41
                    [np.array([0, 2, 3], dtype=np.int64),
                     np.array([2, 1, 3], dtype=np.int64)], # 190
                    [np.array([1, 2], dtype=np.int64),
                     np.array([0, 2], dtype=np.int64)], # 98
                    [np.array([1], dtype=np.int64),
                     np.array([3], dtype=np.int64)]] # 182


    for test_idx in xrange(len(test_intensities)):
        x_locs, y_locs = assignment8.getYXLocations(test_input,
                                                     test_intensities[test_idx])
        x_ans, y_ans = test_answers[test_idx]


        # Test type.
        if type(x_locs) != type(x_ans):
            raise ValueError(
                ("Error - x_locs has type {}." +
                 " Expected type is {}.").format(type(x_locs), type(x_ans)))
        if type(x_locs[0]) != type(x_ans[0]):
            raise ValueError(
                ("Error - x_locs values have type {}." +
                 " Expected value type is {}.").format(type(x_locs[0]),
                                                       type(x_ans[0])))

        # Test length (did you find the right amount of points).
        if len(x_locs) != len(x_ans):
            raise ValueError(
                ("Error - x_locs has len {}." +
                 " Expected len is {}.").format(len(x_locs), len(x_ans)))

        if len(x_locs) != len(y_locs):
            raise ValueError(
                ("The length of your outputs is not the same." +
                 "x_locs length: {} | y_locs length: {}.").format(len(x_locs),
                                                                  len(y_locs)))


        np.testing.assert_array_equal(y_locs, y_ans)
        np.testing.assert_array_equal(x_locs, x_ans)

    print "getYXLocations testing passed."
    return True

我相信問題出在這張支票上-

if type(x_locs[0]) != type(x_ans[0]):
        raise ValueError(
            ("Error - x_locs values have type {}." +
             " Expected value type is {}.").format(type(x_locs[0]),
                                                   type(x_ans[0])))

test_answers可以看到, x_ans[0]的類型為np.int64 ,因為x_ans是一維數組。

但是,當您在其他函數中創建x_locs時, x_locs其創建為2D數組,形狀為(1,10),因此,當您訪問x_locs[0] ,您會得到一個數組。

一些可能適合您的解決方案-

  1. 您要定義x_locsy_locs僅具有1里面陣列(當時具有count元素的量),也許你應該將它們定義為一維數組,而不是二維數組(我可以看到,在您的測試功能,你正在做大量的假設它們是一維數組,但實際上不是。 要將它們更改為一維數組,請將以下行更改為-

     x_locs = np.empty([count], dtype=np.int64) y_locs = np.empty([count], dtype=np.int64) 

    請注意,在此之后,當您嘗試訪問數組時,應該使用x_locs[place]而不是x_locs[0][place]

  2. 另一種解決方案是更正您的測試邏輯,以將x_locsy_locs處理為2D數組,而不是1D數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM