Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.33 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. u.data     -- The full u data set, 100000 ratings by 943 users on 1682 items.
  4.              Each user has rated at least 20 movies.  Users and items are
  5.              numbered consecutively from 1.  The data is randomly
  6.              ordered. This is a tab separated list of
  7.              user id | item id | rating | timestamp.
  8.              The time stamps are unix seconds since 1/1/1970 UTC
  9.  
  10. """
  11. import numpy as np
  12. import math
  13.  
  14.  
  15.  
  16. def srednia_filmu(movie_id, in_array):
  17.     suma=0
  18.     ilosc=0    
  19.     for x in in_array:
  20.         if x[1]==movie_id:
  21.             ilosc+=1
  22.             suma+=x[2]
  23.     return suma/ilosc;
  24.    
  25. def srednia_uzytkownika(user_id,in_array):
  26.     suma=0
  27.     ilosc=0    
  28.     for x in in_array:
  29.         if x[0]==user_id:
  30.             ilosc+=1
  31.             suma+=x[2]
  32.     print('Suma %s (%f)' % (suma))
  33.     return suma/ilosc;
  34.    
  35.    
  36. def select(parameter_index,value,in_array):
  37.     out_list=[]    
  38.     for x in in_array:
  39.         if x[parameter_index]==value:
  40.             out_list.append(x)
  41.     return out_list;
  42.    
  43. def sim(a,b):
  44.     P=[]
  45.     P1=select(0,a,arr)
  46.     P2=select(0,b,arr)
  47.     for x in P1:
  48.         for y in P2:
  49.             if y[1]==x[1]:
  50.                 P.append([x[1], x[2], y[2]])
  51.             break
  52.     if not P:
  53.         return 0
  54.     sr_a=srednia_uzytkownika(a,arr)
  55.     sr_b=srednia_uzytkownika(b,arr)
  56.     dzielna=0    
  57.     for p in P:
  58.         dzielna+=(p[1]-sr_a)*(p[2]-sr_b)
  59.     s1=0
  60.     for p in P:
  61.         s1+=(p[1]-sr_a)*(p[1]-sr_a)
  62.     s2=0
  63.     for p in P:
  64.         s2+=(p[2]-sr_b)*(p[2]-sr_b)
  65.    
  66.     dzielnik=math.sqrt(s1)*math.sqrt(s2)  
  67.     return dzielna/dzielnik;
  68. def users():
  69.     print("Generowanie słownika użytkownikow")
  70.     users_dictionary={}
  71.     size=0    
  72.     for x in arr:
  73.         if x[0] not in users_dictionary:
  74.             users_dictionary[x[0]]=select(0,x[0],arr)
  75.             size+=1
  76.             if size % 20 ==0:
  77.                 print("Ilość użytkownikow: "+str(size))
  78.     return users_dictionary;
  79. def users1():
  80.     print("Generowanie słownika użytkownikow")
  81.     users_dictionary={}
  82.     size=0    
  83.     for x in arr:
  84.         if x[0] not in users_dictionary:
  85.             l=select(0,x[0],arr)
  86.             node={}
  87.             for y in l:
  88.                 node[y[1]]=y[2]
  89.             users_dictionary[x[0]]=node
  90.             size+=1
  91.             if size % 20 ==0:
  92.                 print("Ilość użytkownikow: "+str(size))
  93.     return users_dictionary;
  94.  
  95. def pred(a,p):
  96.     sr_a=srednia_uzytkownika(a,arr)
  97.     dzielna=0
  98.     for k,value in users_movies_rate.items():
  99.         if k!= a:
  100.             if p in value:
  101.                 r_k_p=value[p]
  102.             else:
  103.                 r_k_p=0
  104.             dzielna+=sim(a,k)*(r_k_p-srednia_uzytkownika(k,arr))
  105.     dzielnik=0
  106.     for k in users_movies_rate:
  107.         if k!=a:
  108.             dzielnik+=sim(a,k)
  109.     return sr_a+dzielna/dzielnik;
  110.            
  111.          
  112.            
  113. f=open("c:\\Users\\Computer\\Desktop\\System_rekomendacyjny-master\\System_rekomendacyjny-master\\DB\\u.data")
  114. lista=[]
  115.  
  116.  
  117. for line in f:
  118.         splited=line.split("\t",4)
  119.         lista.append([int(splited[0]),int(splited[1]),\
  120.         int(splited[2]),int(splited[3])])
  121.  
  122. arr=np.array(lista)
  123. users_movies_rate=users1()
  124.  
  125. ar = []
  126. #print('Najbardziej podobny %s (%f)' % (srednia_filmu(1, ar)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement