Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save puraminy/c6ff1925387628b04f9489250ff20816 to your computer and use it in GitHub Desktop.
Save puraminy/c6ff1925387628b04f9489250ff20816 to your computer and use it in GitHub Desktop.
import numpy as np
NaN = np.nan
import pandas as pd
# The ratings by users (U)
df = pd.DataFrame(np.array([[8,NaN,10,NaN,10],
[NaN,2,NaN,3,3],
[2,8,NaN,4,1],
[8,NaN,NaN,2,NaN],
[NaN,7,3,NaN,NaN],
[5,NaN,8,1,NaN]]),
columns = ['U1', 'U2', 'U3', 'U4', 'UA'])
# These are items
df.rows = ['A', 'B', 'C', 'D', 'E', 'F']
Z = df.sub(df.mean(), axis=1)
# Z = df.add(df.mean() + df, axis=1)
Z
for i in range(len(Z.columns)-1):
Z['Z'+str(i)+'xZa']=[x*y for x, y in zip(Z.loc[:,'U'+str(i+1)], Z.loc[:,'UA'])]
w = Z.sum(axis=0)[5:9]
w= w.values.tolist()
x = w+['']+w
Z.loc['Similarity(W)', :] = x
print("W",w)
print("meam:", m[4])
d = 1/np.sum(w)
print("d:",d)
print("================")
for i in range(3,6):
s = 0
for j in range(0,4):
a = Z.iloc[i,j]
b = w[j]
if (not np.isnan(a) and not np.isnan(b)):
s += a*b
Z.iloc[i,4] = np.round(m[4] + d*s,2)
from IPython.display import display, HTML
Z=Z.replace(np.nan, '', regex=True)
# Z=Z.round(1)
display(Z)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment