我試圖為每個向量縮放三個不同比例的三個向量,然后找到向量的線性組合以找到一個點。
向量是等邊三角形的三個角,尺度隨機選取。
w_list = np.zeros(3)
sum = 0
for i in range(3):
w_list[i] = np.random.random_sample()
sum = w_list[i]
w = w_list/sum # scaling weights
corners = [(0, 0), (1, 0), (0.5, np.sqrt(3/4))]
x = 0, 0
for i, e in zip(w, corners): # w contains the three scales
x = (i * e[0], i * e[1])
但是,當我列印 x 時,我得到一個帶有 7 個點的向量,而它應該只列印一個點。
(0, 0, 0.0, 0.0, 0.4682062316923167, 0.0, 0.15138058449552882, 0.26219886362572936)
uj5u.com熱心網友回復:
雖然對于解決您的問題不是絕對必要的,但習慣于充分發揮 numpy 的潛力可能會對您有所幫助。它真的非常強大,可以將您的所有計算速度提高一百倍。
import numpy as np
corners = np.array([(0, 0), (1, 0), (0.5, np.sqrt(3/4))])
weights = np.random.random(3)
weights /= np.sum(weights) # normalization
x = np.dot(weights, corners)
uj5u.com熱心網友回復:
嘗試以下(使用 list 和 tuple as x)
import numpy as np
corners = [(0, 0), (1, 0), (0.5, np.sqrt(3/4))]
w = [1,2,3]
x = [0, 0]
for i, e in zip(w, corners): # w contains the three scales
x[0]= x[0] i * e[0]
x[1]= x[1] i * e[1]
print(x)
輸出
[3.5, 2.598076211353316]
uj5u.com熱心網友回復:
您目前正在執行元組連接而不是向量加法(請注意,您問題中的最后一個元組有 8 個而不是 7 個數字)。相反,讓你x的 numpy 陣列:
import numpy as np
w_list = np.zeros(3)
sum = 0
for i in range(3):
w_list[i] = np.random.random_sample()
sum = w_list[i]
w = w_list/sum # scaling weights
corners = [(0, 0), (1, 0), (0.5, np.sqrt(3/4))]
x = np.array([0.0, 0.0]) #don't want an int array
for i, e in zip(w, corners): # w contains the three scales
x = (i * e[0], i * e[1])
print(x) #[0.25691217 0.4105851 ]
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/350270.html
上一篇:十進制最大值的驗證未正確激活
