第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

函數(shù)認(rèn)為我在浮動(dòng)

函數(shù)認(rèn)為我在浮動(dòng)

寶慕林4294392 2021-04-09 16:27:34
我想計(jì)算兩個(gè)數(shù)組中所有坐標(biāo)對(duì)之間的距離。這是我寫的一些代碼:def haversine(x,y):    """    Calculate the great circle distance between two points     on the earth (specified in decimal degrees)    """    # convert decimal degrees to radians     print(type(x))    lat1, lon1 = np.radians(x)    lat2, lon2 = np.radians(y)    # haversine formula     dlon = lon2 - lon1     dlat = lat2 - lat1     a = np.sin(dlat/2)**2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon/2)**2    c = 2 * np.arcsin(np.sqrt(a))     r = 6371 # Radius of earth in kilometers. Use 3956 for miles    return c * rhaversine = np.vectorize(haversine)數(shù)組是gas_coords和postal_coords。注意type(postal_coords)>>>numpy.ndarraytype(gas_coords)>>>numpy.ndarray并且每個(gè)數(shù)組都有兩列。當(dāng)我嘗試計(jì)算距離時(shí)using scipy.spatial.distance.cdist,出現(xiàn)以下錯(cuò)誤:in haversine(x, y)      6     # convert decimal degrees to radians      7     print(type(x))---->; 8     lat1,lon1 =np.radians(x)      9     lat2,lon2 = np.radians(y)     10 TypeError: 'numpy.float64' object is not iterablehaversine似乎認(rèn)為輸入x是浮點(diǎn)數(shù)而不是數(shù)組。即使當(dāng)我將數(shù)組傳遞給haversine類似haversine(np.zeros(2),np.zeros(2))的對(duì)象時(shí),也會(huì)出現(xiàn)同樣的問題。我應(yīng)該注意,這僅在通過進(jìn)行矢量化后發(fā)生np.vectorize。從來看haversine,參數(shù)不會(huì)以任何方式改變。是什么原因引起的錯(cuò)誤?這是一個(gè)最小的工作示例:import numpy as npfrom scipy.spatial.distance import cdistgas_coords = np.array([[50, 80], [50, 81]])postal_coords = np.array([[51, 80], [51, 81]])cdist(postal_coords, gas_coords, metric = haversine)>>>array([[ 111.19492664,  131.7804742 ],          [ 131.7804742 ,  111.19492664]])
查看完整描述

1 回答

?
Qyouu

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超11個(gè)贊

給定所需的輸出,可以通過不對(duì)haversine函數(shù)進(jìn)行向量化來避免錯(cuò)誤,因?yàn)檫@會(huì)將標(biāo)量傳遞給函數(shù)(如上面的注釋所述)。因此,您可以致電cdist:


import numpy as np

from scipy.spatial.distance import cdist


def haversine(x, y):

    """

    Calculate the great circle distance between two points 

    on the earth (specified in decimal degrees)

    """

    # convert decimal degrees to radians 

    print(type(x))

    lat1, lon1 = np.radians(x)

    lat2, lon2 = np.radians(y)


    # haversine formula 

    dlon = lon2 - lon1 

    dlat = lat2 - lat1 

    a = np.sin(dlat/2)**2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon/2)**2

    c = 2 * np.arcsin(np.sqrt(a)) 

    r = 6371 # Radius of earth in kilometers. Use 3956 for miles

    return c * r


gas_coords = np.array([[50, 80], [50, 81]])

postal_coords = np.array([[51, 80], [51, 81]])


cdist(postal_coords, gas_coords, metric=haversine)


查看完整回答
反對(duì) 回復(fù) 2021-04-27
  • 1 回答
  • 0 關(guān)注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)