text:
import pandas as pd

import numpy as np
import pickle
import dill

import lime
from lime import lime_tabular

import matplotlib.pyplot as plt

from rdkit import Chem
from rdkit.Chem import MACCSkeys
from rdkit.Chem import AllChem

Maccs_keys_dict = {
          1:('?','Isotope'),
          2:('[#104]','Atomic no 104 - Rutherfordium'),
          3:('[#32,#33,#34,#50,#51,#52,#82,#83,#84]','Group IVa, Va, VIa Periods 4-6: Ge, As, Se, Sn, Sb, Te,Pb, Bi, Po'),
          4:('[Ac,Th,Pa,U,Np,Pu,Am,Cm,Bk,Cf,Es,Fm,Md,No,Lr]','Actinide'),
          5:('[Sc,Ti,Y,Zr,Hf]','Group IIIB, IVB'),
          6:('[La,Ce,Pr,Nd,Pm,Sm,Eu,Gd,Tb,Dy,Ho,Er,Tm,Yb,Lu]','Lanthanide'),
          7:('[V,Cr,Mn,Nb,Mo,Tc,Ta,W,Re]','Group VB, VIB, VIIB'),
          8:('[!#6;!#1]1~*~*~*~1','QAAA@1'),
          9:('[Fe,Co,Ni,Ru,Rh,Pd,Os,Ir,Pt]','Group VIII'),
          10:('[Be,Mg,Ca,Sr,Ba,Ra]','Group IIa (Alkaline earth)'),
          11:('*1~*~*~*~1','4M Ring'),
          12:('[Cu,Zn,Ag,Cd,Au,Hg]','Group IB, IIB'),
          13:('[#8]~[#7](~[#6])~[#6]','ON(C)C'),
          14:('[#16]-[#16]','S-S'),
          15:('[#8]~[#6](~[#8])~[#8]','OC(O)O'),
          16:('[!#6;!#1]1~*~*~1','QAA@1'),
          17:('[#6]#[#6]','CTC'),
          18:('[#5,#13,#31,#49,#81]','Group IIIA'),
          19:('*1~*~*~*~*~*~*~1','7M Ring'),
          20:('[#14]','Si'),
          21:('[#6]=[#6](~[!#6;!#1])~[!#6;!#1]','C=C(Q)Q'),
          22:('*1~*~*~1','3M Ring'),
          23:('[#7]~[#6](~[#8])~[#8]','NC(O)O'),
          24:('[#7]-[#8]','N-O'),
          25:('[#7]~[#6](~[#7])~[#7]','NC(N)N'),
          26:('[#6]=;@[#6](@*)@*','C$=C($A)$A'),
          27:('[I]','I'),
          28:('[!#6;!#1]~[CH2]~[!#6;!#1]','QCH2Q'),
          29:('[#15]','P'),
          30:('[#6]~[!#6;!#1](~[#6])(~[#6])~*','CQ(C)(C)A'),
          31:('[!#6;!#1]~[F,Cl,Br,I]','QX'),
          32:('[#6]~[#16]~[#7]','CSN'),
          33:('[#7]~[#16]','NS'),
          34:('[CH2]=*','CH2=A'),
          35:('[Li,Na,K,Rb,Cs,Fr]','Group IA (Alkali Metal)'),
          36:('[#16R]','S Heterocycle'),
          37:('[#7]~[#6](~[#8])~[#7]','NC(O)N'),
          38:('[#7]~[#6](~[#6])~[#7]',' NC(C)N'),
          39:('[#8]~[#16](~[#8])~[#8]','OS(O)O'),
          40:('[#16]-[#8]','S-O'),
          41:('[#6]#[#7]','CTN'),
          42:('F','F'),
          43:('[!#6;!#1;!H0]~*~[!#6;!#1;!H0]','QHAQH'),
          44:('?','Other - atoms include any atoms other than H, C, N, O, Si, P, S, F, Cl, Br, and I, and is abbreviated "Z"'),
          45:('[#6]=[#6]~[#7]','C=CN'),
          46:('Br',"Br"),
          47:('[#16]~*~[#7]','SAN'),
          48:('[#8]~[!#6;!#1](~[#8])(~[#8])','OQ(O)O'),
          49:('[!+0]','CHARGE'),
          50:('[#6]=[#6](~[#6])~[#6]','C=C(C)C'),
          51:('[#6]~[#16]~[#8]','CSO'),
          52:('[#7]~[#7]','NN'),
          53:('[!#6;!#1;!H0]~*~*~*~[!#6;!#1;!H0]','QHAAAQH'),
          54:('[!#6;!#1;!H0]~*~*~[!#6;!#1;!H0]','QHAAQH'),
          55:('[#8]~[#16]~[#8]','OSO'),
          56:('[#8]~[#7](~[#8])~[#6]','ON(O)C'),
          57:('[#8R]','O Heterocycle'),
          58:('[!#6;!#1]~[#16]~[!#6;!#1]','QSQ'),
          59:('[#16]!:*:*','Snot%A%A'),
          60:('[#16]=[#8]','S=O'),
          61:('*~[#16](~*)~*','AS(A)A'),
          62:('*@*!@*@*','A$!A$A'),
          63:('[#7]=[#8]','N=O'),
          64:('*@*!@[#16]','A$A!S'),
          65:('c:n','C%N'),
          66:('[#6]~[#6](~[#6])(~[#6])~*','CC(C)(C)A'),
          67:('[!#6;!#1]~[#16]','QS'),
          68:('[!#6;!#1;!H0]~[!#6;!#1;!H0]','QHQH (&...)'),
          69:('[!#6;!#1]~[!#6;!#1;!H0]','QQH'),
          70:('[!#6;!#1]~[#7]~[!#6;!#1]','QNQ'),
          71:('[#7]~[#8]','NO'),
          72:('[#8]~*~*~[#8]','OAAO'),
          73:('[#16]=*','S=A'),
          74:('[CH3]~*~[CH3]','CH3ACH3'),
          75:('*!@[#7]@*','A!N$A'),
          76:('[#6]=[#6](~*)~*','C=C(A)A'),
          77:('[#7]~*~[#7]','NAN'),
          78:('[#6]=[#7]', 'C=N'),
          79:('[#7]~*~*~[#7]', 'NAAN'),
          80:('[#7]~*~*~*~[#7]','NAAAN'),
          81:('[#16]~*(~*)~*', 'SA(A)A'),
          82:('*~[CH2]~[!#6;!#1;!H0]','ACH2QH'),
          83:('[!#6;!#1]1~*~*~*~*~1','QAAAA@1'),
          84:('[NH2]','NH2'),
          85:('[#6]~[#7](~[#6])~[#6]','CN(C)C'),
          86:('[C;H2,H3][!#6;!#1][C;H2,H3]','CH2QCH2'),
          87:('[F,Cl,Br,I]!@*@*','X!A$A'),
          88:('[#16]','S'),
          89:('[#8]~*~*~*~[#8]','OAAAO'),
          90:('[$([!#6;!#1;!H0]~*~*~[CH2]~*),$([!#6;!#1;!H0;R]1@[R]@[R]@[CH2;R]1),$([!#6;!#1;!H0]~[R]1@[R]@[CH2;R]1)]','QHAACH2A'),
          91:('[$([!#6;!#1;!H0]~*~*~*~[CH2]~*),$([!#6;!#1;!H0;R]1@[R]@[R]@[R]@[CH2;R]1),$([!#6;!#1;!H0]~[R]1@[R]@[R]@[CH2;R]1),$([!#6;!#1;!H0]~*~[R]1@[R]@[CH2;R]1)]','QHAAACH2A'),
          92:('[#8]~[#6](~[#7])~[#6]','OC(N)C'),
          93:('[!#6;!#1]~[CH3]', 'QCH3'),
          94:('[!#6;!#1]~[#7]','QN'),
          95:('[#7]~*~*~[#8]', 'NAAO'),
          96:('*1~*~*~*~*~1', '5M Ring'),
          97:('[#7]~*~*~*~[#8]', 'NAAAO'),
          98:('[!#6;!#1]1~*~*~*~*~*~1', 'QAAAAA@1'),
          99:('[#6]=[#6]', 'C=C'),
          100:('*~[CH2]~[#7]', 'ACH2N'),
          101:('[$([R]@1@[R]@[R]@[R]@[R]@[R]@[R]@[R]1),$([R]@1@[R]@[R]@[R]@[R]@[R]@[R]@[R]@[R]1),$([R]@1@[R]@[R]@[R]@[R]@[R]@[R]@[R]@[R]@[R]1),$([R]@1@[R]@[R]@[R]@[R]@[R]@[R]@[R]@[R]@[R]@[R]1),$([R]@1@[R]@[R]@[R]@[R]@[R]@[R]@[R]@[R]@[R]@[R]@[R]1),$([R]@1@[R]@[R]@[R]@[R]@[R]@[R]@[R]@[R]@[R]@[R]@[R]@[R]1),$([R]@1@[R]@[R]@[R]@[R]@[R]@[R]@[R]@[R]@[R]@[R]@[R]@[R]@[R]1)]','8M Ring or larger (max 14)'),
          102:('[!#6;!#1]~[#8]', 'QO'),
          103:('Cl', 'Cl'),
          104:('[!#6;!#1;!H0]~*~[CH2]~*', 'QHACH2A'),
          105:('*@*(@*)@*', 'A$A($A)$A'),
          106:('[!#6;!#1]~*(~[!#6;!#1])~[!#6;!#1]', 'QA(Q)Q'),
          107:('[F,Cl,Br,I]~*(~*)~*', 'XA(A)A'),
          108:('[CH3]~*~*~*~[CH2]~*','CH3AAACH2A'),
          109:('*~[CH2]~[#8]', 'ACH2O'),
          110:('[#7]~[#6]~[#8]', 'NCO'),
          111:('[#7]~*~[CH2]~*', 'NACH2A'),
          112:('*~*(~*)(~*)~*', 'AA(A)(A)A'),
          113:('[#8]!:*:*', 'Onot%A%A'),
          114:('[CH3]~[CH2]~*', 'CH3CH2A'),
          115:('[CH3]~*~[CH2]~*', 'CH3ACH2A'),
          116:('[$([CH3]~*~*~[CH2]~*),$([CH3]~*1~*~[CH2]1)]','CH3AACH2A'),
          117:('[#7]~*~[#8]','NAO'),
          118:('[$(*~[CH2]~[CH2]~*),$(*1~[CH2]~[CH2]1)]','ACH2CH2A'),
          119:('[#7]=*','N=A'),
          120:('[!#6;R]','Heterocyclic atom > 1 (&...)'),
          121:('[#7;R]','N Heterocycle'),
          122:('*~[#7](~*)~*', 'AN(A)A'),
          123:('[#8]~[#6]~[#8]', 'OCO'),
          124:('[!#6;!#1]~[!#6;!#1]', 'QQ'),
          125:('?', 'Aromatic Ring > 1'),
          126:('*!@[#8]!@*', 'A!O!A'),
          127:('*@*!@[#8]', 'A$A!O > 1 (&...)'),
          128:('[$(*~[CH2]~*~*~*~[CH2]~*),$([R]1@[CH2;R]@[R]@[R]@[R]@[CH2;R]1),$(*~[CH2]~[R]1@[R]@[R]@[CH2;R]1),$(*~[CH2]~*~[R]1@[R]@[CH2;R]1)]', 'ACH2AAACH2A'),
          129:('[$(*~[CH2]~*~*~[CH2]~*),$([R]1@[CH2]@[R]@[R]@[CH2;R]1),$(*~[CH2]~[R]1@[R]@[CH2;R]1)]', 'ACH2AACH2A'),
          130:('[!#6;!#1]~[!#6;!#1]', 'QQ > 1 (&...) '),
          131:('[!#6;!#1;!H0]', 'QH > 1'),
          132:('[#8]~*~[CH2]~*', 'OACH2A'),
          133:('*@*!@[#7]', 'A$A!N'),
          134:('[F,Cl,Br,I]', 'X (HALOGEN)'),
          135:('[#7]!:*:*', 'Nnot%A%A'),
          136:('[#8]=*', 'O=A>1'),
          137:('[!C;!c;R]', 'Heterocycle'),
          138:('[!#6;!#1]~[CH2]~*', 'QCH2A>1 (&...)'),
          139:('[O;!H0]', 'OH'),
          140:('[#8]', 'O > 3 (&...)'),
          141:('[CH3]', 'CH3 > 2  (&...)'),
          142:('[#7]', 'N > 1'),
          143:('*@*!@[#8]', 'A$A!O'),
          144:('*!:*:*!:*', 'Anot%A%Anot%A'),
          145:('*1~*~*~*~*~*~1', '6M ring > 1'),
          146:('[#8]', 'O > 2'),
          147:('[$(*~[CH2]~[CH2]~*),$([R]1@[CH2;R]@[CH2;R]1)]', 'ACH2CH2A'),
          148:('*~[!#6;!#1](~*)~*', 'AQ(A)A'),
          149:('[C;H3,H4]', 'CH3 > 1'),
          150:('*!@*@*!@*', 'A!A$A!A'),
          151:('[#7;!H0]', 'NH'),
          152:('[#8]~[#6](~[#6])~[#6]', 'OC(C)C'),
          153:('[!#6;!#1]~[CH2]~*', 'QCH2A'),
          154:('[#6]=[#8]', 'C=O'),
          155:('*!@[CH2]!@*', 'A!CH2!A'),
          156:('[#7]~*(~*)~*', 'NA(A)A'),
          157:('[#6]-[#8]', 'C-O'),
          158:('[#6]-[#7]', 'C-N'),
          159:('[#8]', 'O>1'),
          160:('[C;H3,H4]', 'CH3'),
          161:('[#7]', 'N'),
          162:('a', 'Aromatic'),
          163:('*1~*~*~*~*~*~1', '6M Ring'),
          164:('[#8]', 'O'),
          165:('[R]', 'Ring'),
          166:('?', 'Fragments')
  }

