-
Notifications
You must be signed in to change notification settings - Fork 1
/
analysis.py
449 lines (424 loc) · 12.3 KB
/
analysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
from pymongo import MongoClient
from datetime import datetime, date, timedelta
import sys
client = MongoClient('mongodb://localhost:27017')
db = client.lianjia
c_detail = db.house_detail
c_price = db.house_price
c_meta_day = db.house_meta_day
def analyse_today(date = date.today()):
year=date.year
month=date.month
day=date.day
document = {
'year':year,
'month':month,
'day':day
}
# 数据总数
nums = c_price.find({
'year':year,
'month':month,
'detail.day':day,
}).count()
if nums == 0:
return None
# 当日均价、数量
result = c_price.aggregate([
{ '$unwind' : "$detail" },
{
'$match': {
'year':year,
'month':month,
'detail.day':day,
}
},
{
'$group': {
'_id': None,
'单价': { '$avg': "$detail.单价" },
'总数': { '$sum': 1}
}
},
])
for item in result:
document['当日均价'] = item['单价']
document['当日总数'] = item['总数']
# 面积区间
cond = [{"$lt": ["$建筑面积", 160] }, "160-" ,"160+"]
interval = [140,120,100,80,60] # 去除起始值
for i in interval:
s = str(i)
tmp = [
{ "$lt": ["$建筑面积", i] }, s+"-", {"$cond": cond}
]
cond = tmp
result = c_price.aggregate([
{ '$unwind' : "$detail" },
{
'$match': {
'year':year,
'month':month,
'detail.day':day,
}
},
{
"$lookup":
{
'from': 'house_detail',
'localField': 'house_id',
'foreignField': '_id',
'as': 'house_detail'
}
},
{
"$replaceRoot": { "newRoot": { "$mergeObjects": [ { "$arrayElemAt": [ "$house_detail", 0 ] }, "$$ROOT" ] } }
},
{ "$project": { "house_detail": 0 } },
{
"$group": {
"_id": {
"$cond": cond
},
"count": {"$sum": 1},
"avg": {"$avg": "$建筑面积"},
}
},
{
"$project": {
"count": 1,
"percentage": {
"$concat": [
{
"$substr": [
{
"$multiply": [{"$divide": ["$count", {"$literal": nums}]}, 100]
},
0,
5
]
},
"",
"%"
]
}
}
},
])
document['面积区间'] = []
for item in result:
document['面积区间'].append({
'区间': item['_id'],
'数量': item['count'],
'百分比': item['percentage']
})
# 价格区间/单
nums = c_price.find({
'year':year,
'month':month,
'detail.day':day,
}).count()
cond = [{"$lt": ["$detail.单价", 30000] }, "30000-" ,"30000+"]
interval = range(28000,8000,-2000) # 去除起始值
for i in interval:
s = str(i)
tmp = [
{ "$lt": ["$detail.单价", i] }, s+"-", {"$cond": cond}
]
cond = tmp
result = c_price.aggregate([
{ '$unwind' : "$detail" },
{
'$match': {
'year':year,
'month':month,
'detail.day':day,
}
},
{
"$group": {
"_id": {
"$cond": cond
},
"count": {"$sum": 1},
"avg": {"$avg": "$detail.单价"},
}
},
{
"$project": {
"count": 1,
"percentage": {
"$concat": [
{
"$substr": [
{
"$multiply": [{"$divide": ["$count", {"$literal": nums}]}, 100]
},
0,
5
]
},
"",
"%"
]
}
}
}
])
document['单价区间'] = []
for item in result:
document['单价区间'].append({
'区间': item['_id'],
'数量': item['count'],
'百分比': item['percentage']
})
# 价格区间/总
cond = [{"$lt": ["$detail.总价", 400] }, "400-" ,"400+"]
interval = range(380,40,-20) # 去除起始值
for i in interval:
s = str(i)
tmp = [
{ "$lt": ["$detail.总价", i] }, s+"-", {"$cond": cond}
]
cond = tmp
result = c_price.aggregate([
{ '$unwind' : "$detail" },
{
'$match': {
'year':year,
'month':month,
'detail.day':day,
}
},
{
"$group": {
"_id": {
"$cond": cond
},
"count": {"$sum": 1},
"avg": {"$avg": "$detail.总价"},
}
},
{
"$project": {
"count": 1,
"percentage": {
"$concat": [
{
"$substr": [
{
"$multiply": [{"$divide": ["$count", {"$literal": nums}]}, 100]
},
0,
5
]
},
"",
"%"
]
}
}
}
])
document['总价区间'] = []
for item in result:
document['总价区间'].append({
'区间': item['_id'],
'数量': item['count'],
'百分比': item['percentage']
})
# 上涨/下跌数量
delta = timedelta(days=-1)
newdate = date+delta
day_asc = -1 if newdate.day < day else 1
group_price = c_price.aggregate([
{ '$unwind' : "$detail" },
{
"$match": {
'$or': [
{
'year':year,
'month':month,
'detail.day': day
},
{
'year':newdate.year,
'month':newdate.month,
'detail.day': newdate.day
},
]
}
},
{ "$sort": {'detail.day':day_asc} },
{
'$group': {
'_id': {
"house_id": "$house_id"
},
'count': {'$sum': 1},
'data': {'$push': {
"price":"$detail.单价",
"date": {
"year":"$year",
"month":"$month",
"day":"$detail.day",
}
}}
}
},
{
"$match": {
"count": 2
}
},
{
"$project": {
'end': { '$arrayElemAt': ["$data.price", 0] },
'start': { '$arrayElemAt': ["$data.price", -1] },
}
},
{
"$project": {
"isRising" : {
"$cond" : {
"if": { '$gt': [ "$start", "$end" ] },
"then": -1,
"else": {
"$cond": {
"if": {'$eq': [ "$start", "$end" ]},
"then": 0,
"else": 1
}
}
}
},
"diff": {
"$subtract": [ "$start", "$end" ]
}
}
},
{
"$match": {
'$or': [ { "isRising": 1 }, { "isRising": -1 } ]
}
},
# {
# "$group": {
# "_id": {
# "rising":"$isRising",
# },
# "count": {"$sum":1}
# }
# }
{
"$sort": { "diff" : 1 }
}
])
document['涨跌数量'] = {
"上涨": 0,
"下跌": 0
}
document['跌幅最大'] = None
start = True
document['涨幅最大'] = None
for item in group_price:
if start:
document['跌幅最大'] = item
start = False
if item['isRising'] == 1:
document['涨跌数量']['上涨'] += 1
elif item['isRising'] == -1:
document['涨跌数量']['下跌'] += 1
if item != None:
document['涨幅最大'] = item #last one
# todo:今日最贵小区
# 写入数据库
c_meta_day.replace_one(
{
'year':year,
'month':month,
'day':day
},
document,
upsert=True)
return document
def most_house_in_community(self):
'''
# 房屋数量最多的小区
'''
result = self.collection.aggregate([
{
'$group': {
'_id': "$小区名称",
'count': {'$sum': 1}
}
},
{"$sort": {"count": -1}},
{"$limit": 20}
])
for i in result:
print(i)
def group_by_aera(self): # 按区间聚合房屋面积
nums = self.collection.find().count()
result = self.collection.aggregate([
{
"$group": {
"_id": {
"$cond": [
{"$lt": ["$建筑面积", 100]},
"0-100",
{
"$cond": [
{"$lt": ["$建筑面积", 150]},
"100-150",
"150+"
]
}
]
},
"count": {"$sum": 1},
"avg": {"$avg": "$建筑面积"},
}
},
{
"$project": {
"count": 1,
"percentage": {
"$concat": [
{
"$substr": [
{
"$multiply": [{"$divide": ["$count", {"$literal": nums}]}, 100]
},
0,
2
]
},
"",
"%"
]
}
}
}
])
print('房屋面积比例')
for i in result:
print(i)
if __name__ == '__main__':
count = len(sys.argv)
if len(sys.argv) == 1:
begin = datetime.today()
end = datetime.today()
elif len(sys.argv) == 2:
begin = datetime.strptime(sys.argv[1], '%Y-%m-%d')
end = datetime.today()
else:
begin = datetime.strptime(sys.argv[1], '%Y-%m-%d')
end = datetime.strptime(sys.argv[2], '%Y-%m-%d')
print('begin:',begin,'end:',end)
for i in range((end - begin).days + 1):
day = begin + timedelta(days=i)
doc = analyse_today(day)
if doc != None:
print(doc['year'],doc['month'],doc['day'])
# analyse_today(datetime.strptime('2018-12-20', '%Y-%m-%d'))