def update_memory(replay_memory, max_replay_memory_size, observation,
                  next_observation, state_size, action, reward, done):
    if len(replay_memory) > max_replay_memory_size:
        replay_memory.popleft()
    replay_memory.append(
        (one_hot_encoder(observation, state_size), action,
         one_hot_encoder(next_observation,
                         state_size), reward, 0 if done else 1))
Exemplo n.º 2
0
def load_data(input_dir, max_nb_cha, width, height, channels, len_set, cha_set):
    """
    数据文件夹需严格符合,图片文件(命名为number.jpg)及一个label.txt 这种形式
    # y[0],[1],[2],[3] 分别对应第1,2,3,4个字符类推
    """
    print 'Loading data...'
    tag = time.time()
    x = []
    y_nb = []
    y = [[] for i in range(max_nb_cha)]

    for dirpath, dirnames, filenames in os.walk(input_dir):
        nb_pic = len(filenames)-1
        if nb_pic >= 1:
            cnt = 50000 ## pay attention!!!!
            for i in range(1, nb_pic+1):
                cnt -= 1
                if cnt == 0:
                    break
                filename = str(i) + '.jpg'
                filepath = dirpath + os.sep + filename
                im = Image.open(filepath)
                im = im.resize((width, height))
                pixels = list(im.getdata())
                if channels > 1:
                    x.append([[[pixels[k*width+i][j] for k in range(height)] for i in range(width)] for j in range(channels)]) # 转成(channel,width,height)shape
                else:
                    x.append([[[pixels[k*width+i] for k in range(height)] for i in range(width)]])
            
            label_path = dirpath + os.sep + 'label.txt'
            with open(label_path) as f:
                cnt = 50000
                for raw in f:
                    # print raw
                    cnt -= 1
                    if cnt == 0:
                        break
                    raw = raw.strip('\n\r')
                    if len(raw) > 0:
                        y_nb.append(len(raw))
                        for i in range(max_nb_cha):
                            if i < len(raw):
                                y[i].append(raw[i])
                            else:
                                y[i].append('empty')
                    

    # 转成keras能接受的数据形式,以及做one hot 编码
    x = np.array(x)
    x = x.astype('float32') # gpu只接受32位浮点运算
    x /= 255 # normalized
    y_nb = np.array(one_hot_encoder(y_nb, len_set))
    for i in range(max_nb_cha):
        y[i] = np.array(one_hot_encoder(y[i], cha_set))

    print 'Data loaded, spend time(m) :', (time.time()-tag)/60
    return [x, y_nb, y]
Exemplo n.º 3
0
def load_data(input_dir, max_nb_cha, width, height, channels, len_set,
              cha_set):
    """
    数据文件夹需严格符合,图片文件(命名为number.jpg)及一个label.txt 这种形式
    # y[0],[1],[2],[3] 分别对应第1,2,3,4个字符类推
    """
    print 'Loading data...'
    tag = time.time()
    x = []
    y_nb = []
    y = [[] for i in range(max_nb_cha)]

    for dirpath, dirnames, filenames in os.walk(input_dir):
        nb_pic = len(filenames) - 1
        if nb_pic >= 1:
            for i in range(1, nb_pic + 1):
                filename = str(i) + '.jpg'
                filepath = dirpath + os.sep + filename
                im = Image.open(filepath)
                im = im.resize((width, height))
                pixels = list(im.getdata())
                if channels > 1:
                    x.append([
                        [[pixels[k * width + i][j] for k in range(height)]
                         for i in range(width)] for j in range(channels)
                    ])  # 转成(channel,width,height)shape
                else:
                    x.append([[[pixels[k * width + i] for k in range(height)]
                               for i in range(width)]])

            label_path = dirpath + os.sep + 'label.txt'
            with open(label_path) as f:
                for raw in f:
                    # print raw
                    raw = raw.strip('\n\r')
                    if len(raw) > 0:
                        y_nb.append(len(raw))
                        for i in range(max_nb_cha):
                            if i < len(raw):
                                y[i].append(raw[i])
                            else:
                                y[i].append('empty')

    # 转成keras能接受的数据形式,以及做one hot 编码
    x = np.array(x)
    x = x.astype('float32')  # gpu只接受32位浮点运算
    x /= 255  # normalized
    y_nb = np.array(one_hot_encoder(y_nb, len_set))
    for i in range(max_nb_cha):
        y[i] = np.array(one_hot_encoder(y[i], cha_set))

    print 'Data loaded, spend time(m) :', (time.time() - tag) / 60
    return [x, y_nb, y]
