本案例的目标是对用户进行推荐,即以一定的方式将用户与物品之间建立联系。为了更好地帮助用户从海量的数据中快速发现感兴趣的网页,在目前相对单一的推荐系统上进行补充。电子商务服务推荐的分析方法与过程的主要内容包括:
用户访问数据的特征
智能推荐系统的流程图
Python访问数据库的代码
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine('mysql+pymysql://root:[email protected]:3306/7law?charset=utf8')
sql = pd.read_sql('all_gzdata', engine, chunksize=10000)
网页类分析
网页类统计结果
网页类分析实现的代码
counts = [i['fullURLId'].value_counts() for i in sql] # 逐块统计
counts = pd.concat(counts).groupby(level=0).sum() # 合并统计结果,把相同的统计项合并(即按index分组并求和)
counts = counts.reset_index() # 重新设置index,将原来的index作为counts的一列。
counts.columns = ['index', 'num'] # 重新设置列名,主要是第二列,默认为0
counts['type'] = counts['index'].str.extract('(\d{3})') # 提取前三个数字作为类别id
counts['percent'] = counts['num'] / counts['num'].sum() * 100
counts_ = counts[['type', 'num', 'percent']].groupby('type').sum() # 按类别合并
counts_.sort_values('num', ascending=False) # 降序排列
print(counts_)
点击次数分析
#统计点击次数
#value_count统计数据出现的频率
c = [i['realIP'].value_counts() for i in sql]
count3 = pd.concat(c).groupby(level=0).sum()
count3 = pd.DataFrame(count3)
count3[1] = 1
count3 = count3.groupby('realIP').sum()
count3_ =count3.iloc[:7,:].append(count3.iloc[7:,:].sum(),ignore_index=True)
count3_.index = list(range(1,8))+['7次以上']
print(count3_)
网页排名
分析方法与过程
数据抽取
1、建立数据库—导入数据—搭建Python环境—数据分析—建立模型
数据探索性分析
2、网页类型分析
3、网页点击次数分析
4、网页排名分析
1 sql_value_counts.py
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine('mysql+pymysql://root:[email protected]:3306/7law?charset=utf8')
sql = pd.read_sql('all_gzdata', engine, chunksize=10000)
'''
用create_engine建立连接,连接地址的意思依次为“数据库格式(mysql)+程序名(pymysql)+账号密码@地址端口/数据库名(test)”,最后指定编码为utf8;
all_gzdata是表名,engine是连接数据的引擎,chunksize指定每次读取1万条记录。这时候sql是一个容器,未真正读取数据。
'''
counts = [i['fullURLId'].value_counts() for i in sql] # 逐块统计
counts = pd.concat(counts).groupby(level=0).sum() # 合并统计结果,把相同的统计项合并(即按index分组并求和)
counts = counts.reset_index() # 重新设置index,将原来的index作为counts的一列。
counts.columns = ['index', 'num'] # 重新设置列名,主要是第二列,默认为0
counts['type'] = counts['index'].str.extract('(\d{3})') # 提取前三个数字作为类别id
counts['percent'] = counts['num'] / counts['num'].sum() * 100
counts_ = counts[['type', 'num', 'percent']].groupby('type').sum() # 按类别合并
counts_.sort_values('num', ascending=False) # 降序排列
print(counts_)
2 ask_value_counts.py
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine('mysql+pymysql://root:[email protected]:3306/7law?charset=utf8')
sql = pd.read_sql('all_gzdata', engine, chunksize=10000)
# 统计101类别的情况
def count101(i): # 自定义统计函数
j = i[['fullURLId']][i['fullURLId'].str.contains('101')].copy() # 找出类别包含101的网址
return j['fullURLId'].value_counts()
counts2 = [count101(i) for i in sql] # 逐块统计
counts2 = pd.concat(counts2).groupby(level=0).sum() # 合并统计结果
counts2 = pd.DataFrame(counts2)
counts2.columns = ['num']
counts2['percent'] = counts2['num'] / counts2['num'].sum() * 100
counts2.sort_values('num', ascending=False) # 降序排列
print(counts2)
3 know_value_counts.py
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine('mysql+pymysql://root:[email protected]:3306/7law?charset=utf8')
sql = pd.read_sql('all_gzdata', engine, chunksize = 10000)
#统计107类别的情况
def count107(i): #自定义统计函数
j = i[['fullURL']][i['fullURLId'].str.contains('107')].copy() #找出类别包含107的网址
j['type'] = None #添加空列
j['type'][j['fullURL'].str.contains('info/.+?/')] = u'知识首页'
j['type'][j['fullURL'].str.contains('info/.+?/.+?')] = u'知识列表页'
j['type'][j['fullURL'].str.contains('/\d+?_*\d+?\.html')] = u'知识内容页'
return j['type'].value_counts()
counts2 = [count107(i) for i in sql] #逐块统计
counts2 = pd.concat(counts2).groupby(level=0).sum() #合并统计结果
counts2 = pd.DataFrame(counts2)
counts2.columns=['num']
counts2['percent'] = counts2['num']/counts2['num'].sum()*100
print(counts2)
4 other_value_counts.py
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine('mysql+pymysql://root:[email protected]:3306/7law?charset=utf8')
sql = pd.read_sql('all_gzdata', engine, chunksize=10000)
# 统计1999001类别的情况
def count101(i): # 自定义统计函数
j = i[['pageTitle']][i['fullURLId'].str.contains('1999001')].copy() # 找出类别包含101的网址
j['type'] = u'其他'
j['type'][(j['pageTitle'] != '') & (j['pageTitle'].str.contains(u'快车-律师助手'))] = u'快车-律师助手'
j['type'][(j['pageTitle'] != '') & (j['pageTitle'].str.contains(u'免费发布法律咨询'))] = u'免费发布咨询'
j['type'][(j['pageTitle'] != '') & (j['pageTitle'].str.contains(u'咨询发布成功'))] = u'咨询发布成功'
j['type'][(j['pageTitle'] != '') & (j['pageTitle'].str.contains(u'快搜'))] = u'快搜'
return j['type'].value_counts()
counts2 = [count101(i) for i in sql] # 逐块统计
counts2 = pd.concat(counts2).groupby(level=0).sum() # 合并统计结果
counts2 = pd.DataFrame(counts2)
counts2.columns = ['num']
counts2['percent'] = counts2['num'] / counts2['num'].sum() * 100
counts2.sort_values('num', ascending=False) # 降序排列
web_click_counts.py
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine('mysql+pymysql://root:[email protected]:3306/7law?charset=utf8')
sql = pd.read_sql('all_gzdata', engine, chunksize = 10000)
#统计点击次数
#value_count统计数据出现的频率
c = [i['realIP'].value_counts() for i in sql]
count3 = pd.concat(c).groupby(level=0).sum()
count3 = pd.DataFrame(count3)
count3[1] = 1
count3 = count3.groupby('realIP').sum()
count3_ =count3.iloc[:7,:].append(count3.iloc[7:,:].sum(),ignore_index=True)
count3_.index = list(range(1,8))+['7次以上']
print(count3_)
# 对浏览次数达7次以上的情况进行分析,发现大部分用户浏览8~100次,代码实现:
counts3_7 = pd.concat([count3.iloc[7:100,:].sum(),count3.iloc[100:300,:].sum(),count3.iloc[300:,:].sum()])
counts3_7.index = ['8-100','101-300','301以上']
counts3_7df = pd.DataFrame(counts3_7)
counts3_7df.index.name = '点击次数'
counts3_7df.columns = ['用户数']
print(counts3_7df)
web_sort
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine('mysql+pymysql://root:[email protected]:3306/7law?charset=utf8')
sql = pd.read_sql('all_gzdata', engine, chunksize=10000)
counts4 = [i[['realIP','fullURL','fullURLId']] for i in sql]
counts4_ = pd.concat(counts4)
a = counts4_[counts4_['fullURL'].str.contains('\.html')]
print(a.head())
.0.0.1:3306/7law?charset=utf8’)
sql = pd.read_sql('all_gzdata', engine, chunksize=10000)
counts4 = [i[['realIP','fullURL','fullURLId']] for i in sql]
counts4_ = pd.concat(counts4)
a = counts4_[counts4_['fullURL'].str.contains('\.html')]
print(a.head())