一步一步认识用户画像

一步一步认识用户画像

标签: 用户画像 GBDT 建模 Python


作为一名资深吃瓜群众,身处大数据时代,过去一直不知道用户画像的存在。用户画像到底是何来头,下面会一一告诉你~
在这里插入图片描述

用户画像

1.什么是用户画像?

交互设计之父AlanCooper最早提出了用户画像(persona)的概念,认为“用户画像是真实用户的虚拟代表,是建立在一系列真实数据之上的目标用户模型”。通过对用户多方面的信息的了解,将多种信息集合在一起并形成在一定类型上的独特的特征与气质,这就形成了用户的独特的“画像”。看下图会更加直接☟☟☟☟
用户画像简介

早期的用户画像相对简单,类似于个人档案信息,区分度和可用性都不强。但是随着大数据的发展,数据量的指数型增长和大数据分析技术的逐渐成熟使得用户可捕捉的行为数据越来越多,用户画像才慢慢地发挥其应有的效果。

2.为什么要用用户画像?

目前用户画像主要应用于商业领域,其核心工作是为用户打标签,打标签的目的之一是为了让人能够理解并且最重要的是方便计算机处理。标签提供了一种便捷的方式,使得计算机能够程序化处理与人相关的信息,甚至通过算法、模型能够“理解”人。当计算机具备这样的能力后,无论是搜索引擎推荐引擎广告投放等各种应用领域,都将能进一步提升精准度,提高信息获取的效率。通过对人群基本属性行为习惯商业价值等多种维度信息数据综合分析,精准的进行目标受众的画像和定位,能够实现基于大数据的精准营销,这也就是用户画像的用处所在了。

3.如何实现用户画像?

一个标签通常是人为规定高度精炼的特征标识,如年龄段标签:25~35岁,地域标签:北京,标签呈现出两个重要特征(看下图):
在这里插入图片描述
人为制定标签规则,并能够通过标签快速读出其中的信息,机器方便做标签提取聚合分析。所以,用户画像,也称用户标签,为我们提供了一种简洁的方式去描述用户信息。
懂得了如何处理用户信息之后,接下来如何实现用户画像呢?首先我们需明确我们的画像方向,例如:影迷偏好、消费者能力等等,人工设计画像的方向,可方便后面的数据采集以及模型构建。详细请看下图:
在这里插入图片描述
主要的思路就是:
用户微观画像的建立→用户画像的标签建模→用户画像的数据架构

4.标签与用户画像的联系

提了这么久的标签,那么标签和用户画像到底是什么关系呢?简单来说,它们是整体和局部的关系,用户画像是整体,标签是局部,这种关系通过“标签体系”体现。整体和局部总包含两方面的关系:
(1)化整为零:整体如何反映在局部;
(2)化零为整:局部如何组成整体。
举例来说:“人有一双眼睛一个鼻子”,那么:
(1)化整为零:对每个人都应该观察到一双眼睛和一个鼻子;
(2)化零为整:只有位置合适的一双眼睛和一个鼻子我们才认为他是一个人。
标签建模是用户画像中核心的一块:
在这里插入图片描述

5.用户画像如何进行验证?

用户画像的验证主要是以下两个方面:
(1)标签打得准不准
(2)标签打得全不全
但目前的重点还是放在准确性的验证上~
在这里插入图片描述

6.用户画像中对基本属性的预测方式

懂得了理论之后,当然不仅仅纸上谈兵,用到实处才是硬道理~用户画像目前采用机器学习模型+少量的人工规则来实现的~
那么接下来我将迈出用户画像建模的一小步,使用GBDT算法(前一篇博客有所提及)来实现对性别这个基本属性的预测。
主要分为以下几个步骤:
(1)训练和测试语料准备
(2)特征选择与特征权重计算
(3)线下模型训练与测试
(4)线下效果评估
(5)线上应用
(6)线上效果评估
具体的操作如下:
安装xgboost-Python(xgboost是GBDT算法的实现,适合用于大量数据处理中,在前一篇博客也有所提及)
(1)安装基本开发工具
在终端中输入:

$ sudo apt install git build-essential python-dev python-setuptools python-pip python-numpy python-scipy

(2)从Github下载最新源代码:

$ git clone --recursive https://github.com/dmlc/xgboost

(3)编译:

$ cd xgboost
$ make -j4

(4)测试:
此处输入图片的描述
(5)实现代码(简单的预测):

import xgboost as xgb
# read in data
dtrain = xgb.DMatrix('demo/data/agaricus.txt.train')
dtest = xgb.DMatrix('demo/data/agaricus.txt.test')
# specify parameters via map
param = {'max_depth':2, 'eta':1, 'silent':1, 'objective':'binary:logistic' }
num_round = 2
bst = xgb.train(param, dtrain, num_round)
# make prediction
preds = bst.predict(dtest)

