Example #1
0
 def __init__(self, pd, transform=None):
     """
     Args:
         pd: Pandas dataframe, first column is assumed to be data
         transform (callable, optional): Optional transform to be applied
             on a sample.
     """
     self.pd_frame = pd.copy()
     self.transform = transform
Example #2
0
def binary_n(pd):
    rs = pd.copy()
    for row in pd.iterrows():
        row = row[1]
        rows = binary(row) 
        if rows is None:
            continue
        rs = rs.append(rows)
        print len(rs)
        print len(pd)
    return rs
def alg():
    start = 0
    koniec = start
    rozmiar_macierzy_wag = 30
    rozmiar_populacji = 30
    liczba_iteracji = 1000
    liczba_par = 9
    liczba_najlepszych_rozwiazan = 2
    prawdopodobienstwo_mutacji = 0.05
    liczba_grup = 1

    macierz = macierz_wag(rozmiar_macierzy_wag)
    populacja = populacja_startowa(rozmiar_populacji, macierz, start, koniec)
    ostatnia_odl = 10000000
    najlepsze_wyniki = []

    for i in range(liczba_iteracji):
        nowa_populacja = []

        # drogi bieżącej populacji
        wyniki = wynik_populacji(populacja, macierz)
        najlepszy = populacja[np.argmin(wyniki)]  # najkrotsza droga
        liczba_ruchow = len(najlepszy)
        odleglosc = zmierz_dlugosc(najlepszy, macierz)

        if odleglosc != ostatnia_odl:
            print('Iteracja %i: droga wynosi %f' % (i, odleglosc))

        # rozmnażanie się członków na podstawanie wyników podobieństwa/metoda ruletki
        for j in range(liczba_par):
            nowy_1 = krzyzowanie(list(populacja[znajdz_kolege(wyniki)]),
                                 list(populacja[znajdz_kolege(wyniki)]))
            nowa_populacja = nowa_populacja + [nowy_1]

        # mutacja
        for j in range(len(nowa_populacja)):
            nowa_populacja[j] = np.copy(
                mutacja(nowa_populacja[j], prawdopodobienstwo_mutacji,
                        macierz))

        # zatrzymanie członków starej populacjiw w nowej
        nowa_populacja += [populacja[np.argmin(wyniki)]]
        for j in range(1, liczba_najlepszych_rozwiazan):
            przechowawca = znajdz_kolege(wyniki)
            nowa_populacja += [populacja[przechowawca]]

        # uzupełnienianie populacji randmowymi członkami
        while len(nowa_populacja) < rozmiar_populacji:
            nowa_populacja += [nowy_osobnik(macierz, start, koniec)]

        populacja = copy.deepcopy(nowa_populacja)
        ostatnia_odl = odleglosc
        najlepsze_wyniki.append([i, odleglosc])
Example #4
0
def generate_random_dataset_with_respect_to_categories(df, rows):
    df_new = pd.copy(df)
    for column in df:
        min_cat = min(np.nanmin(df[column]))
        max_cat = max(np.nanmax(df[column]))
        print('yo')
        try:
            df_new[column] = np.random.choice(
                range(int(min_cat),
                      int(max_cat) + 1), rows)
        except ValueError:
            print('hello')
Example #5
0
def callback(pd):
    global rec_db
    global count

    pd_temp = []
    #    print pd
    if (count % 4 == 0):
        pd_temp = pd.copy(deep=True)
        pd_temp = pd_temp.drop('Date', 1)
        rec_db = rec_db.append(pd_temp)

    count += 1
    return pd_temp