info = """ Atom symbols:
A : Any valid periodic table element symbol
Q : Hetro atoms; any non-C or non-H atom
X : Halogens; F, Cl, Br, I
Z : Others; other than H, C, N, O, Si, P, S, F, Cl, Br, I

Bond types:

- : Single
= : Double
T : Triple
# : Triple
~ : Single or double query bond
% : An aromatic query bond
None : Any bond type; no explicit bond specified
$ : Ring bond; before a bond type specifies ring bond
! : Chain or non-ring bond; before a bond type specifies chain bond
@ : A ring linkage and the number following it specifies the atoms position in the line, thus @1 means linked back to the first atom in the list.

Aromatic: Kekule or Arom5
Kekule: Bonds in 6-membered rings with alternate single/double bonds or perimeter bonds
Arom5: Bonds in 5-membered rings with two double bonds and a hetro atom at the apex of the ring. """

def GetMACCS(smiles):
    """Function to convert smiles to numpy array of MACCS keys"""
    mol = Chem.MolFromSmiles(smiles)
    fp = MACCSkeys.GenMACCSKeys(mol)
    fp.ToBitString()
    fp_list = list(fp)[1:]
    return np.array(fp_list)

def Explain(fingerprint, model):
  """Function to return model explanation for fingerprint"""
  with open('lime_explainer', 'rb') as f:
    explainer = dill.load(f)
  explanation = explainer.explain_instance(fingerprint, model.predict, num_features=20)
  return explanation

def ExpToDf(explanation, fingerprint):
    """Function to convert explanation from lime to pandas dataframe with extra informations"""
    df_exp = pd.DataFrame(explanation.as_map()[1], columns=['Key', 'Influence'])
    df_exp['Key']+=1
    df_exp['Value'] = [fingerprint[idx-1] for idx in df_exp['Key']]
    df_exp['Influence sign'] = np.where(df_exp['Influence'] < 0, "negative", np.where(df_exp['Influence'] >0, 'positive', 'neutral'))
    df_exp = df_exp[['Key', 'Value', 'Influence', 'Influence sign']]
    df_exp['Smarts'] = df_exp.apply(lambda x: Maccs_keys_dict[x.Key][0], axis=1)
    df_exp['Smarts'] = df_exp['Smarts'].str.replace('$', '$\$$')
    df_exp['Description'] = df_exp.apply(lambda x: Maccs_keys_dict[x.Key][1], axis=1)
    df_exp['Description'] = df_exp['Description'].str.replace('$', '$\$$')
    df1 = df_exp.style.set_table_styles([dict(selector='th', props=[('text-align', 'center')])])
    df1.set_properties(**{'text-align': 'center'}).hide_index()
    return df1

def predict_and_interpret(smiles):
  """Function to get prediction and interpretation dataframe"""
  loaded_model = pickle.load(open("rf.pkl", 'rb'))
  fingerprint = GetMACCS(smiles)
  prediction  = loaded_model.predict(fingerprint.reshape(1,-1))
  explanation  = Explain(fingerprint, loaded_model)
  data_frame = ExpToDf(explanation, fingerprint)
  return prediction, data_frame

def predict(data_frame):
  """Function to get prediction for dataframe"""
  loaded_model = pickle.load(open("rf.pkl", 'rb'))
  fingerprints = [GetMACCS(smiles) for smiles in data_frame['smiles']]
  rf_predictions  = loaded_model.predict(fingerprints)
  return np.array(rf_predictions)


text:
import numpy as np
import torch
from torch.utils.data import Dataset
from tqdm.auto import tqdm
device = 'cuda' if torch.cuda.is_available() else 'cpu'

class PointwiseDataset(Dataset):
    def __init__(
        self,
        df,
        label_col,
        cat_cols,
        num_cols,
    ):
        self.label_col = label_col
        self.cat_cols = cat_cols
        self.num_cols = num_cols
        self.df = df
        self.cat_dims = [self.df[i].nunique() for i in self.cat_cols]
        self.features = []
        self._build()

    def __len__(self):
        return len(self.features)

    def __getitem__(self, i):
        return self.features[i]

    def _build(self):
        cat_features = torch.tensor(self.df[self.cat_cols].values,dtype=torch.long).to(device)
        num_features = torch.tensor(self.df[self.num_cols].values,dtype=torch.float).to(device)
        label = torch.tensor(self.df[self.label_col].values,dtype=torch.float).to(device)
        for i in tqdm(range(self.df.shape[0])):
            feat = {
                'cat_feature': cat_features[i],
                'num_feature': num_features[i],
                'label': label[i]
            }
            self.features.append(feat)

class PairwiseDataset(Dataset):
    def __init__(
        self,
        df,
        label_col,
        cat_cols,
        num_cols,
    ):
        self.label_col = label_col
        self.cat_cols = cat_cols
        self.num_cols = num_cols
        self.df = df
        self.cat_dims = [self.df[i].nunique() for i in self.cat_cols]
        self.features = []
        self._build()

    def __len__(self):
        return len(self.features)

    def __getitem__(self, i):
        feat_i =  self.features[i]
        feat_j = self.features[np.random.randint(len(self.features))]
        return {
            'cat_feature_i': feat_i['cat_feature'],
            'num_feature_i': feat_i['num_feature'],
            'cat_feature_j': feat_j['cat_feature'],
            'num_feature_j': feat_j['num_feature'],
            'label': torch.tensor([int(feat_i['label'] > feat_j['label'])],dtype=torch.float).to(device)
        }

    def _build(self):
        cat_features = torch.tensor(self.df[self.cat_cols].values,dtype=torch.long).to(device)
        num_features = torch.tensor(self.df[self.num_cols].values,dtype=torch.float).to(device)
        label = self.df[self.label_col].values
        for i in tqdm(range(self.df.shape[0])):
            feat = {
                'cat_feature': cat_features[i],
                'num_feature': num_features[i],
                'label': label[i]
            }
            self.features.append(feat)

ext: py

max_stars_repo_name: Datatouille/sushirank

max_issues_repo_name: Datatouille/sushirank

max_forks_repo_name: Datatouille/sushirank

reason: import numpy


text:
import numpy as np
from config import config

all_agents = np.arange(config.AGENTS)

class Agent:
    def __init__(self, env, pool, brain):
        self.env = env
        self.pool = pool
        self.brain = brain

        self.epsilon = config.EPSILON_START

        self.idx = np.zeros(config.AGENTS, dtype=np.int32)
        self.S = np.zeros(
            (config.AGENTS, config.FEATURE_DIM + 1, 2, config.FEATURE_DIM),
            dtype=np.float32,
        )
        self.A = np.zeros((config.AGENTS, config.FEATURE_DIM + 1), dtype=np.int64)
        self.R = np.zeros((config.AGENTS, config.FEATURE_DIM + 1), dtype=np.float32)
        self.U = np.zeros((config.AGENTS, config.FEATURE_DIM + 1), dtype=np.float32)
        self.NA = np.zeros(
            (config.AGENTS, config.FEATURE_DIM + 1, config.ACTION_DIM), dtype=np.bool
        )

        s, na = self.env.reset()
        self.S[all_agents, self.idx] = s
        self.NA[all_agents, self.idx] = na

    def act(self, s, na):
        q = self.brain.predict_np(s)
        p = (
            q - config.MAX_MASK_CONST * na
        )  # select an action not considering those already performed
        a = np.argmax(p, axis=1)

        rand_agents = np.random.rand(config.AGENTS) < self.epsilon
        rand_number = np.random.rand(
            config.AGENTS
        )  # rand() call is expensive, better to do it at once

        possible_actions_count = config.ACTION_DIM - np.sum(na, axis=1)
        u = (1 - self.epsilon) + (self.epsilon / possible_actions_count)

        for i in range(config.AGENTS):
            if rand_agents[i]:  # random action
                possible_actions = np.where(na[i] == False)[
                    0
                ]  # select a random action, don't repeat an action

                w = int(rand_number[i] * possible_actions_count[i])
                a_ = possible_actions[w]

                if a[i] == a_:
                    u[i] = (1 - self.epsilon) + (
                        self.epsilon / possible_actions_count[i]
                    )  # randomly selected the maximizing action

                else:
                    a[i] = a_
                    u[i] = (
                        self.epsilon / possible_actions_count[i]
                    )  # probability of taking a random action

        return a, u

    def step(self):
        s = self.S[all_agents, self.idx]
        na = self.NA[all_agents, self.idx]

        a, u = self.act(s, na)
        s_, r, na_, done, info = self.env.step(a)

        self.A[all_agents, self.idx] = a
        self.R[all_agents, self.idx] = r
        self.U[all_agents, self.idx] = u

        for i in np.where(done)[0]:  # truncate & store the finished episode i
            idx = self.idx[i] + 1

            _s = self.S[i, :idx].copy()
            _a = self.A[i, :idx].copy()
            _r = self.R[i, :idx].copy()
            _u = self.U[i, :idx].copy()
            _na = self.NA[i, :idx].copy()

            # extract the true state
            _x = np.broadcast_to(self.env.x[i].copy(), (idx, config.FEATURE_DIM))
            _y = np.repeat(self.env.y[i], idx)

            self.pool.put((_s, _a, _r, _u, _na, _x, _y))

        self.idx = (done == 0) * (
            self.idx + 1
        )  # advance idx by 1 and reset to 0 for finished episodes

        self.NA[all_agents, self.idx] = na_  # unavailable actions
        self.S[all_agents, self.idx] = s_

        return s, a, r, s_, done, info

    def update_epsilon(self, epoch):
        if epoch >= config.EPSILON_EPOCHS:
            self.epsilon = config.EPSILON_END
        else:
            self.epsilon = (
                config.EPSILON_START
                + epoch
                * (config.EPSILON_END - config.EPSILON_START)
                / config.EPSILON_EPOCHS
            )

class PerfAgent(Agent):
    def __init__(self, env, brain):
        self.env = env
        self.brain = brain

        self.idx = np.zeros(config.AGENTS, dtype=np.int32)
        self.S = np.zeros(
            (config.AGENTS, config.FEATURE_DIM + 1, 2, config.FEATURE_DIM),
            dtype=np.float32,
        )
        self.NA = np.zeros(
            (config.AGENTS, config.FEATURE_DIM + 1, config.ACTION_DIM), dtype=np.bool
        )

        s, na = self.env.reset()
        self.S[all_agents, self.idx] = s
        self.NA[all_agents, self.idx] = na

    def act(self, s, na):
        q = self.brain.predict_np(s)
        p = (
            q - config.MAX_MASK_CONST * na
        )  # select an action not considering those already performed
        a = np.argmax(p, axis=1)

        return a, 1.0

    def step(self):
        s = self.S[all_agents, self.idx]
        na = self.NA[all_agents, self.idx]

        a, u = self.act(s, na)
        s_, r, na_, done, info = self.env.step(a)

        self.idx = (done == 0) * (
            self.idx + 1
        )  # advance idx by 1 and reset to 0 for finished episodes

        self.NA[all_agents, self.idx] = na_  # unavailable actions
        self.S[all_agents, self.idx] = s_

        return s, a, r, s_, done, info

max_stars_repo_name: gatech-sysml/cwcf

max_issues_repo_name: gatech-sysml/cwcf

max_forks_repo_name: gatech-sysml/cwcf


text:
# coding=utf-8
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tasks which use a resnet like architecture for image classification."""
import functools
from typing import Any, Mapping

import chex
import gin
import haiku as hk
import jax
import jax.numpy as jnp
from learned_optimization.tasks import base
from learned_optimization.tasks import resnet
from learned_optimization.tasks.datasets import image

class _ResnetTask(base.Task):
  """Tranformer from a dictionary configuration."""

  def __init__(self, cfg: Mapping[str, Any], name: str = '__Resnet'):
    dataset_map = {
        16: image.imagenet16_datasets,
        32: image.imagenet32_datasets,
        64: image.imagenet64_datasets
    }
    self.datasets = dataset_map[cfg['image_size']](cfg['batch_size'])
    self._cfg = cfg
    self._net = hk.transform_with_state(self._hk_forward)
    self._name = name

  @property
  def name(self):
    return self._name

  def _hk_forward(self, batch):
    args = [
        'blocks_per_group', 'use_projection', 'channels_per_group',
        'initial_conv_kernel_size', 'initial_conv_stride', 'max_pool',
        'resnet_v2'
    ]
    num_classes = self.datasets.extra_info['num_classes']
    mod = resnet.ResNet(
        num_classes=num_classes, **{k: self._cfg[k] for k in args})
    logits = mod(batch['image'], is_training=True)
    loss = base.softmax_cross_entropy(
        logits=logits, labels=jax.nn.one_hot(batch['label'], num_classes))
    return jnp.mean(loss)

  def init_with_state(self, key: chex.PRNGKey) -> base.Params:
    batch = jax.tree_map(lambda x: jnp.ones(x.shape, x.dtype),
                         self.datasets.abstract_batch)
    return self._net.init(key, batch)

  def loss_with_state(self, params, state, key, data):
    loss, state, _ = self.loss_with_state_and_aux(params, state, key, data)
    return loss, state

  def loss_with_state_and_aux(self, params, state, key, data):
    loss, state = self._net.apply(params, state, key, data)
    return loss, state, {}

# These configs are chosen based on a large hparam search over architecture.
# Each subsequent config both performs better, and is more expensive to compute
# per step. In general these models are still quite small compared to say,
# Resnet50.

# pyformat: disable, pylint: disable=bad-continuation
for _i, _cfg in enumerate([
  {'image_size': 16, 'batch_size': 64, 'blocks_per_group': (1, 1, 1, 1), 'use_projection': (True, True, True, True), 'channels_per_group': (8, 16, 16, 16), 'initial_conv_kernel_size': 3, 'initial_conv_stride': 2, 'max_pool': True, 'resnet_v2': True},
  {'image_size': 16, 'batch_size': 64, 'blocks_per_group': (1, 1, 1, 1), 'use_projection': (True, True, True, True), 'channels_per_group': (8, 16, 16, 32), 'initial_conv_kernel_size': 3, 'initial_conv_stride': 2, 'max_pool': True, 'resnet_v2': True},
  {'image_size': 16, 'batch_size': 64, 'blocks_per_group': (1, 1, 1, 1), 'use_projection': (True, True, True, True), 'channels_per_group': (64, 128, 128, 128), 'initial_conv_kernel_size': 3, 'initial_conv_stride': 2, 'max_pool': True, 'resnet_v2': True},
  {'image_size': 16, 'batch_size': 192, 'blocks_per_group': (1, 1, 1, 1), 'use_projection': (True, True, True, True), 'channels_per_group': (64, 128, 256, 512), 'initial_conv_kernel_size': 5, 'initial_conv_stride': 2, 'max_pool': True, 'resnet_v2': True},
  {'image_size': 16, 'batch_size': 192, 'blocks_per_group': (1, 1, 1, 1), 'use_projection': (True, True, True, True), 'channels_per_group': (128, 256, 512, 512), 'initial_conv_kernel_size': 5, 'initial_conv_stride': 2, 'max_pool': True, 'resnet_v2': True},
  {'image_size': 32, 'batch_size': 128, 'blocks_per_group': (1, 1, 1, 1), 'use_projection': (True, True, True, True), 'channels_per_group': (128, 256, 512, 512), 'initial_conv_kernel_size': 5, 'initial_conv_stride': 2, 'max_pool': True, 'resnet_v2': True},
  {'image_size': 32, 'batch_size': 256, 'blocks_per_group': (1, 1, 1, 1), 'use_projection': (True, True, True, True), 'channels_per_group': (128, 256, 512, 512), 'initial_conv_kernel_size': 3, 'initial_conv_stride': 2, 'max_pool': True, 'resnet_v2': True},
  {'image_size': 32, 'batch_size': 256, 'blocks_per_group': (1, 1, 1, 1), 'use_projection': (True, True, True, True), 'channels_per_group': (256, 512, 512, 1024), 'initial_conv_kernel_size': 5, 'initial_conv_stride': 2, 'max_pool': True, 'resnet_v2': True},
  {'image_size': 64, 'batch_size': 256, 'blocks_per_group': (1, 1, 1, 1), 'use_projection': (True, True, True, True), 'channels_per_group': (256, 512, 1024, 1024), 'initial_conv_kernel_size': 3, 'initial_conv_stride': 2, 'max_pool': True, 'resnet_v2': True},
  {'image_size': 64, 'batch_size': 256, 'blocks_per_group': (1, 2, 4, 5), 'use_projection': (True, True, True, True), 'channels_per_group': (256, 512, 1024, 1024), 'initial_conv_kernel_size': 3, 'initial_conv_stride': 2, 'max_pool': True, 'resnet_v2': True},
  {'image_size': 64, 'batch_size': 256, 'blocks_per_group': (1, 1, 3, 4), 'use_projection': (True, True, True, True), 'channels_per_group': (256, 512, 1024, 1024), 'initial_conv_kernel_size': 5, 'initial_conv_stride': 2, 'max_pool': False, 'resnet_v2': True},
]):
  # pyformat: enable, pylint: enable=bad-continuation
  _task_name = 'Resnet_MultiRuntime_%d' % _i
  locals()[_task_name] = gin.external_configurable(
      functools.partial(_ResnetTask, _cfg, name=_task_name), _task_name)
  del _task_name

reason: import jax


text:
#!/usr/bin/env python
# -*- encoding:utf-8 -*-

import inference
import cv2
import numpy as np

image = cv2.imread('./test_data/00.0.jpg')
image = cv2.resize(image, (64, 64))

# np.save('test_inference.npy', image)

result = inference.predict(image)

rank = result['rank']

i = 0
for r in rank:
	i += 1
	print('{} : {}'.format(r['no'], r['accuracy']))

print(i)
# i = 1
# for r in result['rank']:
# 	print('{} : {} : {}'.format(str(i), r['no'], str(r['accuracy'])))
# 	i += 1

max_stars_repo_name: keydepth/facedetect

max_issues_repo_name: keydepth/facedetect

max_forks_repo_name: keydepth/facedetect


text:
import numpy as np
from scipy.stats import gamma as sc_gamma
from scipy.stats import norm as sc_norm
from scipy.special import erfinv

from . import util

#
# Create a radius distribution with tip behavior of order p=1, 2
#

def linear(rmin, rmax, nr=1000):
    return np.linspace(rmin, rmax, nr)

def quadratic(rmin, rmax, nr=1000):
    y = np.linspace(0, 1, nr)
    delta = rmax - rmin
    return rmin + delta * (np.arccos(1 - 2*y) / np.pi)

#
# Normal distribution
#

def normal(mean, stddev, nr=1000, rmax=None):
    cdf = np.linspace(0, 1, nr+2)[1:-1]
    return sc_norm.ppf(cdf, loc=mean, scale=stddev)

#
# Gamma distribution
#

def gamma(shape, scale, nr=1000):
    cdf = np.linspace(0, 1, nr+2)[1:-1]
    return sc_gamma.ppf(cdf, shape, scale=scale)

#
# Take an arbitray density function f and get an (approximate)
# radius vector out of it
#

def from_approx_pdf(f, r0, rmax, nr):
    import scipy.integrate as integrate
    a, _ = integrate.quad(f, r0, rmax)
    r = [r0]
    print(r)
    while r[-1] < rmax:
        r.append(a/(nr*f(r[-1])) + r[-1])

    return np.array(r)[::-1]

#
# Radius rankings corresponding to the family (f_p)
# of self-similar LSW-solutions
#

def lsw(mean, nr=1000, p=np.inf):
    y   = np.linspace(0, 1, nr)
    x   = np.linspace(0, 1.5, 1e6)
    cdf = util.lswcdf_mean(x, p=p)

    rranking = np.zeros(len(y))
    j = 0
    for i in range(0, len(y)):
        if j >= len(x): rranking[i] = mean * x[np.argmax(cdf)]
        while y[i] > cdf[j]: j += 1
        rranking[i] = mean * x[j]

    return rranking[::-1]

max_stars_repo_name: thomas-staudt/rampr

max_issues_repo_name: thomas-staudt/rampr

max_forks_repo_name: thomas-staudt/rampr

reason: import numpy,import scipy,from scipy


text:
import numpy as np
import pandas as pd
import torch
from torch.autograd.variable import Variable
from tqdm import tqdm

from divmachines.classifiers import Classifier
from divmachines.classifiers import BPR_MF
from divmachines.utility.helper import _swap_k, index, re_index
from divmachines.utility.torch import gpu

ITEMS = 'items'
USERS = 'users'

class MF_LFP(Classifier):
    """
    Latent Factor Portfolio implementation based on the following work

    `Shi, Y., Zhao, X., Wang, J., Larson, M., & Hanjalic, A. (2012, August).
    Adaptive diversification of recommendation results via latent factor portfolio.
    In Proceedings of the 35th international ACM SIGIR conference on Research
    and development in information retrieval (pp. 175-184). ACM.`

    Parameters
    ----------
    model: classifier, str or optional
        An instance of `divmachines.classifier.lfp.FM`.
        Default is None
    n_factors: int, optional
        Number of factors to use in user and item latent factors
    sparse: boolean, optional
        Use sparse dataset
    loss: function, optional
        an instance of a Pytorch optimizer or a custom loss.
    l2: float, optional
        L2 loss penalty.
    learning_rate: float, optional
        Initial learning rate.
    optimizer_func: function, optional
        Function that takes in module parameters as the first argument and
        returns an instance of a Pytorch optimizer. Overrides l2 and learning
        rate if supplied. If no optimizer supplied, then use SGD by default.
    n_iter: int, optional
        Number of iterations to run.
    batch_size: int, optional
        Mini batch size.
    random_state: instance of numpy.random.RandomState, optional
        Random state to use when fitting.
    use_cuda: boolean, optional
        Run the models on a GPU.
    device_id: int, optional
        GPU device ID to which the tensors are sent.
        If set use_cuda must be True.
        By Default uses all GPU available.
    logger: :class:`divmachines.logging`, optional
        A logger instance for logging during the training process
    n_jobs: int, optional
        Number of jobs for data loading.
        Default is 0, it means that the data loader runs in the main process.
    early_stopping: bool, optional
        Performs a dump every time to enable early stopping.
        Default False.
        n_iter_no_change : int, optional, default 10
        Maximum number of epochs to not meet ``tol`` improvement.
        Only effective when solver='sgd' or 'adam'
    n_iter_no_change : int, optional, default 10
        Maximum number of epochs to not meet ``tol`` improvement.
        Only effective when solver='sgd' or 'adam'
    tol : float, optional, default 1e-4
        Tolerance for the optimization. When the loss or score is not improving
        by at least ``tol`` for ``n_iter_no_change`` consecutive iterations,
        convergence is considered to be reached and training stops.
    stopping: bool, optional
    frac: float, optional
        Fraction of Negative Item sampling for BPR
    """

    def __init__(self,
                 model=None,
                 n_factors=10,
                 sparse=False,
                 n_iter=10,
                 loss=None,
                 l2=0.0,
                 learning_rate=1e-2,
                 optimizer_func=None,
                 batch_size=None,
                 random_state=None,
                 use_cuda=False,
                 device_id=None,
                 logger=None,
                 n_jobs=0,
                 pin_memory=False,
                 verbose=False,
                 early_stopping=False,
                 n_iter_no_change=10,
                 tol=1e-4,
                 stopping=True,
                 frac=0.8):
        self._model = model
        self._n_factors = n_factors
        self._sparse = sparse
        self._n_iter = n_iter
        self._loss = loss
        self._l2 = l2
        self._learning_rate = learning_rate
        self._optimizer_func = optimizer_func
        self._batch_size = batch_size
        self._random_state = random_state
        self._use_cuda = use_cuda
        self._logger = logger
        self._n_jobs = n_jobs
        self._pin_memory = pin_memory
        self._verbose = verbose
        self._early_stopping = early_stopping
        self._tol = tol
        self._n_iter_no_change = n_iter_no_change
        self._stopping = stopping
        self._frac = frac
        if device_id is not None and not self._use_cuda:
            raise ValueError("use_cuda flag must be true")
        self._device_id = device_id

    @property
    def n_users(self):
        return self._model.n_users

    @n_users.getter
    def n_users(self):
        return self._model.n_users

    @property
    def n_items(self):
        return self._model.n_items

    @n_items.getter
    def n_items(self):
        return self._model.n_items

    @property
    def logger(self):
        return self._logger

    def _initialize(self):
        self._init_model()

    def _init_model(self):
        if self._model is None:
            self._model = BPR_MF(n_factors=self._n_factors,
                                 sparse=self._sparse,
                                 n_iter=self._n_iter,
                                 l2=self._l2,
                                 learning_rate=self._learning_rate,
                                 optimizer_func=self._optimizer_func,
                                 batch_size=self._batch_size,
                                 random_state=self._random_state,
                                 use_cuda=self._use_cuda,
                                 device_id=self._device_id,
                                 logger=self._logger,
                                 n_jobs=self._n_jobs,
                                 pin_memory=self._pin_memory,
                                 verbose=self._verbose,
                                 early_stopping=self._early_stopping,
                                 n_iter_no_change=self._n_iter_no_change,
                                 tol=self._tol,
                                 stopping=self._stopping,
                                 frac=self._frac)
        elif isinstance(self._model, str):
            self._model = BPR_MF(model=self._model,
                                 n_factors=self._n_factors,
                                 sparse=self._sparse,
                                 n_iter=self._n_iter,
                                 l2=self._l2,
                                 learning_rate=self._learning_rate,
                                 optimizer_func=self._optimizer_func,
                                 batch_size=self._batch_size,
                                 random_state=self._random_state,
                                 use_cuda=self._use_cuda,
                                 device_id=self._device_id,
                                 logger=self._logger,
                                 n_jobs=self._n_jobs,
                                 pin_memory=self._pin_memory,
                                 verbose=self._verbose,
                                 early_stopping=self._early_stopping,
                                 n_iter_no_change=self._n_iter_no_change,
                                 tol=self._tol,
                                 stopping=self._stopping,
                                 frac=self._frac)
        elif not isinstance(self._model, BPR_MF):
            raise ValueError("Model must be an instance of "
                             "divmachines.classifiers.lfp.MF class")

    def _init_dataset(self, x, train=True):
        self._build_indexes()
        if train:
            self._estimate_variance(x)

    def _build_indexes(self):
        self._rev_item_index = {}
        self._user_index = {}
        self._item_index = {}
        for k, v in self._model.index.items():
            if k.startswith(ITEMS):
                self._rev_item_index[str(v)] = k[len(ITEMS):]
                self._item_index[k[len(ITEMS):]] = v
            elif k.startswith(USERS):
                self._user_index[k[len(USERS):]] = v

            else:
                raise ValueError("Not possible")

    def fit(self, x, y, n_users=None, n_items=None):
        """
        Fit the underlying classifier.
        When called repeatedly, models fitting will resume from
        the point at which training stopped in the previous fit
        call.

        Parameters
        ----------
        x: ndarray
            Training samples. User column must be 0 while item column
            must be 1
        y: ndarray
            Target values for samples
        n_users: int, optional
            Total number of users. The model will have `n_users` rows.
            Default is None, `n_users` will be inferred from `x`.
        n_items: int, optional
            Total number of items. The model will have `n_items` columns.
            Default is None, `n_items` will be inferred from `x`.
        """
        self._initialize()

        if x.shape[1] != 2:
            raise ValueError("x must have two columns: users and items cols")

        dic = {USERS: 0, ITEMS: 1}

        self._model.fit(x, y, dic=dic, n_users=n_users, n_items=n_items)
        self._init_dataset(x)

        if self._early_stopping:
            self._prepare()

    def _prepare(self):
        dump = self._model.dump
        dump['variance'] = self.torch_variance().cpu().state_dict()
        self.dump = dump

    def save(self, path):
        torch.save(self.dump, path)

    def predict(self, x, top=10, b=0.5):
        """
        Predicts

        Parameters
        ----------
        x: ndarray or int
            array of users, user item interactions matrix
            (you must provide the same items for all user
            listed) or instead a single user to which
            predict the item ranking.
        top: int, optional
            Length of the ranking
        b: float, optional
            System-level Diversity.
            It controls the trade-off for all users between
            accuracy and diversity.
        Returns
        -------
        top-k: ndarray
            `top` items for each user supplied
        """
        file = None
        load = isinstance(self._model, str)
        if load:
            file = self._model
            self._initialize()

        n_users = np.unique(x[:, 0]).shape[0]
        n_items = np.unique(x[:, 1]).shape[0]

        try:
            rank = self._model.predict(x).reshape(n_users, n_items)
        except ValueError:
            raise ValueError("You may want to provide for each user "
                             "the item catalog as transaction matrix.")

        # Rev-indexes and other data structures are updated
        # if new items and new users are provided.
        if load:
            self._build_indexes()
            variance = torch.nn.Embedding(self.n_users, self._n_factors)
            variance.load_state_dict(
                torch.load(file,
                           map_location=lambda storage, loc: storage)['variance'])
            self._var = variance.weight.data.numpy()

        self._init_dataset(x, train=False)

        users = index(np.array([x[i, 0] for i in sorted(
            np.unique(x[:, 0], return_index=True)[1])]), self._user_index)
        items = index(np.array([x[i, 1] for i in sorted(
            np.unique(x[:, 1], return_index=True)[1])]), self._item_index)

        re_ranking = self._sequential_re_ranking(rank, users, items, top, b)

        return re_ranking

    def _sequential_re_ranking(self, rank, users, items, top, b):
        rank = np.argsort(-rank, 1)

        re_index(items, rank)

        x = self._model.x
        y = self._model.y
        var = self.torch_variance()

        for k in tqdm(range(1, top),
                      desc="Sequential Re-ranking",
                      leave=False,
                      disable=not self._verbose):
            values = self._compute_delta_f(x, y, k, b, var, rank, users)

            arg_max_per_user = np.argsort(values, 1)[:, -1].copy()
            _swap_k(arg_max_per_user, k, rank)

        return index(rank[:, :top], self._rev_item_index)

    def _compute_delta_f(self, x, y, k, b, var, rank, users):
        # Initialize Variables
        # and other coefficients
        u_idx = Variable(gpu(torch.from_numpy(users),
                             self._use_cuda,
                             self._device_id))
        i_idx = Variable(gpu(torch.from_numpy(rank),
                             self._use_cuda,
                             self._device_id))

        wk = 1/(2**k)
        wm = Variable(gpu(torch.from_numpy(
            np.array([1 / (2 ** m) for m in range(k)],
                     dtype=np.float32)),
            self._use_cuda,
            self._device_id)) \
            .unsqueeze(1).expand(k, self._n_factors)

        i_ranked = (y(i_idx[:, :k]) * wm).transpose(0, 1).unsqueeze(0)
        i_unranked = y(i_idx[:, k:]).transpose(0, 1)

        term0 = self._first_term(i_unranked, u_idx, x)

        term1 = self._second_term(b, i_unranked, u_idx, var, wk)

        term2 = self._third_term(b,
                                 k,
                                 u_idx,
                                 var,
                                 i_ranked,
                                 i_unranked,
                                 len(users),
                                 rank.shape[1])

        delta_f = torch.mul(term0 - term1 - term2, wk) \
            .sum(2).transpose(0, 1)

        return delta_f.cpu().data.numpy()

    def _third_term(self,
                    b, k, u_idx, var,
                    i_ranked, i_unranked,
                    n_users, n_items):
        # This block of code computes the third term of the DeltaF
        e_ranked = i_ranked.expand(n_items - k,
                                   k,
                                   n_users,
                                   self._n_factors) \
            .transpose(0, 1)
        e_unranked = i_unranked.unsqueeze(0).expand(k,
                                                    n_items - k,
                                                    n_users,
                                                    self._n_factors)

        coeff1 = torch.mul(var(u_idx), 2. * b)
        term2 = (e_ranked * e_unranked).sum(0) * coeff1
        return term2

    def _second_term(self, b, i_unranked, u_idx, var, wk):
        # This block of code computes the second term of the DeltaF
        term1 = torch.pow(i_unranked, 2) * var(u_idx)
        coeff0 = b * wk
        term1 = torch.mul(term1, coeff0)
        return term1

    def _first_term(self, i_unranked, u_idx, x):
        # This block of code computes the first term of the DeltaF.
        users_batch = x(u_idx)
        term0 = (users_batch * i_unranked)
        return term0

    def torch_variance(self):
        var_v = torch.from_numpy(self._var)
        var = torch.nn.Embedding(var_v.size(0), var_v.size(1))
        var.weight = torch.nn.Parameter(var_v)
        var = gpu(var,
                  self._use_cuda,
                  self._device_id)
        return var

    def _estimate_variance(self, x):
        train = index_dataset(x, self._user_index, self._item_index)
        x = self._model.x
        y = self._model.y

        self._var = np.zeros((self.n_users, self._n_factors),
                             dtype=np.float32)

        for i, (u, g) in tqdm(enumerate(pd.DataFrame(train).groupby(0)),
                              desc="Var. Estimate",
                              leave=False,
                              disable=not self._verbose):
            user_profile = g.values[:, 1].astype(np.int64)
            upl = user_profile.shape[0]
            user_idx = Variable(gpu(torch.from_numpy(np.array([u])),
                                    self._use_cuda,
                                    self._device_id))
            item_idx = Variable(gpu(torch.from_numpy(user_profile),
                                    self._use_cuda,
                                    self._device_id))
            diff = x(user_idx) - y(item_idx)

            prod = torch.pow(diff, 2).sum(0)

            var = torch.div(prod, upl)
            self._var[i, :] = var.cpu().data.numpy()

def index_dataset(x, idx0, idx1):
    user_profile = x.copy()
    users = index(user_profile[:, 0], idx0)
    items = index(user_profile[:, 1], idx1)
    user_profile[:, 0] = users
    user_profile[:, 1] = items
    return user_profile


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import ipywidgets as widgets
from IPython.display import display, clear_output, Math
from qiskit.visualization import plot_bloch_vector
from numpy import sqrt, cos, sin, pi

from qiskit_textbook.widgets._helpers import _pre, _img

def binary_widget(nbits=5):
    nbits = max(min(10, nbits), 2) # Keep nbits between 2 and 10

    output = _pre()
    bits = ['0' for i in range(nbits)]
    button_list = [widgets.ToggleButton(description=str(2**i),
                                        layout=widgets.Layout(width='3em',height='3em'))
                   for i in reversed(range(nbits))]

    hbox = widgets.HBox(button_list)
    label1 = widgets.Label(value="Toggle the bits below to change the binary number.")
    label2 = widgets.Label(value="Think of a number between 0 and %i and try to write it down in binary." % ((2**nbits)-1))
    vbox = widgets.VBox([label1,label2,hbox])
    def on_btn_click(b):
        for b in range(nbits):
            if button_list[b].value:
                bits[b] = '1'
            else:
                bits[b] = '0'
        string = "".join(bits)
        output.value = "Binary" + " "*(nbits//2) + " Decimal\n " + string + " = " + str(int(string,2))

    for button in button_list: button.observe(on_btn_click)

    # Add output before button press
    string = "".join(bits)
    output.value = "Binary" + " "*(nbits//2) + " Decimal\n " + string + " = " + str(int(string,2))

    display(vbox)
    display(output.widget)

def state_vector_exercise(target):
    output = _pre()
    button = widgets.Button(description="Check", layout=widgets.Layout(width='5em'))
    text_input = widgets.Text(value='[1, 0]',
                              placeholder='Type something',
                              width='50px',
                              disabled=False)

    label = widgets.Label(value="State Vector:")

    def on_button_click(b):
        try:
            state_vector = eval(text_input.value)
            c1, c2 = state_vector[0], state_vector[1]
        except Exception as e:
            output.value = str(e).split("(")[0]
            return

        squared_magnitude = abs(c1)**2 + abs(c2)**2
        p = abs(c1)**2
        if not (squared_magnitude < 1.01 and squared_magnitude > .99): # Close Enough
            output.value = "Magnitude is not equal to 1"
            return
        elif p > target*0.99 and p < target*1.01:
            output.value = "Correct!"
        else:
            output.value = "The absolute value of " + str(c1) + ", squared is not equal to " + str(target)

    hbox = widgets.HBox([text_input, button])
    vbox = widgets.VBox([label, hbox])
    button.on_click(on_button_click)

    display(vbox)
    display(output.widget)

def bloch_calc():
    output = _pre()
    button = widgets.Button(description="Plot", layout=widgets.Layout(width='4em'))
    theta_input = widgets.Text(label='$\\theta$',
                               placeholder='Theta',
                               disabled=False)
    phi_input = widgets.Text(label='$\phi$',
                             placeholder='Phi',
                             disabled=False)

    label = widgets.Label(value="Define a qubit state using $\\theta$ and $\phi$:")
    image = _img(value=plot_bloch_vector([0, 0, 1]))
    def on_button_click(b):
        from math import pi, sqrt
        try:
            theta = eval(theta_input.value)
            phi = eval(phi_input.value)
        except Exception as e:
            output.value = "Error: " + str(e)
            return
        x = sin(theta)*cos(phi)
        y = sin(theta)*sin(phi)
        z = cos(theta)
        # Remove horrible almost-zero results
        if abs(x) < 0.0001:
            x = 0
        if abs(y) < 0.0001:
            y = 0
        if abs(z) < 0.0001:
            z = 0
        output.value = "x = r * sin(" + theta_input.value + ") * cos(" + phi_input.value + ")\n"
        output.value += "y = r * sin(" + theta_input.value + ") * sin(" + phi_input.value + ")\n"
        output.value += "z = r * cos(" + theta_input.value + ")\n\n"
        output.value += "Cartesian Bloch Vector = [" + str(x) + ", " + str(y) + ", " + str(z) + "]"
        image.value = plot_bloch_vector([x,y,z])

    hbox = widgets.HBox([phi_input, button])
    vbox = widgets.VBox([label, theta_input, hbox])
    button.on_click(on_button_click)
    display(vbox)
    display(output.widget)
    display(image.widget)

def plot_bloch_vector_spherical(coords):
    clear_output()
    theta, phi, r = coords[0], coords[1], coords[2]
    x = r*sin(theta)*cos(phi)
    y = r*sin(theta)*sin(phi)
    z = r*cos(theta)
    output = widgets.Output()
    return plot_bloch_vector([x,y,z])

def scalable_circuit(func):
    """Makes a scalable circuit interactive. Function must take
    qc (QuantumCircuit) and number of qubits (int) as positional inputs"""
    from qiskit import QuantumCircuit
    def interactive_function(n):
        qc = QuantumCircuit(n)
        func(qc, n)
        return qc.draw('mpl')

    from ipywidgets import IntSlider
    # Ideally this would use `interact` from ipywidgets but this is
    # incompatible with thebe lab
    image = _img()
    n_slider = IntSlider(min=1,max=8,step=1,value=4)
    image.value = interactive_function(n_slider.value)
    def update_output(b):
        image.value = interactive_function(n_slider.value)
    n_slider.observe(update_output)
    display(n_slider)
    display(image.widget)

def gate_demo(gates='full',qsphere=True):
    from qiskit import QuantumCircuit, execute, Aer
    from qiskit.visualization import plot_bloch_multivector, plot_state_qsphere
    gate_list = []
    showing_rz = False
    if 'pauli' in gates:
        gate_list += ['X','Y','Z']
    if '+h' in gates:
        gate_list.append('H')
    if '+rz' in gates:
        showing_rz = True
    if gate_list == [] or gates == 'full':
        gate_list = ['I','X','Y','Z','H','S','Sdg','T','Tdg']
        showing_rz = True

    backend = Aer.get_backend('statevector_simulator')
    qc = QuantumCircuit(1)
    button_list = [widgets.Button(description=gate, layout=widgets.Layout(width='3em', height='3em')) for gate in gate_list]
    button_list.append(widgets.Button(description='Reset', layout=widgets.Layout(width='6em', height='3em')))
    image = _img()
    def update_output():
        out_state = execute(qc,backend).result().get_statevector()
        if qsphere:
            image.value = plot_state_qsphere(out_state)
        else:
            image.value = plot_bloch_multivector(out_state)

    def apply_gates(b,qc):
        functionmap = {
            'X':qc.x,
            'Y':qc.y,
            'Z':qc.z,
            'H':qc.h,
            'S':qc.s,
            'T':qc.t,
            'Sdg':qc.sdg,
            'Tdg':qc.tdg,
        }
        if b.description == 'I':
            pass
        elif b.description == 'Reset':
            qc.data = []
        elif b.description == 'Rz':
                qc.rz(zrot_slider.value,0)
        else:
            functionmap[b.description](0)

    def on_button_click(b):
        apply_gates(b,qc)
        update_output()

    for button in button_list:
        button.on_click(on_button_click)
    if showing_rz:
        rz_button = widgets.Button(description='Rz', layout=widgets.Layout(width='3em', height='3em'))
        rz_button.on_click(on_button_click)
        zrot_slider = widgets.FloatSlider(value=pi,
                                         min= -pi,
                                         max= pi,
                                         disabled=False,
                                         readout_format='.2f')
    qc = QuantumCircuit(1)
    update_output()

    if showing_rz:
        top_box = widgets.HBox(button_list)
        bottom_box = widgets.HBox([rz_button, zrot_slider])
        main_box = widgets.VBox([top_box, bottom_box])
    else:
        main_box = widgets.HBox(button_list)

    display(main_box)
    display(image.widget)

def bv_widget(nqubits, hidden_string, display_ancilla=False, hide_oracle=True):
    if nqubits < 1:
        print("nqubits must be 1 or greater, setting to 1.")
        nqubits = 1
    if nqubits < len(hidden_string):
        difference = len(hidden_string) - nqubits
        hidden_string = hidden_string[difference:]
        print("Error: s is too long, trimming the first %i bits and using '%s' instead." % (difference, hidden_string))
    import numpy as np
    from qiskit_textbook.tools import num_to_latex, array_to_latex
    from qiskit import QuantumCircuit, Aer, execute
    backend = Aer.get_backend('statevector_simulator')
    nqubits += 1
    if hide_oracle:
        oracle_qc = QuantumCircuit(nqubits)
        q = 0
        for char in hidden_string:
            if char == "1":
                oracle_qc.cx(q,nqubits-1)
            q += 1
        oracle_gate = oracle_qc.to_gate()
    qc = QuantumCircuit(nqubits)
    qc.h(nqubits-1)
    qc.z(nqubits-1)
    class Message():
        def __init__(self):
            if display_ancilla:
                self.ops = "|{-}\\rangle\\otimes|" + "0"*(nqubits-1) + "\\rangle"
                self.vec = "|{-}\\rangle\\otimes|" + "0"*(nqubits-1) + "\\rangle"
            else:
                self.ops = "|" + "0"*(nqubits-1) + "\\rangle"
                self.vec = "|" + "0"*(nqubits-1) + "\\rangle"

    msg = Message()
    def vec_in_braket(vec, nqubits):
        scalfac = ""
        tensorfac = ""
        state = ""
        # Factor out separable 'output' qubit if possible
        if nqubits > 1:
            vfirst = vec[:2**nqubits//2]
            vlast = vec[2**nqubits//2:]
            if np.allclose(vfirst, 0):
                vec = vlast
                tensorfac += "|1\\rangle"
                nqubits -= 1
            elif np.allclose(vlast, 0):
                vec = vfirst
                tensorfac += "|0\\rangle"
                nqubits -= 1
            elif np.allclose(vfirst, vlast):
                vec = vfirst*np.sqrt(2)
                tensorfac += "|{+}\\rangle"
                nqubits -= 1
            elif np.allclose(vfirst, -vlast):
                vec = vfirst*np.sqrt(2)
                tensorfac += "|{-}\\rangle"
                nqubits -= 1

        if np.allclose(np.abs(vec), np.abs(vec[0])):
            scalfac = num_to_latex(vec[0])
            vec = vec/vec[0]

        for i in range(len(vec)):
            if not np.isclose(vec[i], 0):
                basis = format(i, 'b').zfill(nqubits)
                if not np.isclose(vec[i], 1):
                    if np.isclose(vec[i], -1):
                        if state.endswith("+ "):
                            state = state[:-2]
                        state += "-"
                    else:
                        state += num_to_latex(vec[i])
                state += "|" + basis +"\\rangle + "
        state = state.replace("j", "i")
        state = state[:-2]
        if len(state) > 5000:
            return "\\text{(Too large to display)}"
        if scalfac != "" or (tensorfac != "" and len(state)>(9+nqubits) and display_ancilla):
            state = ("(%s)" % state)
        if scalfac != "":
            state = scalfac + state
        if tensorfac != "" and display_ancilla:
            state =  tensorfac + "\otimes" + state
        return state

    def hadamards(qc, nqubits):
        for q in range(nqubits-1):
            qc.h(q)

    def oracle(qc, nqubits):
        if hide_oracle:
            qc.append(oracle_gate, range(nqubits))
        else:
            qc.barrier()
            q = 0
            for char in hidden_string:
                if char == "1":
                    qc.cx(q,nqubits-1)
                q += 1
            qc.barrier()

    def update_output():
        statevec = execute(qc, backend).result().get_statevector()
        msg.vec = vec_in_braket(statevec, nqubits)
        html_math.value = "$$ %s = %s $$" % (msg.ops, msg.vec)
        image.value = qc.draw('mpl')

    def on_hads_click(b):
        hadamards(qc, nqubits)
        if display_ancilla:
            msg.ops = "|{-}\\rangle\\otimes H^{\\otimes n}" + msg.ops[18:]
        else:
            msg.ops = "H^{\\otimes n}" + msg.ops
        update_output()
    def on_oracle_click(b):
        oracle(qc, nqubits)
        if display_ancilla:
            msg.ops = "|{-}\\rangle\\otimes U_f" + msg.ops[18:]
        else:
            msg.ops = "U_f" + msg.ops
        update_output()

    def on_clear_click(b):
        for i in range(len(qc.data)-2):
            qc.data.pop()
        msg.__init__()
        update_output()

    hads_btn = widgets.Button(description="H⊗ⁿ")
    hads_btn.on_click(on_hads_click)
    oracle_btn = widgets.Button(description="Oracle")
    oracle_btn.on_click(on_oracle_click)
    clear_btn = widgets.Button(description="Clear")
    clear_btn.on_click(on_clear_click)

    hbox = widgets.HBox([hads_btn, oracle_btn, clear_btn])
    html_math = widgets.HTMLMath()
    html_math.value = "$$ %s = %s $$" % (msg.ops, msg.vec)
    image = _img()
    image.value = qc.draw('mpl')
    display(hbox, html_math, image.widget)

def dj_widget(size="small", case="balanced", display_ancilla=False, hide_oracle=True):
    size, case = size.lower(), case.lower()
    if case not in ["balanced", "constant"]:
        print("Error: `case` must be 'balanced' or 'constant'")
        return
    if size not in ["small", "large"]:
        print("Error: `size` must be 'small' or 'large'")
        return
    import numpy as np
    import random
    from qiskit_textbook.tools import num_to_latex, array_to_latex
    from qiskit_textbook.problems import dj_problem_oracle
    from qiskit import QuantumCircuit, Aer, execute
    if case == 'balanced':
        problem = random.choice([1,3,4])
    else:
        problem = 2
    backend = Aer.get_backend('statevector_simulator')
    if size == "small":
        oracle = QuantumCircuit(3)
        if case == "balanced":
            if problem == 1:
                oracle.cx(0,2)
                oracle.cx(1,2)
            elif problem == 3:
                oracle.cx(0,2)
            elif problem == 4:
                oracle.ccx(0,1,2)
                oracle.x(0)
                oracle.x(1)
                oracle.ccx(0,1,2)
                oracle.x(0)
                oracle.x(1)
        else:
            oracle.i(2)
    else:
        oracle = dj_problem_oracle(problem, to_gate=False)
    if hide_oracle:
        oracle = oracle.to_gate()
    if size == "small":
        nqubits = 3
    else:
        nqubits = 5
    qc = QuantumCircuit(nqubits)
    qc.h(nqubits-1)
    qc.z(nqubits-1)
    class Message():
        def __init__(self):
            if display_ancilla:
                self.ops = "|{-}\\rangle\\otimes|" + "0"*(nqubits-1) + "\\rangle"
                self.vec = "|{-}\\rangle\\otimes|" + "0"*(nqubits-1) + "\\rangle"
            else:
                self.ops = "|" + "0"*(nqubits-1) + "\\rangle"
                self.vec = "|" + "0"*(nqubits-1) + "\\rangle"

    msg = Message()
    def vec_in_braket(vec, nqubits):
        scalfac = ""
        tensorfac = ""
        state = ""
        # Factor out separable 'output' qubit if possible
        if nqubits > 1:
            vfirst = vec[:2**nqubits//2]
            vlast = vec[2**nqubits//2:]
            if np.allclose(vfirst, 0):
                vec = vlast
                tensorfac += "|1\\rangle"
                nqubits -= 1
            elif np.allclose(vlast, 0):
                vec = vfirst
                tensorfac += "|0\\rangle"
                nqubits -= 1
            elif np.allclose(vfirst, vlast):
                vec = vfirst*np.sqrt(2)
                tensorfac += "|{+}\\rangle"
                nqubits -= 1
            elif np.allclose(vfirst, -vlast):
                vec = vfirst*np.sqrt(2)
                tensorfac += "|{-}\\rangle"
                nqubits -= 1

        if np.allclose(np.abs(vec), np.abs(vec[0])):
            scalfac = num_to_latex(vec[0])
            vec = vec/vec[0]

        for i in range(len(vec)):
            if not np.isclose(vec[i], 0):
                basis = format(i, 'b').zfill(nqubits)
                if not np.isclose(vec[i], 1):
                    if np.isclose(vec[i], -1):
                        if state.endswith("+ "):
                            state = state[:-2]
                        state += "-"
                    else:
                        state += num_to_latex(vec[i])
                state += "|" + basis +"\\rangle + "
        state = state.replace("j", "i")
        state = state[:-2]
        if len(state) > 5000:
            return "\\text{(Too large to display)}"
        if scalfac != "" or (tensorfac != "" and len(state)>(9+nqubits) and display_ancilla):
            state = ("(%s)" % state)
        if scalfac != "":
            state = scalfac + state
        if tensorfac != "" and display_ancilla:
            state =  tensorfac + "\otimes" + state
        return state

    def hadamards(qc, nqubits):
        for q in range(nqubits-1):
            qc.h(q)

    def apply_oracle(qc, nqubits):
        if hide_oracle:
            qc.append(oracle, range(nqubits))
        else:
            qc.barrier()
            qc += oracle
            qc.barrier()

    def update_output():
        statevec = execute(qc, backend).result().get_statevector()
        msg.vec = vec_in_braket(statevec, nqubits)
        html_math.value = "$$ %s = %s $$" % (msg.ops, msg.vec)
        image.value = qc.draw('mpl')

    def on_hads_click(b):
        hadamards(qc, nqubits)
        if display_ancilla:
            msg.ops = "|{-}\\rangle\\otimes H^{\\otimes n}" + msg.ops[18:]
        else:
            msg.ops = "H^{\\otimes n}" + msg.ops
        update_output()
    def on_oracle_click(b):
        apply_oracle(qc, nqubits)
        if display_ancilla:
            msg.ops = "|{-}\\rangle\\otimes U_f" + msg.ops[18:]
        else:
            msg.ops = "U_f" + msg.ops
        update_output()

    def on_clear_click(b):
        for i in range(len(qc.data)-2):
            qc.data.pop()
        msg.__init__()
        update_output()

    hads_btn = widgets.Button(description="H⊗ⁿ")
    hads_btn.on_click(on_hads_click)
    oracle_btn = widgets.Button(description="Oracle")
    oracle_btn.on_click(on_oracle_click)
    clear_btn = widgets.Button(description="Clear")
    clear_btn.on_click(on_clear_click)

    hbox = widgets.HBox([hads_btn, oracle_btn, clear_btn])
    html_math = widgets.HTMLMath()
    html_math.value = "$$ %s = %s $$" % (msg.ops, msg.vec)
    image = _img()
    image.value = qc.draw('mpl')
    display(hbox, html_math, image.widget)

max_stars_repo_name: exAClior/qiskit-textbook

max_issues_repo_name: exAClior/qiskit-textbook

max_forks_repo_name: exAClior/qiskit-textbook

reason: import numpy,from numpy


text:
import numpy as np
from sklearn.base import BaseEstimator, ClassifierMixin
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
import xgboost as xgb

class GradientBoostingRegressor(BaseEstimator, ClassifierMixin):
    """
    Gradient boosting regressor implementation

    Args:
        params (dict): Model parameters. If None, use default pre-set values.
        n_rounds (int): Number of training rounds.
        objective (str): Prediction objecitve.
        f_to_name (dict): Dictionary mapping feature enumerations in the form 'f0', 'f1', ...
        to their names.

    Attributes:
        params (dict): Model parameters. If None, use default pre-set values.
        objective (str): Prediction objecitve.
        n_rounds (int): Number of training rounds.
    """

    def __init__(self, params=None, n_rounds=2000, objective='reg:linear', f_to_name=None):

        # Set parameters.
        if params is None:
            self.params = {
                'booster':            'gbtree',
                'objective':          objective,
                'learning_rate':      0.05,
                'max_depth':          14,
                'subsample':          0.9,
                'colsample_bytree':   0.7,
                'colsample_bylevel':  0.7,
                'silent':             1,
                'feval':              'rmsle'
            }
        else:
            self.params = params

        # Set prediction objective.
        self.objective = objective

        # Set number of rounds.
        self.n_rounds = n_rounds

    def fit(self, X, y):
        """
        Fit classifier to training data.

        Args:
            X (numpy.ndarray): Training data samples
            y (numpy.ndarray): Training data labels

        Returns:
            (obj): Reference to self
        """

        # Split training data into training and validation sets.
        data_train, data_val, target_train, target_val = train_test_split(X, y, test_size=0.2)

        # Define train and validation sets in required format.
        dtrain = xgb.DMatrix(data_train, np.log(target_train + 1))
        dval = xgb.DMatrix(data_val, np.log(target_val+1))
        watchlist = [(dval, 'eval'), (dtrain, 'train')]

        # Train model.
        gbm = xgb.train(self.params,
                        dtrain,
                        num_boost_round = self.n_rounds,
                        evals = watchlist,
                        verbose_eval = True
                        )
        self._gbm = gbm

        # Return reference to self.
        return self

    def predict(self, X, y=None):
        """
        Predict labels of new data.

        Args:
            X (numpy.ndarray): Data for which to predict target values

        Returns:
            (numpy.ndarray): Predicted target values
        """

        # Return labels with highest probability.
        return np.exp(self._gbm.predict(xgb.DMatrix(X))) - 1

    def score_features(self, f_to_name):
        """
        Score feature importances.

        Args:
            f_to_name (dict): Dictionary mapping feature enumerations such as 'f0', 'f1', ...
            to feature names.

        Returns:
            (dict): Dictionary mapping feature names as defined in f_to_name parameter to
            their estimated importances.
        """

        f_scores = self._gbm.get_fscore()
        sum_f_scores = sum(f_scores.values())
        return {f_to_name[key] : val/sum_f_scores for key, val in f_scores.items()}


text:
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"   # see issue #152
os.environ["CUDA_VISIBLE_DEVICES"] = ""

import numpy as np
from collections import deque
from keras.models import Model
from keras.layers import Input, Dense, BatchNormalization, LSTM, TimeDistributed, Dot
import keras.optimizers as optimizers
from keras.utils import to_categorical
import gym

class Environment:
    def __init__(self):
        pass

    def is_end(self):
        """ returns true if is end step
        """
        pass

    def reset(self):
        """ reset internal states and returns initial state
        """
        pass

    def step(self, action):
        """ returns (new_state, reward, is_end)
        """

class Memory:
    def __init__(self, max_memory_size, trace_size):
        self.max_memory_size = max_memory_size
        self.trace_size = trace_size
        self.state_memory = []
        self.action_memory = []
        self.reward_memory = []
        self.episode_sizes = []
        self.total_size = 0

    def clear(self):
        self.state_memory = []
        self.action_memory = []
        self.reward_memory = []
        self.episode_sizes = []
        self.total_size = 0

    def remember_episode(self, states, actions, rewards):
        """ An episode is a time series of memories.
            Each of format (state, action, reward, new_state, is_end)
        """
        self.state_memory.append(states)
        self.action_memory.append(actions)
        self.reward_memory.append(rewards)
        len_e = actions.shape[0] - self.trace_size + 1
        self.episode_sizes.append(len_e)
        self.total_size += len_e
        istart = 0
        while self.total_size > self.max_memory_size:
            self.total_size -= self.episode_sizes[istart]
            istart += 1
        self.episode_sizes = self.episode_sizes[istart:]
        self.state_memory = self.state_memory[istart:]
        self.action_memory = self.action_memory[istart:]
        self.reward_memory = self.reward_memory[istart:]

    def replay(self, batch_size):
        ez = np.array(self.episode_sizes)
        if np.min(ez) <= 0:
            print(ez)
        episodes = np.random.choice(len(self.episode_sizes), size=batch_size, p=ez / self.total_size)
        sizes = ez[episodes]
        indices = (np.random.random(size=batch_size) * sizes).astype(np.int)
        # indices = (np.array(sizes) - 1).astype(np.int)

        state_traces = [self.state_memory[episodes[i]][indices[i]:indices[i] + self.trace_size + 1] for i in range(batch_size)]
        action_traces = [self.action_memory[episodes[i]][indices[i]:indices[i] + self.trace_size] for i in range(batch_size)]
        reward_traces = [self.reward_memory[episodes[i]][indices[i]:indices[i] + self.trace_size] for i in range(batch_size)]
        end_traces = [[j == sizes[i] + self.trace_size - 2 for j in range(indices[i], indices[i] + self.trace_size)] for i in range(batch_size)]
        # if np.any(end_traces):
        #     print('End')
        if np.any(np.array(end_traces)[:, :-1]):
            print('Wrong!')
        return np.array(state_traces), np.array(action_traces), np.array(reward_traces), np.array(end_traces)

class DeepRecurrentQNetwork:
    def __init__(
        self,
        env,
        hidden_sizes,
        max_episode_size,
        skip_front,
        trace_size,
        memory_size,
        discount_rate=1.0,
        learning_rate=0.001
    ):
        self.env = env
        self.hidden_sizes = hidden_sizes
        self.memory = Memory(memory_size, trace_size)
        self.max_episode_size = max_episode_size
        self.skip_front = skip_front
        self.trace_size = trace_size
        self.gamma = discount_rate    # discount rate
        self.learning_rate = learning_rate
        # TODO: Here assumes action space is 1-d discrete
        self.action_size = self.env.action_space.n
        self.main_model = self._build_model(1)

    def _build_model(self, batch_size, include_train_model=False):
        # TODO: Here assumes state space is 1-d
        state_size = self.env.observation_space.shape[0]

        input = Input(batch_shape=(batch_size, None, state_size), name='input')
        lstm_out = LSTM(self.hidden_sizes[0], stateful=True, return_sequences=True, name='lstm')(input)
        qout = TimeDistributed(Dense(self.action_size, activation=None), name='qout')(lstm_out)
        model = Model(inputs=[input], outputs=[qout])
        model.compile(loss={'qout': 'logcosh'}, optimizer=optimizers.adam(lr=self.learning_rate))
        if not include_train_model:
            return model

        action_input = Input(batch_shape=(batch_size, None, self.action_size), name='action')
        q = Dot(axes=-1)([qout, action_input])
        model2 = Model(inputs=[input, action_input], outputs=[q])
        model2.compile(loss='logcosh', optimizer=optimizers.adam(lr=self.learning_rate))
        return model, model2

    def run(self):
        env = self.env
        self.main_model.reset_states()
        input_state = env.reset()
        is_end = False
        while not is_end:
            env.render()
            qs = self.main_model.predict(input_state.reshape(1, 1, -1), batch_size=1)
            action = np.argmax(qs.ravel(), axis=-1)
            input_state, reward, is_end, info = env.step(action)

    def train(
            self,
            update_freq=10,
            batch_size=4,
            n_epochs=100,
            epsilon=1.0,
            epsilon_min=0.01,
            epsilon_decay=0.995,
    ):
        target_model = self._build_model(batch_size)
        q_model, q_train_model = self._build_model(batch_size, include_train_model=True)
        q_model.set_weights(self.main_model.get_weights())
        target_model.set_weights(self.main_model.get_weights())
        env = self.env

        self.memory.clear()
        e = epsilon
        state_memory = [None for _ in range(self.max_episode_size)]
        action_memory = [None for _ in range(self.max_episode_size)]
        reward_memory = [None for _ in range(self.max_episode_size)]
        itotal = 0
        # all_rewards = np.zeros(len(envs))
        for i in range(n_epochs):
            ienv = 0
            self.main_model.reset_states()
            input_state = env.reset()
            is_end = False
            ie = 0
            state_memory[0] = input_state
            while not is_end:
                env.render()
                q0s = self.main_model.predict(input_state.reshape(1, 1, -1), batch_size=1)
                if np.random.rand() <= epsilon:
                    action = np.random.randint(self.action_size)
                else:
                    action = np.argmax(q0s.ravel(), axis=-1)
                new_input_state, reward, is_end, info = env.step(action)
                if ie + 1 >= len(state_memory):
                    print('Out of range!')
                state_memory[ie + 1] = new_input_state
                action_memory[ie] = action
                reward_memory[ie] = reward
                input_state = new_input_state
                ie += 1
                itotal += 1

                if itotal >= self.skip_front:
                    if e > epsilon_min:
                        e *= epsilon_decay

                    if itotal % update_freq == 0:
                        states, actions, rewards, ends = self.memory.replay(batch_size)
                        next_inputs_batch = states[:, 1:]
                        qs = q_model.predict(next_inputs_batch, batch_size=batch_size)
                        next_actions = np.argmax(qs, axis=-1)
                        q2s = target_model.predict(next_inputs_batch, batch_size=batch_size)
                        next_targets = q2s.reshape(-1, self.action_size)[np.arange(batch_size*self.trace_size), next_actions.ravel()].reshape(batch_size, self.trace_size)
                        # next_targets = q2s[..., actions]
                        # rewards = np.vstack([v[3] for v in train_batch])
                        inputs = states[:, :-1]
                        # ends = np.zeros(rewards.shape)
                        # ends[..., -1] = 1.0
                        # Bellman equation:
                        #   Q(t) = R(t) + gamma * Q(t+1)
                        targets = (rewards + (1 - ends) * next_targets * self.gamma).reshape(batch_size, self.trace_size, -1)
                        actions = to_categorical(actions, num_classes=self.action_size).reshape(batch_size, self.trace_size, -1)

                        target_model.set_weights(self.main_model.get_weights())
                        q_model.reset_states()
                        q_train_model.fit(
                            [inputs.reshape(batch_size, self.trace_size, -1), actions],
                            targets,
                            batch_size=batch_size,
                            epochs=1,
                            shuffle=False,
                            verbose=0
                        )
                        self.main_model.set_weights(q_model.get_weights())
                        target_model.reset_states()
                        q_model.reset_states()
            # all_rewards[ienv] = np.mean(reward_memory[:ie])
            # print('Env #' + str(ienv) + ':', all_rewards[ienv] * 10000, 'E:', e)
            ienv += 1
            self.memory.remember_episode(np.array(state_memory[:ie + 1]), np.array(action_memory[:ie]), np.array(reward_memory[:ie]))
            print('Episode #' + str(i) + ':', ie, e, np.max(q0s))

class IntradayPriceEnvironment(Environment):
    def __init__(self, inputs, returns, slippage_rate=0):
        super().__init__()
        self._istep = 0
        self._position = 0
        self._inputs = inputs
        self._returns = returns
        self._slippage_rate = slippage_rate

    def is_end(self):
        """ returns true if is end step
        """
        return self._istep >= self._inputs.shape[0] - 1

    def reset(self):
        self._istep = 0
        self._position = 0
        return np.concatenate([self._inputs[0], [0]])

    def step(self, action):
        """ returns (new_state, reward, is_end)
        """
        new_position = min([max([-1, self._position + action - 1]), 1])
        pnl = new_position * self._returns[self._istep]
        slippage = abs(new_position - self._position) * self._slippage_rate
        reward = pnl - slippage
        self._istep += 1
        self._position = new_position
        new_state = np.concatenate([self._inputs[self._istep], [new_position]])
        return new_state, reward, self.is_end()

if __name__ == "__main__":
    import os
    from tensorflow import set_random_seed
    np.random.seed(3721)
    set_random_seed(3721)

    # from lstm_daily import *
    # dataset = pickle.load(open('/home/kaiwang/Documents/data/experiments/Kaggle_Daily/processed_dataset.pkl', 'rb'))
    # dataset = filter_and_split_dataset(
    #     dataset,
    #     datetime.date(2015, 1, 1),
    #     datetime.date(2016, 1, 1),
    #     start_date=datetime.date(2006, 1, 1),
    #     n_skip_front=30,
    #     min_train_sequence_length=250
    # )
    # input_scalers = [
    #     pp.MinMaxScaler(feature_range=(-1, 1)),
    #     pp.MinMaxScaler(feature_range=(-1, 1)),
    #     pp.MinMaxScaler(feature_range=(-1, 1)),
    #     pp.RobustScaler(),
    #     pp.RobustScaler(),
    #     pp.RobustScaler(),
    #     pp.RobustScaler(),
    #     pp.RobustScaler(),
    #     None
    # ]
    # output_scalers = [pp.RobustScaler()]
    # scale_dataset(dataset, input_scalers, output_scalers)
    # train_envs = []
    # valid_envs = []
    # test_envs = []
    # for tag, data in dataset.items():
    #     train_envs.append(IntradayPriceEnvironment(data['train inputs'], data['train outputs'].ravel()))
    #     valid_envs.append(IntradayPriceEnvironment(data['valid inputs'], data['valid outputs'].ravel()))
    #     test_envs.append(IntradayPriceEnvironment(data['test inputs'], data['test outputs'].ravel()))
    # dataset = None
    # max_episode_size = max([env._inputs.shape[0] for env in train_envs])
    #
    # drqn = DeepRecurrentQNetwork(
    #     9,
    #     3,
    #     [128],
    #     max_episode_size,
    #     10000,
    #     16,
    #     10000,
    #     discount_rate=0.98,
    #     learning_rate=0.001
    # )
    # drqn.train(
    #     train_envs[:25],
    #     update_freq=10,
    #     batch_size=64,
    #     n_epochs=100,
    #     epsilon=1.0,
    #     epsilon_min=0.1,
    #     epsilon_decay=0.999999,
    # )

    env = gym.make('CartPole-v0')
    drqn = DeepRecurrentQNetwork(
        env,
        [64],
        1000,
        1000,
        8,
        50000,
        discount_rate=0.9,
        learning_rate=0.001
    )
    drqn.train(
        update_freq=10,
        batch_size=128,
        n_epochs=10000,
        epsilon=1.0,
        epsilon_min=0.02,
        epsilon_decay=0.9999,
    )
    drqn.run()
    print('All Done!')
# from gridworld import gameEnv
# env = gameEnv(partial=True, size=9)
# drqn = DeepRecurrentQNetwork(
#     9,
#     3,
#     [128],
#     max_episode_size,
#     10000,
#     16,
#     10000,
#     discount_rate=0.98,
#     learning_rate=0.001
# )
# drqn.train(
#     train_envs,
#     update_freq=10,
#     batch_size=64,
#     n_epochs=100,
#     epsilon=1.0,
#     epsilon_min=0.1,
#     epsilon_decay=0.9999,
# )

max_stars_repo_name: kwangphys/rl

max_issues_repo_name: kwangphys/rl

max_forks_repo_name: kwangphys/rl


text:
import random
import time
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
from enum import Enum
import operator
import math

class FunctionType(Enum):
    ROSENBROCK = 1
    GRIEWANK = 2
    RASTRIGIN = 3

def calc_function(x,y, function_type):
    if function_type == FunctionType.ROSENBROCK:
        return ((1 - x) ** 2) + (100 * ((y - x ** 2) ** 2))
    elif function_type == FunctionType.GRIEWANK:
        return (x**2)/4000 + (y**2)/4000 - (math.cos(x)*math.cos(y/math.sqrt(2))) + 1
    elif function_type == FunctionType.RASTRIGIN:
        return 20 + (x**2 - 10*math.cos(2*math.pi*x)) + (y**2 - 10*math.cos(2*math.pi*y))

def calc_function_np(x, y, function_type):
    if function_type == FunctionType.ROSENBROCK:
        return ((1 - x) ** 2) + (100 * ((y - x ** 2) ** 2))
    elif function_type == FunctionType.GRIEWANK:
        return (x**2)/4000 + (y**2)/4000 - (np.cos(x)*np.cos(y/np.sqrt(2))) + 1
    elif function_type == FunctionType.RASTRIGIN:
        return 20 + (x**2 - 10*np.cos(2*np.pi*x)) + (y**2 - 10*np.cos(2*np.pi*y))

def get_max_and_min(function_type):
    if function_type == FunctionType.ROSENBROCK:
        return (-3, 3, -3, 3, 0.5)
    elif function_type == FunctionType.GRIEWANK:
        return (-600, 600, -600, 600, 50)
    elif function_type == FunctionType.RASTRIGIN:
        return (-5.12, 5.12, -5.12, 5.12, 0.5)

class FoodSourceStatus(Enum):
    OCCUPIED = 1
    DEPLETED = 2

class FoodSource:
    def __init__(self, x: float = 0, y: float = 0, function_type:FunctionType = FunctionType.ROSENBROCK):
        self.x = x
        self.y = y
        self.function_type = function_type
        self.value = calc_function(x, y, function_type=function_type)
        self.status = FoodSourceStatus.OCCUPIED

    def __str__(self):
        return str(self.value)

    def __cmp__(self, other):
        return self.value - other.value

    def __eq__(self, other):
        return self.x == other.x and self.y == other.y

class BeeType(Enum):
    SCOUT = 1
    ONLOOKER = 2
    EMPLOYED = 3

    def __str__(self):
        if self.value == 1:
            return 'scout'
        elif self.value == 2:
            return 'onlooker'
        else:
            return 'employed'

class Bee:
    def __init__(self, index: int = 0, x: float = 0.0, y: float = 0.0, type: BeeType = BeeType.SCOUT, decay: int = 3,
                 function_type: FunctionType = FunctionType.ROSENBROCK, search_pase: float = 0.1):
        self.index = index
        self.x = x
        self.y = y
        self.function_type = function_type
        self.search_pase = search_pase
        self.type = type
        self.food_source = FoodSource(self.x, self.y, function_type)
        self.decay = decay

    def get_found_food_source(self):
        return self.food_source

    def find_new_food_source(self):
        self.food_source.status = FoodSourceStatus.DEPLETED
        self.food_source = FoodSource(self.x, self.y, self.function_type)
        return self.food_source

    def __str__(self):
        return str(self.index) + '(' + str(self.type) + ')' + ': ' + str(self.x) + " " + str(self.y) + " -> " + str(
            self.food_source)

    def __eq__(self, other):
        return self.index == other.index

    def search(self, emp):
        u1 = random.randint(-1, 1)
        u2 = random.randint(-1, 1)
        new_x = emp.x + random.uniform(0, self.search_pase) * u1
        new_y = emp.y + random.uniform(0, self.search_pase) * u2
        return FoodSource(new_x, new_y, function_type=self.function_type)

    def search_scout(self, x_min, x_max, y_min, y_max):
        x = random.uniform(x_min, x_max)
        y = random.uniform(y_min, y_max)
        return FoodSource(x, y, function_type=self.function_type)

# ABC
def artificial_bee_colony(nr_albine, nr_iteratii, decay_rate, function_type):
    (x_min, x_max, y_min, y_max, mashgrid_rate) = get_max_and_min(function_type)
    plt.ion()
    food_sources = []
    emp = []
    onl = []
    sco = []
    # Initializam toate albinele ca fiind scout la inceput
    for i in range(nr_albine):
        sco.append(Bee(i, random.uniform(x_min, x_max), random.uniform(y_min, y_max), function_type=function_type, search_pase=mashgrid_rate))
    for i in range(nr_albine):
        food_sources.append(sco[i].food_source)
    # Sortam albinele scout dupa valoare sursei de hrana si jumatate din albine devin employed si jumatate onlooker
    sco.sort(key=operator.attrgetter('food_source.value'))
    best_food_source = sco[0].food_source
    for i in range(nr_albine):
        if i <= int(nr_albine / 2):
            sco[i].type = BeeType.EMPLOYED
            emp.append(sco[i])
        else:
            sco[i].type = BeeType.ONLOOKER
            onl.append(sco[i])
    sco.clear()
    prob = []
    fig = plt.figure()
    ax = axes3d.Axes3D(fig)
    # Incepem iteratiile
    for t in range(nr_iteratii):
        print('Iteratia ' + str(t))
        # Fiecare albina employed cauta un loc mai bun in vecinatatea s-a
        for i in range(len(emp)):
            new_food_source = emp[i].search(emp[i])
            if new_food_source not in food_sources:
                if new_food_source.value > emp[i].food_source.value:
                    old_food_source = emp[i].food_source
                    old_food_source.status = FoodSourceStatus.DEPLETED
                    food_sources.append(new_food_source)
                    emp[i].food_source = new_food_source
                    emp[i].x = new_food_source.x
                    emp[i].y = new_food_source.y
                    emp[i].decay = decay_rate
                else:
                    emp[i].decay = emp[i].decay - 1
        sum = 0
        for i in range(len(emp)):
            sum = sum + emp[i].food_source.value
        prob.clear()
        # Calculam probabilitatile pentru fiecare sursa de hrana gasita de albinele employed de a fi alese de un onlooker
        for i in range(len(emp)):
            prob.append(emp[i].food_source.value / sum)
        # Pentru fiecare onlooker
        for i in range(len(onl)):
            u = random.uniform(0, 1)
            index_emp = 0
            # Gasim un employed pe care sa il cerceteze
            for j in range(len(prob)):
                if j == 0:
                    if u <= prob[j] and u > 0:
                        index_emp = j
                else:
                    if u <= prob[j] and u > prob[j - 1]:
                        index_emp = j
            # Luam o sursa de hrana de pe langa o albina employed
            new_food_source = onl[i].search(emp[index_emp])
            # Daca e noua
            if new_food_source not in food_sources:
                # Mutam albina onlooker pe pozitia noua
                old_food_source = onl[i].food_source
                old_food_source.status = FoodSourceStatus.DEPLETED
                food_sources.append(new_food_source)
                onl[i].food_source = new_food_source
                onl[i].x = new_food_source.x
                onl[i].y = new_food_source.y
                onl[i].decay = decay_rate
        # Pentru fiecare employed
        for i in range(len(emp)):
            # Daca a epuizat hrana
            if emp[i].decay == 0:
                print('Employee ' + str(emp[i].index) + " has depleted it's food source")
                new_food_source = emp[i].search_scout(x_min, x_max, y_min, y_max)
                print(str(new_food_source))
                while new_food_source in food_sources:
                    print('Searching for ' + str(emp[i].index) + " new food source")
                    new_food_source = emp[i].search_scout(x_min, x_max, y_min, y_max)
                    print(str(new_food_source))
                food_sources.append(new_food_source)
                emp[i].x = new_food_source.x
                emp[i].y = new_food_source.y
                emp[i].type = BeeType.SCOUT
                emp[i].decay = decay_rate
                emp[i].food_source = new_food_source
        sco.clear()
        for i in range(len(emp)):
            sco.append(emp[i])
        for j in range(len(onl)):
            sco.append(onl[j])
        sco.sort(key=operator.attrgetter('food_source.value'))
        emp.clear()
        onl.clear()
        for i in range(nr_albine):
            if i <= int(nr_albine / 2):
                sco[i].type = BeeType.EMPLOYED
                emp.append(sco[i])
            else:
                sco[i].type = BeeType.ONLOOKER
                onl.append(sco[i])
        # Plotam rezultatele
        X = np.arange(x_min, x_max, mashgrid_rate)
        Y = np.arange(y_min, y_max, mashgrid_rate)
        X, Y = np.meshgrid(X, Y)
        Z = calc_function_np(X, Y, function_type)
        ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='hot')
        bee_x = [bee.x for bee in filter(lambda b: b.type == BeeType.EMPLOYED, sco)]
        bee_y = [bee.y for bee in filter(lambda b: b.type == BeeType.EMPLOYED, sco)]
        bee_z = [bee.food_source.value for bee in filter(lambda b: b.type == BeeType.EMPLOYED, sco)]
        bee_i = [bee.index for bee in filter(lambda b: b.type == BeeType.EMPLOYED, sco)]
        ax.plot(bee_x, bee_y, bee_z, 'ro')
        for i in range(len(bee_x)):
            plt.annotate(bee_i[i], (bee_x[i], bee_y[i]))
        bee_x = [bee.x for bee in filter(lambda b: b.type == BeeType.ONLOOKER, sco)]
        bee_y = [bee.y for bee in filter(lambda b: b.type == BeeType.ONLOOKER, sco)]
        bee_z = [bee.food_source.value for bee in filter(lambda b: b.type == BeeType.ONLOOKER, sco)]
        bee_i = [bee.index for bee in filter(lambda b: b.type == BeeType.ONLOOKER, sco)]
        ax.plot(bee_x, bee_y, bee_z, 'bo')
        for i in range(len(bee_x)):
            plt.annotate(bee_i[i], (bee_x[i], bee_y[i]))
        plt.xlabel('X')
        plt.ylabel('Y')
        plt.show()
        print('Pas ' + str(t))
        plt.pause(0.01)
        ax.clear()
    print('Bees: ')
    for i in range(len(emp)):
        print(str(emp[i]))
    for j in range(len(onl)):
        print(str(onl[j]))
    print('Food sources')
    food_sources.sort(key=operator.attrgetter('value'))
    for i in range(len(food_sources)):
        print(str(food_sources[i]))
    best_food_source = food_sources[0]
    print('Best food source is ')
    print('(' + str(best_food_source.x) + ' ' + str(best_food_source.y) + ') -> ' + str(best_food_source.value))
    plt.ioff()

artificial_bee_colony(100, 10, 3, function_type=FunctionType.GRIEWANK)


text:
'''Python script to generate Cohort Analysis'''
'''Authors - @Julia Liu
'''

import numpy as np
import pandas as pd
from datetime import datetime
import collections

from .helpers import *

class CohortAnalysis:

    def __init__(self, mrr, cohorts):
        print("INIT COHORT ANALYSIS")
        self.mrr = pd.DataFrame(mrr)
        self.cohorts = pd.DataFrame(cohorts)
        self.missing_months = []

    def run(self):
        self.clean_inputs()
        print(self.mrr)
        print(self.cohorts)

        self.revenue_cohorts()
        self.customer_cohorts()
        self.revenue_retention()
        self.logo_retention()
        self.cumulative()

        self.clean_outputs()
        json = {
            "Revenue Cohorts Calculation (Monthly)": self.rev_cohorts.to_dict(orient='records'),
            "Customer Cohorts Calculation (Monthly)": self.cust_cohorts.to_dict(orient='records'),
            "Revenue Retention (Monthly)": self.rev_retention.to_dict(orient='records'),
            "Logo Retention (Monthly)": self.logo_retention.to_dict(orient='records'),
            "Cumulative Revenue Per Customer (Monthly)": self.cumulative.to_dict(orient='records')
        }
        return json

    def clean_inputs(self):
        self.mrr = self.mrr.copy()
        self.mrr.set_index("Customer", inplace=True)
        self.mrr.apply(filter_to_dec_list)
        self.mrr.drop(self.mrr.tail(2).index, inplace=True)
        self.cohorts = self.cohorts.copy()
        self.cohorts.set_index("Customer", inplace=True)
        self.cohorts.iloc[:, 1:-1] = self.cohorts.iloc[:, 1:-1].apply(filter_to_dec_list)
        self.cohorts = self.cohorts[self.cohorts['Cohort']!="N/A"]

    def clean_outputs(self):
        cohort_months = list(pd.to_datetime(self.rev_cohorts.index).strftime('%m/%Y'))
        years = [m.split("/")[1] for m in cohort_months]
        counter = collections.Counter(years)
        all_months = []
        for year in counter.keys():
            all_months.extend(["{:02d}".format(i+1)+"/"+year for i in range(12)])
        all_months.pop(0)
        self.missing_months = list(set(all_months) - set(cohort_months))
        last_month = str_to_datetime(cohort_months[-1])
        self.missing_months = list(filter(lambda m: str_to_datetime(m) <= last_month, self.missing_months))

        self.clean_outputs_add_empty_rows(self.rev_cohorts)
        self.clean_outputs_add_empty_rows(self.cust_cohorts)
        self.clean_outputs_add_empty_rows(self.rev_retention)
        self.clean_outputs_add_empty_rows(self.logo_retention)
        self.clean_outputs_add_empty_rows(self.cumulative)

        col_labels_dict = {col: "M"+str(col+1) for col in self.rev_cohorts.columns}

        self.retention_statistics(self.rev_retention)
        self.retention_statistics(self.logo_retention)
        self.retention_statistics(self.cumulative)

        indices = [i for i in range(self.cumulative.shape[1]) if "# Customers" != self.cumulative.columns[i]]

        self.rev_cohorts = self.rev_cohorts.astype(object)
        self.rev_cohorts.apply(nan_to_blank_list)
        self.rev_cohorts.apply(dec_to_dollars_list)
        self.rev_cohorts.rename(columns=col_labels_dict, inplace=True)
        self.rev_cohorts.reset_index(inplace=True)

        self.cust_cohorts = self.cust_cohorts.astype(object)
        self.cust_cohorts.apply(nan_to_blank_list)
        self.cust_cohorts.apply(numbers_with_commas_list)
        self.cust_cohorts.rename(columns=col_labels_dict, inplace=True)
        self.cust_cohorts.reset_index(inplace=True)

        self.rev_retention = self.rev_retention.astype(object)
        self.rev_retention.apply(nan_to_blank_list)
        self.rev_retention.iloc[:, indices] = self.rev_retention.iloc[:, indices].apply(dec_to_percents_list)
        self.rev_retention = self.rev_retention.reindex(['# Customers'] + list(self.cumulative.columns[:-1]), axis=1)
        self.rev_retention.rename(columns=col_labels_dict, inplace=True)
        self.rev_retention.reset_index(inplace=True)

        self.logo_retention = self.logo_retention.astype(object)
        self.logo_retention.apply(nan_to_blank_list)
        self.logo_retention.iloc[:, indices] = self.logo_retention.iloc[:, indices].apply(dec_to_percents_list)
        self.logo_retention = self.logo_retention.reindex(['# Customers'] + list(self.cumulative.columns[:-1]), axis=1)
        self.logo_retention.rename(columns=col_labels_dict, inplace=True)
        self.logo_retention.reset_index(inplace=True)

        self.cumulative = self.cumulative.astype(object)
        self.cumulative.apply(nan_to_blank_list)
        self.cumulative.iloc[:, indices] = self.cumulative.iloc[:, indices].apply(dec_to_dollars_list)
        self.cumulative = self.cumulative.reindex(['# Customers'] + list(self.cumulative.columns[:-1]), axis=1)
        self.cumulative.rename(columns=col_labels_dict, inplace=True)
        self.cumulative.reset_index(inplace=True)

        print("REVENUE COHORTS")
        print(self.rev_cohorts)
        print("CUSTOMER COHORTS")
        print(self.cust_cohorts)
        print("REVENUE RETENTION")
        print(self.rev_retention)
        print("LOGO RETENTION")
        print(self.logo_retention)
        print("CUMULATIVE")
        print(self.cumulative)

    def clean_outputs_add_empty_rows(self, data):
        for m in self.missing_months:
            data.loc[str_to_datetime(m)] = ['NaN'] * data.shape[1]
        data.sort_index(inplace=True)
        data.index = pd.to_datetime(data.index).strftime('%m/%Y')

    def revenue_cohorts(self):
        self.mrr_cohorts = self.mrr.copy()
        self.mrr_cohorts = self.mrr_cohorts.iloc[:, 1:]
        self.mrr_cohorts.columns = pd.to_datetime(self.mrr_cohorts.columns).strftime('%m/%Y')
        self.mrr_cohorts['Cohort'] = pd.to_datetime(self.cohorts['Cohort'])
        self.rev_cohorts = self.mrr_cohorts.groupby(by=['Cohort']).sum()
        self.rev_cohorts = self.rev_cohorts.iloc[1:]
        self.rev_cohorts.sort_values('Cohort')
        self.rev_cohorts = self.rev_cohorts.apply(lambda x: pd.Series(x[x != 0].dropna().values), axis=1)
        for i in range(self.rev_cohorts.shape[1], self.mrr.shape[1]-1):
            self.rev_cohorts[i] = pd.Series([0]*self.rev_cohorts.shape[0])

    def customer_cohorts(self):
        self.cust_cohorts = self.mrr_cohorts.groupby(by=['Cohort']).agg(lambda x: x.ne(0).sum())
        self.cust_cohorts = self.cust_cohorts.iloc[1:]
        self.cust_cohorts.sort_values('Cohort')
        self.cust_cohorts = self.cust_cohorts.apply(lambda x: pd.Series(x[x != 0].dropna().values), axis=1)
        for i in range(self.cust_cohorts.shape[1], self.mrr.shape[1]-1):
            self.cust_cohorts[i] = pd.Series([0]*self.cust_cohorts.shape[0])

    def revenue_retention(self):
        self.rev_retention = pd.DataFrame(index=np.arange(self.rev_cohorts.shape[0]))
        self.rev_retention.set_index(self.rev_cohorts.index, inplace=True)
        self.rev_retention = self.rev_cohorts.apply(lambda x: x/x.iloc[0], axis=1)
        for i in range(self.rev_retention.shape[1], self.mrr.shape[1]-1):
            self.rev_retention[i] = pd.Series([0]*self.rev_retention.shape[0])

    def logo_retention(self):
        self.logo_retention = pd.DataFrame(index=np.arange(self.cust_cohorts.shape[0]))
        self.logo_retention.set_index(self.cust_cohorts.index, inplace=True)
        self.logo_retention = self.cust_cohorts.apply(lambda x: x/x.iloc[0], axis=1)
        for i in range(self.logo_retention.shape[1], self.mrr.shape[1]-1):
            self.logo_retention[i] = pd.Series([0]*self.logo_retention.shape[0])

    def cumulative(self):
        self.cumulative = self.rev_cohorts.copy()
        self.cumulative = self.cumulative.apply(pd.DataFrame.cumsum, axis=1)
        self.cumulative['# Customers'] = self.cust_cohorts.iloc[:, 0]
        self.cumulative = self.cumulative.apply(lambda x: x/x.loc['# Customers'], axis=1)

    def retention_statistics(self, data):
        data_copy = data.astype('float64')
        data.loc["Min"] = data_copy[data_copy != "NaN"].min()
        data.loc["25th Percentile"] = data_copy[data_copy != "NaN"].quantile(0.25)
        data.loc["Median"] = data_copy[data_copy != "NaN"].median()
        data.loc["Mean"] = data_copy[data_copy != "NaN"].mean()
        data.loc["75th Percentile"] = data_copy[data_copy != "NaN"].quantile(0.75)
        data.loc["Max"] = data_copy[data_copy != "NaN"].max()
        data['# Customers'] = self.cust_cohorts.iloc[:, 0]

max_stars_repo_name: juliasliu/application

max_issues_repo_name: juliasliu/application

max_forks_repo_name: juliasliu/application