view Code/eda.py @ 16:68b8b088f50a

Code for pre-training
author Paulo Chiliguano <p.e.chiilguano@se14.qmul.ac.uk>
date Mon, 27 Jul 2015 19:24:37 +0100
parents 2e3c57fba632
children ee13c193c76e
line wrap: on
line source
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 22 17:42:09 2015

@author: paulochiliguano
"""


import random
import numpy as np
from sklearn import mixture

#User-item dictionary
users = {"Angelica": {"SOAJJPC12AB017D63F": 3.5, "SOAKIXJ12AC3DF7152": 2.0,
                      "SOAKPFH12A8C13BA4A": 4.5, "SOAGTJW12A6701F1F5": 5.0,
                      "SOAKWCK12A8C139F81": 1.5, "SOAKNZI12A58A79CAC": 2.5,
                      "SOAJZEP12A8C14379B": 2.0},
         "Bill":{"SOAJJPC12AB017D63F": 2.0, "SOAKIXJ12AC3DF7152": 3.5,
                 "SOAHQFM12A8C134B65": 4.0, "SOAGTJW12A6701F1F5": 2.0,
                 "SOAKWCK12A8C139F81": 3.5, "SOAJZEP12A8C14379B": 3.0},
         "Chan": {"SOAJJPC12AB017D63F": 5.0, "SOAKIXJ12AC3DF7152": 1.0,
                  "SOAHQFM12A8C134B65": 1.0, "SOAKPFH12A8C13BA4A": 3.0,
                  "SOAGTJW12A6701F1F5": 5, "SOAKWCK12A8C139F81": 1.0},
         "Dan": {"SOAJJPC12AB017D63F": 3.0, "SOAKIXJ12AC3DF7152": 4.0,
                 "SOAHQFM12A8C134B65": 4.5, "SOAGTJW12A6701F1F5": 3.0,
                 "SOAKWCK12A8C139F81": 4.5, "SOAKNZI12A58A79CAC": 4.0,
                 "SOAJZEP12A8C14379B": 2.0},
         "Hailey": {"SOAKIXJ12AC3DF7152": 4.0, "SOAHQFM12A8C134B65": 1.0,
                    "SOAKPFH12A8C13BA4A": 4.0, "SOAKNZI12A58A79CAC": 4.0,
                    "SOAJZEP12A8C14379B": 1.0},
         "Jordyn":  {"SOAKIXJ12AC3DF7152": 4.5, "SOAHQFM12A8C134B65": 4.0,
                     "SOAKPFH12A8C13BA4A": 5.0, "SOAGTJW12A6701F1F5": 5.0,
                     "SOAKWCK12A8C139F81": 4.5, "SOAKNZI12A58A79CAC": 4.0,
                     "SOAJZEP12A8C14379B": 4.0},
         "Sam": {"SOAJJPC12AB017D63F": 5.0, "SOAKIXJ12AC3DF7152": 2.0,
                 "SOAKPFH12A8C13BA4A": 3.0, "SOAGTJW12A6701F1F5": 5.0,
                 "SOAKWCK12A8C139F81": 4.0, "SOAKNZI12A58A79CAC": 5.0},
         "Veronica": {"SOAJJPC12AB017D63F": 3.0, "SOAKPFH12A8C13BA4A": 5.0,
                      "SOAGTJW12A6701F1F5": 4.0, "SOAKWCK12A8C139F81": 2.5,
                      "SOAKNZI12A58A79CAC": 3.0}
        }

items = {"SOAJJPC12AB017D63F": [2.5, 4, 3.5, 3, 5, 4, 1, 5, 4, 1],
         "SOAKIXJ12AC3DF7152": [2, 5, 5, 3, 2, 1, 1, 5, 4, 1],
         "SOAKPFH12A8C13BA4A": [1, 5, 4, 2, 4, 1, 1, 5, 4, 1],
         "SOAGTJW12A6701F1F5": [4, 5, 4, 4, 1, 5, 1, 5, 4, 1],
         "SOAKWCK12A8C139F81": [1, 4, 5, 3.5, 5, 1, 1, 5, 4, 1],
         "SOAKNZI12A58A79CAC": [1, 5, 3.5, 3, 4, 5, 1, 5, 4, 1],
         "SOAJZEP12A8C14379B": [5, 5, 4, 2, 1, 1, 1, 5, 4, 1],
         "SOAHQFM12A8C134B65": [2.5, 4, 4, 1, 1, 1, 1, 5, 4, 1]}
'''
profile = {"Profile0": [2.5, 4, 3.5, 3, 5, 4, 1],
           "Profile1": [2.5, 4, 3.5, 3, 5, 4, 1],
           "Profile2": [2.5, 4, 3.5, 3, 5, 4, 1],
           "Profile3": [2.5, 4, 3.5, 3, 5, 4, 1],
           "Profile4": [2.5, 4, 3.5, 3, 5, 4, 1],
           "Profile5": [2.5, 4, 3.5, 3, 5, 4, 1],
           "Profile6": [2.5, 4, 3.5, 3, 5, 4, 1],
           "Profile7": [2.5, 4, 3.5, 3, 5, 4, 1]}
'''

'''
Generate M individuals uniformly
'''
np.random.seed(len(users))
M = np.random.uniform(1, 5, len(users) * len(items.values()[0]))
M.shape = (-1, len(items.values()[0]))
profile = {}
i = 0
for row in M.tolist():
    profile["Profile" + str(i)] = M.tolist()[i]
    i = i + 1

np.random.seed(1)
g = mixture.GMM(n_components=7)
# Generate random observations with two modes centered on 0
# and 10 to use for training.
obs = np.concatenate((np.random.randn(100, 1), 10 + np.random.randn(300, 1)))
g.fit(obs) 
np.round(g.weights_, 2)
np.round(g.means_, 2)
np.round(g.covars_, 2) 
g.predict([[0], [2], [9], [10]]) 
np.round(g.score([[0], [2], [9], [10]]), 2)
# Refit the model on new data (initial parameters remain the
# same), this time with an even split between the two modes.
g.fit(20 * [[0]] +  20 * [[10]]) 
np.round(g.weights_, 2)