2.9 KiB
2.9 KiB
import numpy as np
import pandas as pd
import random
from numpy.linalg import norm
dimensions=[1,2,3]+[10*i for i in range(1,10)]
nb_vectors=10000
trials=100
k=1 # by setting k=1 we want to check how often the closest vector to the avarage of 2 random vectors is one of these 2 vectors
result=[]
for dimension in dimensions:
vectors=np.random.normal(0,1,size=(nb_vectors, dimension))
successes=0
for i in range(trials):
i1,i2=random.sample(range(nb_vectors),2)
target=(vectors[i1]+vectors[i2])/2
distances=pd.DataFrame(enumerate(np.dot(target, vectors.transpose())/norm(target)/norm(vectors.transpose(), axis=0)))
distances=distances.sort_values(by=[1], ascending=False)
if (i1 in (list(distances[0][:k]))) | (i2 in (list(distances[0][:k]))):
successes+=1
result.append(successes/trials)
[f'dimensions: {i}, cases when observation is the nearest: {100*round(j,3)}%' for i,j in zip(dimensions, result)]
['dimensions: 1, cases when observation is the nearest: 0.0%', 'dimensions: 2, cases when observation is the nearest: 0.0%', 'dimensions: 3, cases when observation is the nearest: 1.0%', 'dimensions: 10, cases when observation is the nearest: 8.0%', 'dimensions: 20, cases when observation is the nearest: 75.0%', 'dimensions: 30, cases when observation is the nearest: 94.0%', 'dimensions: 40, cases when observation is the nearest: 100.0%', 'dimensions: 50, cases when observation is the nearest: 100.0%', 'dimensions: 60, cases when observation is the nearest: 100.0%', 'dimensions: 70, cases when observation is the nearest: 100.0%', 'dimensions: 80, cases when observation is the nearest: 100.0%', 'dimensions: 90, cases when observation is the nearest: 100.0%']