Exemplo n.º 4
0
def generate_data(input_dir, max_nb_cha, width, height, channels, len_set,
                  cha_set, batch_size):
    cnt = 0
    x = []
    y_nb = []
    y = [[] for i in range(max_nb_cha)]
    while True:
        for dirpath, dirnames, filenames in os.walk(input_dir):
            nb_pic = len(filenames) - 1
            if nb_pic >= 1:
                label_path = dirpath + os.sep + 'label.txt'
                with open(label_path) as f:
                    for (i, raw) in zip(range(1, nb_pic + 1), f):
                        filename = str(i) + '.jpg'
                        filepath = dirpath + os.sep + filename
                        im = Image.open(filepath)
                        im = im.resize((width, height))
                        pixels = list(im.getdata())
                        x.append([
                            [[pixels[k * width + i][j] for k in range(height)]
                             for i in range(width)] for j in range(channels)
                        ])  # 转成(channel,width,height)shape

                        raw = raw.strip('\n\r')
                        y_nb.append(len(raw))
                        for i in range(max_nb_cha):
                            if i < len(raw):
                                y[i].append(raw[i])
                            else:
                                y[i].append('empty')

                        cnt += 1
                        if cnt == batch_size:
                            x = np.array(x)
                            x = x.astype('float32')  # gpu只接受32位浮点运算
                            x /= 255  # normalized
                            y_nb = np.array(one_hot_encoder(y_nb, len_set))
                            for i in range(max_nb_cha):
                                y[i] = np.array(one_hot_encoder(y[i], cha_set))

                            ret = {
                                'output%d' % i: y[i - 1]
                                for i in range(1, max_nb_cha + 1)
                            }
                            ret['input'] = x
                            ret['output_nb'] = y_nb
                            yield ret
                            x = []
                            y_nb = []
                            y = [[] for i in range(max_nb_cha)]
                            cnt = 0
def run():
    # game info
    game_name = 'Taxi-v2'
    env = gym.make(game_name)
    state_size = env.observation_space.n
    action_size = env.action_space.n
    print 'State size', state_size
    print 'Action size', action_size
    # policy_net = policy_network(state_size, action_size)
    policy_net = load_model('models/policy_11000_8.00')

    episode_reward_tracker = []
    episode_len_tracker = []
    nb_episodes = 200000
    max_episode_steps = 200
    model_save_freq = 500
    update_freq = 10
    training = True
    queue = []
    gema = 0.99

    # real_q = json.loads(open('Taxi_q').readline())
    # print real_q, len(real_q)
    for t in xrange(nb_episodes):
        observation = env.reset()
        whole_reward = 0
        states = []
        actions = []
        rewards = []
        for step in xrange(max_episode_steps):
            state = one_hot_encoder(observation, state_size)
            action = select_action(policy_net, state)
            next_observation, reward, done, _ = env.step(action)
            whole_reward += reward
            if training:
                states.append(state)
                actions.append(action)
                rewards.append(reward)

            if done:
                # Save model
                episode_reward_tracker.append(whole_reward)
                episode_len_tracker.append(step + 1)
                ave_reward = float(sum(episode_reward_tracker[-100:])) / 100
                ave_len = float(sum(episode_len_tracker[-100:])) / 100
                print t, step, ave_reward, ave_len, whole_reward
                if training:
                    rewards = decay_rewards(rewards, gema)
                    queue.append((states, actions, rewards))
                if training and t % update_freq == 0:
                    train(policy_net, queue, action_size)
                    queue = []
                if training and (t % model_save_freq == 0):
                    plot(episode_reward_tracker, episode_len_tracker)
                    policy_net.save('models/policy_%d_%.2lf' % (t, ave_reward))
                break

            else:
                observation = next_observation