(6)深层次的预测:包括进行特征提取、添加缺失值

import datetime

import pandas as pd

import numpy as np

from sklearn.cross_validation import train_test_split

import xgboost as xgb

import random

from operator import itemgetter

import zipfile

from sklearn.metrics import roc_auc_score

import time

random.seed(3)​

training_filename = 'C:\\Users\\ganda\\Desktop\\train.csv'

testing_filename = 'C:\\Users\\ganda\\Desktop\\test.csv'

submission_dir = 'C:\\Users\\ganda\\Desktop\\'

def load_train():

    print("Loading training data csv: " + training_filename)

    training_data = pd.read_csv(training_filename)

    return training_data

def load_test():

    #print("Loading training data csv: " + training_filename)

    test = pd.read_csv(testing_filename)

    # TODO

    return test

def create_feature_map(features):

    outfile = open('xgb.fmap', 'w')

    for i, feat in enumerate(features):

        outfile.write('{0}\t{1}\tq\n'.format(i, feat))

    outfile.close()

def get_importance(gbm, features):

    create_feature_map(features)

    importance = gbm.get_fscore(fmap='xgb.fmap')

    importance = sorted(importance.items(), key=itemgetter(1), reverse=True)

    return importance

def print_features_importance(imp):

    for i in range(len(imp)):

        print("# " + str(imp[i][1]))

        print('output.remove(\'' + imp[i][0] + '\')')

def run_XGBoost(train, test, features,target,random_state=0):

    eta = 0.1

    max_depth = 5

    subsample = 0.8

    colsample_bytree = 0.8

    start_time = time.time()

    print('XGBoost params. ETA: {}, MAX_DEPTH: {}, SUBSAMPLE: {}, COLSAMPLE_BY_TREE: {}'.format(eta, max_depth, subsample, colsample_bytree))

    params = {

        "objective": "binary:logistic",

        "booster" : "gbtree",

        "eval_metric": "auc",

        "eta": eta,

        "max_depth": max_depth,

        "subsample": subsample,

        "colsample_bytree": colsample_bytree,

        "silent": 1,

        "seed": random_state

    }

    num_boost_round = 260

    early_stopping_rounds = 20

    test_size = 0.1

    X_train, X_valid = train_test_split(train, test_size=test_size, random_state=random_state)

    y_train = X_train[target]

    y_valid = X_valid[target]

    #添加缺失数据

    dtrain = xgb.DMatrix(X_train[features],y_train,missing = -9999)

    dvalid = xgb.DMatrix(X_valid[features],y_valid,missing = -9999)

    watchlist = [(dtrain, 'train'), (dvalid, 'eval')]

    gbm = xgb.train(params, dtrain, num_boost_round, evals=watchlist, early_stopping_rounds=early_stopping_rounds, verbose_eval=True)

    print("Validating...")

    check = gbm.predict(xgb.DMatrix(X_valid[features]), ntree_limit=gbm.best_ntree_limit)

    score = roc_auc_score(y_valid.values,check)

    print('Check error value: {:.6f}'.format(score))

    imp = get_importance(gbm, features)

    print('Importance array: ', imp)

    print("Predict test set...")

    test_prediction = gbm.predict(xgb.DMatrix(test[features]), ntree_limit=gbm.best_ntree_limit)

    print('Training time: {} minutes'.format(round((time.time() - start_time)/60, 2)))

    return test_prediction.tolist(), score

def create_submission(score, test, prediction):

    # Make Submission

    now = datetime.datetime.now()

    sub_file = submission_dir + 'submission_' + str(score) + '_' + str(now.strftime("%Y-%m-%d-%H-%M")) + '.csv'

    print('Writing submission: ', sub_file)

    f = open(sub_file, 'w')

    f.write('id,probability\n')

    total = 0

    for id in test['id']:

        str1 = str(id) + ',' + str(prediction[total])

        str1 += '\n'

        total += 1

        f.write(str1)

    f.close()

train = load_train()

test = load_test()

features = [] 

# TODO!

#添加特征​

features.extend([''])

print('Length of train: ', len(train))

print('Length of test: ', len(test))

print('Features [{}]: {}'.format(len(features), sorted(features)))

#label是目标字段​

test_prediction, score = run_XGBoost(train, test, features,'label')

print('Score = {}'.format(score))

create_submission(score, test, test_prediction)

参考资料:
1.一步步教你看懂大数据时代下的“用户画像”
2.大数据用户画像方法与实践
3.Xgboost分类
4.通过观影兴趣预测用户画像之性别属性

  • 6
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值