我定義了一個名爲dist的方法來計算兩個點之間的距離,我直接使用該方法時可以正確執行它。 然而,當我得到一個函數來調用它來計算兩點之間的距離,我得到 UnboundLocalError:分配之前引用局部變量「minkowski_distance」方法工作正常,直到它在函數中調用,然後UnboundLocalError
編輯 對不起,我才意識到,這個功能不工作。不過,我有另一種方法調用它沒有。我把最後一個方法底部
這是方法:
class MinkowskiDistance(Distance):
def __init__(self, dist_funct_name_str = 'Minkowski distance', p=2):
self.p = p
def dist(self, obj_a, obj_b):
distance_to_power_p=0
p=self.p
for i in range(len(obj_a)):
distance_to_power_p += abs((obj_a[i]-obj_b[i]))**(p)
minkowski_distance = (distance_to_power_p)**(1/p)
return minkowski_distance
,這是功能: (它基本上拆分元組x和y到他們的數字和字符串組成部分和計算距離x和y,然後將串部分之間的距離的數字部分之間,然後將它們相加。
def total_dist(x, y, p=2, q=2):
jacard = QGramDistance(q=q)
minkowski = MinkowskiDistance(p=p)
x_num = []
x_str = []
y_num = []
y_str = []
#I am spliting each vector into its numerical parts and its string parts so that the distances
#of each part can be found, then summed together.
for i in range(len(x)):
if type(x[i]) == float or type(x[i]) == int:
x_num.append(x[i])
y_num.append(y[i])
else:
x_str.append(x[i])
y_str.append(y[i])
num_dist = minkowski.dist(x_num,y_num)
str_dist = I find using some more steps
#I am simply adding the two types of distance to get the total distance:
return num_dist + str_dist
class NearestNeighbourClustering(Clustering):
def __init__(self, data_file,
clust_algo_name_str='', strip_header = "no", remove = -1):
self.data_file= data_file
self.header_strip = strip_header
self.remove_column = remove
def run_clustering(self, max_dist, p=2, q=2):
K = {}
#dictionary of clusters
data_points = self.read_data_file()
K[0]=[data_points[0]]
k=0
#I added the first point in the data to the 0th cluster
#k = number of clusters minus 1
n = len(data_points)
for i in range(1,n):
data_point_in_a_cluster = "no"
for c in range(k+1):
distances_from_i = [total_dist(data_points[i],K[c][j], p=p, q=q) for j in range(len(K[c]))]
d = min(distances_from_i)
if d <= max_dist:
K[c].append(data_points[i])
data_point_in_a_cluster = "yes"
if data_point_in_a_cluster == "no":
k += 1
K[k]=[data_points[i]]
return K
只有當'len(obj_a)== 0'時,你纔會遇到問題,因爲你不會在'for'循環中設置'minkowski_distance'。 – Blender
你可以粘貼實際的回溯....你得到 – avasal
'str_dist =我發現使用一些更多的步驟'這條線會引發一個錯誤 – avasal