def e_greedy(epsilon, env, observation, state_size, action_function):
    if np.random.random() < epsilon:
        action = env.action_space.sample()
    else:
        state = np.asarray([one_hot_encoder(observation, state_size)])
        q_values = action_function.predict(state)[0]
        # print q_values
        action = np.argmax(q_values)
    return action
Exemplo n.º 7
0
def generate_data(input_dir, max_nb_cha, width, height, channels, len_set, cha_set, batch_size):
    cnt = 0
    x = []
    y_nb = []
    y = [[] for i in range(max_nb_cha)]
    while True:
        for dirpath, dirnames, filenames in os.walk(input_dir):
            nb_pic = len(filenames)-1
            if nb_pic >= 1:            
                label_path = dirpath + os.sep + 'label.txt'
                with open(label_path) as f:
                    for (i, raw) in zip(range(1, nb_pic+1), f):
                        filename = str(i) + '.jpg'
                        filepath = dirpath + os.sep + filename
                        im = Image.open(filepath)
                        im = im.resize((width, height))
                        pixels = list(im.getdata())
                        x.append([[[pixels[k*width+i][j] for k in range(height)] for i in range(width)] for j in range(channels)]) # 转成(channel,width,height)shape
                        
                        raw = raw.strip('\n\r')
                        y_nb.append(len(raw))
                        for i in range(max_nb_cha):
                            if i < len(raw):
                                y[i].append(raw[i])
                            else:
                                y[i].append('empty')

                        cnt += 1
                        if cnt == batch_size:
                            x = np.array(x)
                            x = x.astype('float32') # gpu只接受32位浮点运算
                            x /= 255 # normalized
                            y_nb = np.array(one_hot_encoder(y_nb, len_set))
                            for i in range(max_nb_cha):
                                y[i] = np.array(one_hot_encoder(y[i], cha_set))

                            ret = {'output%d'%i:y[i-1] for i in range(1, max_nb_cha+1)}
                            ret['input'] = x
                            ret['output_nb'] = y_nb
                            yield ret
                            x = []
                            y_nb = []
                            y = [[] for i in range(max_nb_cha)]
                            cnt = 0
def train():
    # game info
    game_name = 'Taxi-v2'
    env = gym.make(game_name)
    state_size = env.observation_space.n
    action_size = env.action_space.n
    print 'State size', state_size
    print 'Action size', action_size
    actor = policy_network(state_size, action_size)
    # actor = load_model('models/policy_11000_8.00')
    critic = dueling_network_ave(state_size, action_size)

    replay_memory = deque()
    max_replay_memory_size = 200000
    min_replay_memory_size = 100000
    episode_reward_tracker = []
    episode_len_tracker = []
    nb_episodes = 200000
    max_episode_steps = 200
    model_save_freq = 500
    update_freq = 10
    gema = 0.99

    for t in xrange(nb_episodes):
        observation = env.reset()
        whole_reward = 0
        for step in xrange(max_episode_steps):
            state = one_hot_encoder(observation, state_size)
            action = select_action(actor, state)
            next_observation, reward, done, _ = env.step(action)
            update_memory(replay_memory, max_replay_memory_size, observation,
                          next_observation, state_size, action, reward, done)
            whole_reward += reward

            if len(replay_memory
                   ) > min_replay_memory_size and step % update_freq == 0:
                replay(replay_memory, actor, critic)
            if done:
                # Save model
                episode_reward_tracker.append(whole_reward)
                episode_len_tracker.append(step + 1)
                ave_reward = float(sum(episode_reward_tracker[-100:])) / 100
                ave_len = float(sum(episode_len_tracker[-100:])) / 100
                print t, step, ave_reward, ave_len, whole_reward
                if len(replay_memory
                       ) > min_replay_memory_size and t % model_save_freq == 0:
                    plot(episode_reward_tracker, episode_len_tracker)
                    actor.save('models/actor_%d_%.2lf' % (t, ave_reward))
                    critic.save('models/critic_%d_%.2lf' % (t, ave_reward))
                break

            else:
                observation = next_observation
Exemplo n.º 9
0
def claim_read(epison=1):
  cl_agg = pd.read_pickle('claim_cached')
  return cl_agg

  claim = pd.read_csv('policy_claim/claim_0702.csv')
  claim['Driver\'s_Gender'], _ = pd.factorize(claim['Driver\'s_Gender'])
  claim['Driver_days_from_birth'] = claim['DOB_of_Driver'].apply(lambda x: \
    datetime.strptime('07/2018', "%m/%Y") - datetime.strptime(x, "%m/%Y")).dt.days
  claim['Driver_days_from_accident'] = claim['Accident_Date'].apply(lambda x: \
    datetime.strptime('07/2018', "%m/%Y") - datetime.strptime(x, "%Y/%m")).dt.days

  claim['Driver\'s_Relationship_with_Insured'] = "rls_" + claim['Driver\'s_Relationship_with_Insured'].astype(str)
  claim['Marital_Status_of_Driver'] = "stat_" + claim['Marital_Status_of_Driver'].astype(str)

  claim['Cause_of_Loss'], _ = pd.factorize(claim['Cause_of_Loss'])
  claim['Cause_of_Loss'] = "cause_" + claim['Cause_of_Loss'].astype(str)

  claim['Accident_area'], _ = pd.factorize(claim['Accident_area'])
  claim['Accident_area'] = "area_" + claim['Accident_area'].astype(str)

  claim.drop(columns=['DOB_of_Driver', 'Accident_Date', 
                      'Vehicle_identifier', 'Accident_area',
                      'Accident_Time'], inplace=True)
  
  claim, claim_cat = util.one_hot_encoder(claim, nan_as_category=False,
                          exclude_list=['Claim_Number','Policy_Number'])

  num_aggregations = {
    'Nature_of_the_claim': ['mean', 'max'],
    'Driver\'s_Gender': ['mean'],
    'Paid_Loss_Amount': ['mean', 'max', 'sum'],
    'paid_Expenses_Amount': ['max', 'mean', 'sum'],
    'Salvage_or_Subrogation?': ['max', 'mean', 'sum'],
    'At_Fault?': ['max', 'mean', 'sum'],
    'Deductible': ['max', 'mean', 'sum'],
    'number_of_claimants': ['max', 'mean', 'sum'],
    'Driver_days_from_birth': ['mean'],
    'Driver_days_from_accident': ['mean']
  }
  
  cat_aggregations = {}
  for cat in claim_cat: cat_aggregations[cat] = ['mean']
  
  claim_agg = claim.groupby('Policy_Number').agg({**num_aggregations, **cat_aggregations})
  claim_agg.columns = pd.Index(['CLAIM_' + e[0] + "_" + e[1].upper() for e in claim_agg.columns.tolist()])
  claim_agg['CLAIM_COUNT'] = claim.groupby('Policy_Number') \
                                    .size()
  claim_agg['CLAIM_NUNIQUE_COUNT'] = claim.groupby('Policy_Number')['Claim_Number'] \
                                    .nunique()
                                    
  del claim
  gc.collect()
Exemplo n.º 10
0
def policy_read(num_rows = None, nan_as_category = True, epison=1):
  po_agg = pd.read_pickle('policy_cached')
  return po_agg
  policy = pd.read_csv('policy_claim/policy_0702.csv')
  
  policy['Flag_first_year_policy'] = policy['lia_class'].apply(lambda x: 1 if x==4 else 0)
  policy['New_Insured_Amount_max'] = policy[['Insured_Amount1', 'Insured_Amount2', 'Insured_Amount3']].max(axis=1)
  policy['Manafactured_Year_and_Month_diff'] = 2018 - policy['Manafactured_Year_and_Month']  
  policy['Cancellation'], _ = pd.factorize(policy['Cancellation'])
  policy['Imported_or_Domestic_Car'] = "index_" + policy['Imported_or_Domestic_Car'].astype(str)
  policy['Policy_days_from_ibirth'] = policy['ibirth'].apply(lambda x: \
    x if pd.isnull(x) else datetime.strptime('07/2018', "%m/%Y") - datetime.strptime(x, "%m/%Y")).dt.days

  res = policy['dbirth'].str.split('/', 1, expand=True)
  policy.loc[2000 < res[1].astype(float),'dbirth'] = np.nan
  policy['Policy_days_from_dbirth'] = policy['dbirth'].apply(lambda x: \
    x if pd.isnull(x) else (datetime.strptime('07/2018', "%m/%Y") - datetime.strptime(x, "%m/%Y")).days)

  policy['fsex'], _ = pd.factorize(policy['fsex'])
  policy['fsex'].replace(-1, np.nan, inplace=True)
  
  policy['fmarriage'], _ = pd.factorize(policy['fsex'])
  policy['fmarriage'].replace(-1, np.nan, inplace=True)
  
  policy['fassured'], _ = pd.factorize(policy['fassured'])
  policy['fassured'] = "cat_" + policy['fassured'].astype(str)
  
  policy['iply_area'], _ = pd.factorize(policy['iply_area'])
  policy['iply_area'] = "area_" + policy['iply_area'].astype(str)
  
  def deductible(df):
    if df['Coverage_Deductible_if_applied'] <0:
      return 0
    if df['Coverage_Deductible_if_applied'] == 1:
      return 5000
    if df['Coverage_Deductible_if_applied'] == 2:
      return 6500
    if df['Coverage_Deductible_if_applied'] == 3:
      return 8000
    if df['Coverage_Deductible_if_applied'] == 10:
      return df['Insured_Amount3'] * 0.1
    if df['Coverage_Deductible_if_applied'] == 20:
      return df['Insured_Amount3'] * 0.2
    if df['Insurance_Coverage'] in ['09I', '10A', '14E', '15F', '15O', '20B', '20K', '29K', '32N', '33F', '33O', '56K', '65K']:
      return 0
    return df['Coverage_Deductible_if_applied']
  
  policy['Deductible_calc'] = policy.apply(deductible, axis=1)

  policy.drop(columns=['Insured\'s_ID', 'Prior_Policy_Number', 'Vehicle_identifier',
                    'Vehicle_Make_and_Model1', 'Vehicle_Make_and_Model2',
                    'Coding_of_Vehicle_Branding_&_Type', 'fpt',
                    'Distribution_Channel',
                    'ibirth', 'dbirth', 'aassured_zip',
                    'fequipment1', 'fequipment2', 'fequipment3',
                    'fequipment4', 'fequipment5', 'fequipment6',
                    'fequipment9', 'nequipment9'], inplace=True)

  policy, polict_cat = util.one_hot_encoder(policy, nan_as_category=False,
                          exclude_list=['Policy_Number'])

  num_aggregations = {
    'Manafactured_Year_and_Month': ['max'], # all same
    'Manafactured_Year_and_Month_diff': ['max'], # all same
    'Engine_Displacement_(Cubic_Centimeter)': ['max'],# all same
    'Insured_Amount1': ['max', 'mean', 'sum'],
    'Insured_Amount2': ['max', 'mean', 'sum'],
    'Insured_Amount3': ['max', 'mean', 'sum'],
    'New_Insured_Amount_max':['max', 'mean', 'sum'],
    'Deductible_calc': ['max', 'mean', 'sum'],
    'qpt':['max', 'mean'],
    'Multiple_Products_with_TmNewa_(Yes_or_No?)': ['max', 'mean', 'sum'],
    'lia_class': ['max', 'mean'],
    'plia_acc': ['max', 'mean'],
    'pdmg_acc': ['max', 'mean'],
    'Coverage_Deductible_if_applied': ['max', 'mean', 'sum'],
    'Premium': ['max', 'mean', 'sum'],
    'Replacement_cost_of_insured_vehicle': ['max', 'mean', 'sum'],
    'Policy_days_from_ibirth': ['mean'],
    'Policy_days_from_dbirth': ['mean']
  }
  
  cat_aggregations = {}
  for cat in polict_cat: cat_aggregations[cat] = ['mean']
  
  policy_agg = policy.groupby('Policy_Number').agg({**num_aggregations, **cat_aggregations})
  policy_agg.columns = pd.Index(['POLICY_' + e[0] + "_" + e[1].upper() for e in policy_agg.columns.tolist()])
  policy_agg['POLICY_COUNT'] = policy.groupby('Policy_Number') \
                                    .size()       
                                    
  policy_curr = policy[(policy['Coverage_Deductible_if_applied']>=0)&
      (policy['Insured_Amount1']>0) |
      (policy['Insured_Amount2']>0) |
      (policy['Insured_Amount3']>0)]
  
  policy_curr['Insured_to_Premium_ratio'] = policy_curr['Premium'] / policy_curr['New_Insured_Amount_max']

  num_aggregations = {
    'Insured_Amount1': ['max', 'mean', 'sum'],
    'Insured_Amount2': ['max', 'mean', 'sum'],
    'Insured_Amount3': ['max', 'mean', 'sum'],
    'New_Insured_Amount_max':['max', 'mean', 'sum'],
    'Insured_to_Premium_ratio':['max', 'mean'],
    'Deductible_calc': ['max', 'mean', 'sum'],
    'Premium': ['max', 'mean', 'sum']
  }
  policy_curr_agg = policy_curr.groupby('Policy_Number').agg(num_aggregations)
  policy_curr_agg.columns = pd.Index(['POLICY_CURR_' + e[0] + "_" + e[1].upper() for e in policy_curr_agg.columns.tolist()])
 
  policy_agg = policy_agg.join(policy_curr_agg, how='left', on='Policy_Number')
  
  for col in policy_curr_agg.columns.tolist():
    policy_agg[col] = policy_agg[col].fillna(0)

  del policy, policy_curr_agg
  gc.collect()
Exemplo n.º 11
0
bureau_missed_payment['FLAG_MISSED'] = bureau_missed_payment[
    'FLAG_MISSED'].fillna(0)


bureau_missed_payment = bureau_missed_payment.groupby('SK_ID_CURR', as_index=False)['FLAG_MISSED'] \
  .max()

data_train = data_train.merge(bureau_missed_payment,
                              on='SK_ID_CURR',
                              how='left')
data_test = data_test.merge(bureau_missed_payment, on='SK_ID_CURR', how='left')

####################

prev = pd.read_csv('previous_application.csv')
prev, cat_cols = one_hot_encoder(prev, nan_as_category=True)
# Days 365.243 values -> nan
prev['DAYS_FIRST_DRAWING'].replace(365243, np.nan, inplace=True)
prev['DAYS_FIRST_DUE'].replace(365243, np.nan, inplace=True)
prev['DAYS_LAST_DUE_1ST_VERSION'].replace(365243, np.nan, inplace=True)
prev['DAYS_LAST_DUE'].replace(365243, np.nan, inplace=True)
prev['DAYS_TERMINATION'].replace(365243, np.nan, inplace=True)
# Add feature: value ask / value received percentage
prev['APP_CREDIT_PERC'] = prev['AMT_APPLICATION'] / prev['AMT_CREDIT']
# Previous applications numeric features
num_aggregations = {
    'AMT_ANNUITY': ['min', 'max', 'mean'],
    'AMT_APPLICATION': ['min', 'max', 'mean'],
    'AMT_CREDIT': ['min', 'max', 'mean'],
    'APP_CREDIT_PERC': ['min', 'max', 'mean', 'var'],
    'AMT_DOWN_PAYMENT': ['min', 'max', 'mean'],