Example #6
0
def simulate_season(trace):
    """
    Simulate a season once, using one random draw from the mcmc chain. 
    """
    #num_samples = atts.trace().shape[0]
    #df_trace = pymc.trace_to_dataframe(trace[:1000])
    num_samples = trace['atts'].shape[0]
    draw = np.random.randint(0, num_samples)
    atts_draw = pd.DataFrame({
        'att': trace['atts'][draw, :],
    })
    defs_draw = pd.DataFrame({
        'def': trace['defs'][draw, :],
    })
    home_draw = trace['home'][draw]
    intercept_draw = trace['intercept'][draw]
    season = pd.copy()
    season = pd.merge(season, atts_draw, left_on='i_home', right_index=True)
    season = pd.merge(season, defs_draw, left_on='i_home', right_index=True)
    season = season.rename(columns={'att': 'att_home', 'def': 'def_home'})
    season = pd.merge(season, atts_draw, left_on='i_away', right_index=True)
    season = pd.merge(season, defs_draw, left_on='i_away', right_index=True)
    season = season.rename(columns={'att': 'att_away', 'def': 'def_away'})
    season['home'] = home_draw
    season['intercept'] = intercept_draw
    season['home_theta'] = season.apply(lambda x: math.exp(x['intercept'] + x[
        'home'] + x['att_home'] + x['def_away']),
                                        axis=1)
    season['away_theta'] = season.apply(
        lambda x: math.exp(x['intercept'] + x['att_away'] + x['def_home']),
        axis=1)
    season['home_goals'] = season.apply(
        lambda x: np.random.poisson(x['home_theta']), axis=1)
    season['away_goals'] = season.apply(
        lambda x: np.random.poisson(x['away_theta']), axis=1)
    season['home_outcome'] = season.apply(
        lambda x: 'win' if x['home_goals'] > x['away_goals'] else 'loss'
        if x['home_goals'] < x['away_goals'] else 'draw',
        axis=1)
    season['away_outcome'] = season.apply(
        lambda x: 'win' if x['home_goals'] < x['away_goals'] else 'loss'
        if x['home_goals'] > x['away_goals'] else 'draw',
        axis=1)
    season = season.join(pd.get_dummies(season.home_outcome, prefix='home'))
    season = season.join(pd.get_dummies(season.away_outcome, prefix='away'))
    return season
Example #7
0
    def EventHandler(self, pd):
        """Event Handler Object"""
        self.pd_temp = pd.copy(deep=True)

        #Hardcoded for now
        if (pd['Event'].values == 3):
            if ((self.buy_db.shape[0] > 0) and (self.buy_db['ItemId'].isin(
                    self.pd_temp['ItemId']).any() == True)):
                index = self.buy_db[self.buy_db['ItemId'].isin(
                    self.pd_temp['ItemId'])].index
                self.buy_db.loc[index, 'Count'] += 1
            else:
                self.pd_temp['Count'] = 1
                self.buy_db = self.buy_db.append(
                    self.pd_temp.drop(['Date', 'SessionID', 'Event'],
                                      axis=1,
                                      inplace=False))
        return
decipline = predicate(
    g.subject_objects(
        predicate=rdflib.term.URIRef(u'http://dbpedia.org/ontology/disciple')))
education = predicate(
    g.subject_objects(predicate=rdflib.term.URIRef(
        u'http://dbpedia.org/ontology/education')))
mentor = predicate(
    g.subject_objects(
        predicate=rdflib.term.URIRef(u'http://dbpedia.org/ontology/mentor')))
university = predicate(
    g.subject_objects(predicate=rdflib.term.URIRef(
        u'http://dbpedia.org/ontology/university')))
#Tensor Factorization by ALS
A, R, _, _, _ = rescal.als([
    0.0001 * RDF_Type, 0.0001 * Subject, OWL_SameAs, Influence, BirthPlace,
    Occupation, wikiPageExternalLink, Country, profesion, nationality,
    deathplace, title, formerteam, league, position, team, termperiod, website,
    careerstation, clubs, currentclub, nationalteam, youth, Chancellor, party,
    religion, spouse, sucees, battle, militaryrank, relation, branch, rank,
    first, last, strokes, collegeteam, associatedband, associatedmusic, genre,
    hometown, recordlev, origion, word_net, after, before, predecessor,
    successor, language, residence, parent, father, house, issue, mother,
    coach, instrument, label, alongside, consitency, deputy, govegene, honpref,
    monarch, office, order, prime, movie, education_place, decoration,
    decipline, education, mentor, university
], 100)
simstru = cosine_similarity(A, A)  #applying cosine similarity
pd = pd.DataFrame(
    simstru)  #A data structure to identify the location of resources in memory
pd2 = pd.copy()
Example #9
0
p_0 = p.copy()
p_0 = p_0.drop(max(p_0.index))
p_0 = p_0.reset_index(drop=True)
p_1 = p.copy()
p_1 = p_1.drop(0).reset_index(drop=True)

x = p_1 / p_0

# plt.plot(x)
# plt.legend(x.keys())
# plt.show()

b = np.full((len(x), d), 1. / d)
s = np.full((len(x), d), 1.)
omega = 30
_x = x.copy()
_x = _x.iloc[omega:]
_x.reset_index(drop=True, inplace=True)
for c in _x.keys():
    _x[c] = np.zeros(len(x) - omega)

epsilon = 5

for i in range(omega, len(p)):
    print('b=', b[i - omega])
    median = geometric_median(p.iloc[0:i],
                              method='rf_regressor',
                              options={
                                  'maxiter': 10000,
                                  'tol': 1e-14
                              })
 def combine_columns_ag_news(pd):
     pd = pd.copy()
     pd[3] = pd[1] + ' ' + pd[2]
     del pd[1]
     del pd[2]
     return pd
