如圖,請問怎麼驗證reshape轉換?
import numpy as np
def reshape_check(before_array, after_array):
# before_array 被轉換的
# after_array 目標轉換成
try:
# 建立由1組成的before_array結構後,reshape至after_array
my_array = np.ones(before_array).reshape(after_array)
print(f'from {before_array} to {after_array} success')
return my_array
except Exception as e:
# 無法轉換報錯
print(e)
'''
範例1:
reshape_check((10, 20, 30),(-1, 13))
>> cannot reshape array of size 6000 into shape (13)
範例2:
reshape_check((1, 2, 3),(1, 6))
>>from (1, 2, 3) to (1, 6) success
>>array([[1., 1., 1., 1., 1., 1.]])
'''
因為你的語法錯了
# define的時候,參數接口是變數名,不能直接傳入數值
def reshape_check(before_array, after_array)
# 後面在call的時候才能丟數值進去
reshape_check((10, 20, 30),(-1, 13))
# 完整程式碼長這樣
import numpy as np
def reshape_check(before_array, after_array):
# before_array 被轉換的
# after_array 目標轉換成
try:
# 建立由1組成的before_array結構後,reshape至after_array
my_array = np.ones(before_array).reshape(after_array)
print(f'from {before_array} to {after_array} success')
return my_array
except Exception as e:
# 無法轉換報錯
print(e)
reshape_check((10, 20, 30),(-1, 13))
!看懂了! 是要先把程式碼打好,後面才丟數值進去給他檢查!!謝謝你~
建議還是要詳細了解一下shape是什麼,透過工具也是一種了解的方式,在了解了之後,以你原本的題目來說應該心算就可以知道答案了。
若有幫到你的話就選個最佳解答吧