From 91404ed645b0f6d82ea9385bfa01f4d8b3dc8f12 Mon Sep 17 00:00:00 2001 From: woody <165194124@qq.com> Date: Wed, 10 Feb 2016 09:31:11 +0800 Subject: [PATCH] 3.5-wudi MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 交作业 --- .../wudi/3.5homework-1.py" | 64 +++++++++++++++++++ .../wudi/3.5homework-avg.py" | 47 ++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 "week3\345\244\247\344\275\234\344\270\232\346\217\220\344\272\244/wudi/3.5homework-1.py" create mode 100644 "week3\345\244\247\344\275\234\344\270\232\346\217\220\344\272\244/wudi/3.5homework-avg.py" diff --git "a/week3\345\244\247\344\275\234\344\270\232\346\217\220\344\272\244/wudi/3.5homework-1.py" "b/week3\345\244\247\344\275\234\344\270\232\346\217\220\344\272\244/wudi/3.5homework-1.py" new file mode 100644 index 0000000..b7ec1b3 --- /dev/null +++ "b/week3\345\244\247\344\275\234\344\270\232\346\217\220\344\272\244/wudi/3.5homework-1.py" @@ -0,0 +1,64 @@ + +# coding: utf-8 + +# In[1]: + +import pymongo,time,charts + + +# In[2]: + +client = pymongo.MongoClient('localhost',27017) +ganji = client['ganji'] +the_sample2 = ganji['the_sample2'] + + +# In[49]: + +def get_area(): + areas = [] + for item in the_sample2.find(): + areas.append(item['area'][0]) + return list(set(areas)) + + +def get_data(date1,date2,area): + pipeline = [ + {'$match':{'$and':[{'area':{'$all':area}},{'pub_date':{'$gte':date1,'$lte':date2}}]}}, + {'$group':{'_id':{'$slice':['$cates',2,1]},'counts':{'$sum':1}}}, + {'$sort':{'counts':-1}}, + {'$limit':3} + ] + + + for x in the_sample2.aggregate(pipeline): + data ={ + 'name':x['_id'][0], + 'data':[x['counts']], + 'type':'column'#圆柱图 + + } + + yield data + + + +# In[50]: + + +series = [x for x in get_data('2015.12.15','2016.1.15',['西单'])] +options = { + 'chart' : {'zoomType':'xy'}, + 'title' : {'text':'发帖量统计'}, + 'subtitle':{'text':'可视化统计图表'}, + 'yAxis' : {'title':{'text':'数量'}}, + +} + +charts.plot(series,options=options,show='inline') + + +# In[ ]: + + + diff --git "a/week3\345\244\247\344\275\234\344\270\232\346\217\220\344\272\244/wudi/3.5homework-avg.py" "b/week3\345\244\247\344\275\234\344\270\232\346\217\220\344\272\244/wudi/3.5homework-avg.py" new file mode 100644 index 0000000..ad3fdf3 --- /dev/null +++ "b/week3\345\244\247\344\275\234\344\270\232\346\217\220\344\272\244/wudi/3.5homework-avg.py" @@ -0,0 +1,47 @@ + +# coding: utf-8 + +# In[1]: + +import pymongo,charts + + +# In[2]: + +client = pymongo.MongoClient('localhost',27017) +ganji = client['ganji'] +the_sample2 = ganji['the_sample2'] + + +# In[3]: + +def get_avg(cates): + pipeline = [ + {'$match':{'$and':[{'cates':cates}, + {'look':{'$nin':['-']}} + ]}}, + {'$group':{'_id':'$look','avg':{'$avg':'$price'}}}, + {'$sort':{'avg':-1}} + + + ] + + for x in the_sample2.aggregate(pipeline): + yield x['avg'] + + +# In[4]: + +data = [i for i in get_avg('北京二手手机')] +options = { + 'title':{'text':'新旧-价格'}, + 'xAxis':{'categories':['95成新', '全新', '99成新', '9成新', '8成新', '7成新及以下', '报废机/尸体']}, + 'yAxis':{'title':{'text':'价格'}} +} +charts.plot(data,show='inline',options=options) + + +# In[ ]: + + +