Example #11
0
 def __init__(self, pd, name):
     self.pd = pd.copy()
     self.name = name
Example #12
0
 def EventHandler(self, pd):
     """Event Handler Object"""
     self.pd_temp = pd.copy(deep=True)
     return
residence=predicate(g.subject_objects(predicate=rdflib.term.URIRef(u'http://dbpedia.org/ontology/residence')))
parent=predicate(g.subject_objects(predicate=rdflib.term.URIRef(u'http://dbpedia.org/ontology/parent')))
father=predicate(g.subject_objects(predicate=rdflib.term.URIRef(u'http://dbpedia.org/property/father')))
house=predicate(g.subject_objects(predicate=rdflib.term.URIRef(u'http://dbpedia.org/property/house')))
issue=predicate(g.subject_objects(predicate=rdflib.term.URIRef(u'http://dbpedia.org/property/issue')))
mother=predicate(g.subject_objects(predicate=rdflib.term.URIRef(u'http://dbpedia.org/property/mother')))
coach=predicate(g.subject_objects(predicate=rdflib.term.URIRef(u'http://dbpedia.org/ontology/coach')))
instrument=predicate(g.subject_objects(predicate=rdflib.term.URIRef(u'http://dbpedia.org/ontology/instrument')))
label=predicate(g.subject_objects(predicate=rdflib.term.URIRef(u'http://dbpedia.org/property/label')))
alongside=predicate(g.subject_objects(predicate=rdflib.term.URIRef(u'http://dbpedia.org/property/alongside')))
consitency=predicate(g.subject_objects(predicate=rdflib.term.URIRef(u'http://dbpedia.org/property/constituencyMp')))
deputy=predicate(g.subject_objects(predicate=rdflib.term.URIRef(u'http://dbpedia.org/property/deputy')))
govegene=predicate(g.subject_objects(predicate=rdflib.term.URIRef(u'http://dbpedia.org/property/governorGeneral')))
honpref=predicate(g.subject_objects(predicate=rdflib.term.URIRef(u'http://dbpedia.org/property/honorificPrefix')))
monarch=predicate(g.subject_objects(predicate=rdflib.term.URIRef(u'http://dbpedia.org/property/monarch')))
office=predicate(g.subject_objects(predicate=rdflib.term.URIRef(u'http://dbpedia.org/property/office')))
order=predicate(g.subject_objects(predicate=rdflib.term.URIRef(u'http://dbpedia.org/property/order')))
prime=predicate(g.subject_objects(predicate=rdflib.term.URIRef(u'http://dbpedia.org/property/primeminister')))
movie=predicate(g.subject_objects(predicate=rdflib.term.URIRef(u'http://dbpedia.org/ontology/movie')))
education_place=predicate(g.subject_objects(predicate=rdflib.term.URIRef(u'http://dbpedia.org/ontology/educationPlace')))
decoration=predicate(g.subject_objects(predicate=rdflib.term.URIRef(u'http://dbpedia.org/ontology/decoration')))
decipline=predicate(g.subject_objects(predicate=rdflib.term.URIRef(u'http://dbpedia.org/ontology/disciple')))
education=predicate(g.subject_objects(predicate=rdflib.term.URIRef(u'http://dbpedia.org/ontology/education')))
mentor=predicate(g.subject_objects(predicate=rdflib.term.URIRef(u'http://dbpedia.org/ontology/mentor')))
university=predicate(g.subject_objects(predicate=rdflib.term.URIRef(u'http://dbpedia.org/ontology/university')))
#Tensor Factorization by ALS
A, R,_, _, _ = rescal.als([0.0001*RDF_Type,0.0001*Subject,OWL_SameAs,Influence,BirthPlace,Occupation,wikiPageExternalLink,Country,profesion,nationality,deathplace,title,formerteam,league,position,team,termperiod,website,careerstation,clubs,currentclub,nationalteam,youth,Chancellor,party,religion,spouse,sucees,battle,militaryrank,relation,branch,rank,first,last,strokes,collegeteam,associatedband,associatedmusic,genre,hometown,recordlev,origion,word_net,after,before,predecessor,successor,language,residence,parent,father,house,issue,mother,coach,instrument,label,alongside,consitency,deputy,govegene,honpref,monarch,office,order,prime,movie,education_place,decoration,decipline,education,mentor,university],100)
simstru=cosine_similarity(A, A)#applying cosine similarity
pd=pd.DataFrame(simstru)#A data structure to identify the location of resources in memory
pd2=pd.copy()
Example #14
0
def limpa_lista(pd):
    pd_clear = pd.copy()
    for i in range(pd.count()):
        pd_clear[i] = 0
    return pd_clear