i have a 2D array numpy s like this:
s = np.array([[ 5., 4., np.nan, 1., np.nan],
[np.nan, 4., 4., 2., 2.],
[ 3., np.nan, np.nan, 5., 5.],
[np.nan, 3., 4., 4., np.nan]])
#now i want to create a new np array s1 base on s like this
s1= np.empty((len(s),len(s)))
for i in range(len(s)):
a = np.abs(s - s[i])
a = np.nanmean(a, axis=1)
w = 1 / (a + 0.001)
s1[i] = w
s1
array([[1000. , 1.99600798, 0.33322226, 0.49975012],
[ 1.99600798, 1000. , 0.33322226, 0.999001 ],
[ 0.33322226, 0.33322226, 1000. , 0.999001 ],
[ 0.49975012, 0.999001 , 0.999001 , 1000. ]])
#without use for loop i write like this
def f(x,y):
a = np.abs(s[y]-s[x])
a = np.nanmean(a)
if np.isnan(a):
return 0
w = 1/(a+0.001) #not let 1/0
return w
s1 = np.fromfunction(np.vectorize(f),(len(s),len(s)),dtype='int')
s1
array([[1000. , 1.99600798, 0.33322226, 0.49975012],
[ 1.99600798, 1000. , 0.33322226, 0.999001 ],
[ 0.33322226, 0.33322226, 1000. , 0.999001 ],
[ 0.49975012, 0.999001 , 0.999001 , 1000. ]])
First i want to ask is my np.fromfunction right? Second, are there another ways to rewrite this code with numpy without use for loop?