修改: PriceSystem/rent_price/district_map0413.py
This commit is contained in:
@@ -2,9 +2,13 @@
|
||||
"""
|
||||
Created on Wed Nov 1 18:50:26 2017
|
||||
|
||||
@author: Administrator
|
||||
"""
|
||||
@author: 罗干
|
||||
增加注释时间20180606
|
||||
|
||||
快离别,有点忧伤
|
||||
在本算法的构造过程中非常感谢王文艳的帮助
|
||||
"""
|
||||
#导入各种库
|
||||
import os
|
||||
|
||||
##print( os.getcwd())
|
||||
@@ -34,47 +38,20 @@ import calendar
|
||||
import pytz
|
||||
|
||||
|
||||
|
||||
#本函数为计算平米租金的主函数
|
||||
def common_rent_price(ct,rl,p,timd,timm,region_feature,mongodb):
|
||||
|
||||
|
||||
'''
|
||||
def f_del(qq,g):
|
||||
|
||||
if g!= None:
|
||||
|
||||
return qq
|
||||
else:
|
||||
del qq['region']
|
||||
return qq
|
||||
'''
|
||||
|
||||
#本函数主要用于处理DataFrame
|
||||
def add_rent_type(ppd):
|
||||
if 'rent_type' not in ppd.columns:
|
||||
ppd['rent_type']='整租'
|
||||
return ppd
|
||||
|
||||
|
||||
'''
|
||||
def sample_pool(ct,rl,timd,timm):
|
||||
query = {"city":ct,"cat":"rent","region":rl,
|
||||
"p_date":{"$gt":timd,"$lt":timm}}
|
||||
query=f_del(query,rl)
|
||||
|
||||
fields1 = {"total_price":1,"area":1, "city":1,"region":1,
|
||||
"district_name":1,"rent_type":1,"fitment":1,"block":1}
|
||||
|
||||
|
||||
data=fangjia.find_sample_pool(query,fields1)
|
||||
if data.shape[0]>10:
|
||||
|
||||
##print('数据条数=',data.shape[0])
|
||||
return data[['district_name','total_price','area']]
|
||||
else:
|
||||
return numpy.array([])
|
||||
'''
|
||||
|
||||
|
||||
#提取特定时间范围内的第三方均价
|
||||
def disanganf_price(ct,rl,lk,timd,timm):
|
||||
#client1 = pymongo.MongoClient('192.168.0.136',27017)
|
||||
#db1 = client1.fangjia
|
||||
@@ -125,18 +102,18 @@ def common_rent_price(ct,rl,p,timd,timm,region_feature,mongodb):
|
||||
else:
|
||||
del tt,data1
|
||||
return 0
|
||||
|
||||
#用于增加板块字段
|
||||
def add_block(ppd):
|
||||
if 'block' not in ppd.columns:
|
||||
ppd['block']='没有'
|
||||
return ppd
|
||||
|
||||
#用于增加room字段
|
||||
def add_room(ppd):
|
||||
if 'room' not in ppd.columns:
|
||||
ppd['room']=2
|
||||
return ppd
|
||||
|
||||
|
||||
#提取某个小区的经纬度
|
||||
def take_location(ct,rl,p):
|
||||
|
||||
dirlt=p[0]
|
||||
@@ -195,7 +172,7 @@ def common_rent_price(ct,rl,p,timd,timm,region_feature,mongodb):
|
||||
return numpy.array([])
|
||||
|
||||
|
||||
|
||||
#提取特定时间范围内的小区租金房源信息
|
||||
def some_district_information(ct,rl,lk,timd,timm):
|
||||
|
||||
query = {"city":ct,"cat":"rent","region":rl,#'rent_type':'整租',
|
||||
@@ -205,7 +182,7 @@ def common_rent_price(ct,rl,p,timd,timm,region_feature,mongodb):
|
||||
return fangjia.findHouse(query)
|
||||
|
||||
|
||||
|
||||
#提取周边小区数据
|
||||
def neighbor(ct,rl,lng,lat,scale,p):
|
||||
estype=p[2]
|
||||
#client1 = pymongo.MongoClient('192.168.0.136',27017)
|
||||
@@ -261,7 +238,7 @@ def common_rent_price(ct,rl,p,timd,timm,region_feature,mongodb):
|
||||
else:
|
||||
del pos,data,dirst,dirst1,lf,le,lr
|
||||
return numpy.array([])
|
||||
|
||||
#频率最高的租金
|
||||
def most_frecuncy_rent(dd2):
|
||||
|
||||
ppp=dd2['total_price']
|
||||
@@ -289,7 +266,7 @@ def common_rent_price(ct,rl,p,timd,timm,region_feature,mongodb):
|
||||
else:
|
||||
return '没有小区租房数据'
|
||||
|
||||
|
||||
#不断扩大搜索范围,计算价格
|
||||
def no_source_price(ct,rl,location,rf,p,timd,timm):
|
||||
#rent_ratio=rf[0]
|
||||
#rent_most=rf[1]
|
||||
@@ -338,12 +315,15 @@ def common_rent_price(ct,rl,p,timd,timm,region_feature,mongodb):
|
||||
|
||||
|
||||
|
||||
|
||||
#计算小区的平米租金,特别是无房源时的数据
|
||||
def RENT_no_data_mostblock_price(ct,rl,dd,rf,p,timd,timm):
|
||||
lk=p[0]
|
||||
print('rf=',rf)
|
||||
rent_ratio=rf[0]
|
||||
most_rent=rf[1]
|
||||
#在采用房源时计算平米租金已经用过这部分信息,但当时采用一定的过滤,
|
||||
#即用典型面积的0.8到1.2倍过滤,或者rent_type是整租的房源,这样有可能还剩下一些房源
|
||||
#默认本小区的房源计算出的价格比推算出要准确
|
||||
if dd.shape[0]>1:
|
||||
|
||||
ds=dd[['total_price','area','district_name','url','block']]
|
||||
@@ -361,6 +341,7 @@ def common_rent_price(ct,rl,p,timd,timm,region_feature,mongodb):
|
||||
|
||||
dd2=ds2[ds2['total_price']>0.5*blrl]
|
||||
#dd3=dd2[dd2['total_price']<2*blrl/900]
|
||||
#用房源来计算
|
||||
if dd2.shape[0]>5:
|
||||
|
||||
ppp=dd2['total_price']
|
||||
@@ -369,6 +350,7 @@ def common_rent_price(ct,rl,p,timd,timm,region_feature,mongodb):
|
||||
###print('lgg=',lgg)
|
||||
del ds,ds1,ds2,dd2,ppp
|
||||
return lgg
|
||||
#采用均价和区域租售比反算
|
||||
else:
|
||||
del ds,ds1,ds2,dd2
|
||||
|
||||
@@ -401,7 +383,7 @@ def common_rent_price(ct,rl,p,timd,timm,region_feature,mongodb):
|
||||
else:
|
||||
return '没有数据'
|
||||
|
||||
|
||||
#聚类算法
|
||||
def k_means(pp):
|
||||
from sklearn.cluster import KMeans
|
||||
pv=list(pp.dropna())
|
||||
@@ -423,19 +405,8 @@ def common_rent_price(ct,rl,p,timd,timm,region_feature,mongodb):
|
||||
return (round(center,2))
|
||||
|
||||
|
||||
'''
|
||||
def dff(i,j,timd,timm):
|
||||
reef=rent_partial_sell(i,j,timd,timm)
|
||||
##print('$$$$$$$$$$')
|
||||
if type(reef[0])!=str:
|
||||
return reef
|
||||
else:
|
||||
##print('&&&&&&&&&&&&&')
|
||||
return rent_partial_sell(i,None,timd,timm)
|
||||
'''
|
||||
|
||||
|
||||
|
||||
#提取某个小区的信息,包括别名等信息
|
||||
def region_district_list_name(ct,rl,lkk):
|
||||
#import pandas
|
||||
#client1 = pymongo.MongoClient('192.168.0.136',27017)
|
||||
@@ -456,7 +427,7 @@ def common_rent_price(ct,rl,p,timd,timm,region_feature,mongodb):
|
||||
#lf=fangjia.seaweed(query1, fields1)
|
||||
return lf
|
||||
|
||||
|
||||
#一下三个函数和均价计算类似,判断某个小区是不是别墅
|
||||
def strip_villa(ct,rl,p,timd,timm):
|
||||
lw=p[0]
|
||||
#estype=p[2]
|
||||
@@ -574,12 +545,13 @@ def common_rent_price(ct,rl,p,timd,timm,region_feature,mongodb):
|
||||
|
||||
###print('cc1p=',p)
|
||||
|
||||
#提取当前时间点,day 天前的时间
|
||||
def front_step_time(day):
|
||||
now = datetime.datetime.now()
|
||||
front = now - datetime.timedelta(days=day)
|
||||
d1 = front.strftime('%Y%m%d')
|
||||
return int(d1)
|
||||
|
||||
#一维聚类
|
||||
def k_means_clus(pp1, clus):
|
||||
|
||||
from sklearn.cluster import KMeans
|
||||
@@ -598,7 +570,7 @@ def common_rent_price(ct,rl,p,timd,timm,region_feature,mongodb):
|
||||
|
||||
###print(centroids,gg)
|
||||
# inertia = estimator.inertia_ # 获取聚类准则的总和
|
||||
center = centroids[index_max][0]
|
||||
center= centroids[index_max][0]
|
||||
return (int(center))
|
||||
else:
|
||||
return int(pp1.mean())
|
||||
@@ -610,6 +582,8 @@ def common_rent_price(ct,rl,p,timd,timm,region_feature,mongodb):
|
||||
#print('e_time=',time.time()-e)
|
||||
|
||||
###print('cc=',cc['p_date'])
|
||||
#提取小区的特征
|
||||
|
||||
def take_some_district_featture(ct,rl,lk):
|
||||
|
||||
query_feature = {"city": ct, "region": rl,'district_name':lk}
|
||||
@@ -625,9 +599,12 @@ def common_rent_price(ct,rl,p,timd,timm,region_feature,mongodb):
|
||||
else:
|
||||
return -1
|
||||
|
||||
|
||||
#ap 是样本的DataFrame ,ook 是从小区特征库中的本小区的典型面积
|
||||
#本函数就是将本样本中的最有数据提出来
|
||||
def rent_filter(ap,ook):
|
||||
print('ook=',ook)
|
||||
|
||||
#okk 是本小区的典型面积
|
||||
if ook!=None:
|
||||
if ook>0:
|
||||
rdd=ap[(ap['area']>ook*0.8)&(ap['area']<ook*1.2)]
|
||||
@@ -636,6 +613,9 @@ def common_rent_price(ct,rl,p,timd,timm,region_feature,mongodb):
|
||||
return ap[ap['rent_type']=='整租']
|
||||
return ap[ap['rent_type']=='整租']
|
||||
|
||||
#aw是样本的DataFrame
|
||||
#day 是提取进day天的数据
|
||||
#nd 是通过均价反算出的价格
|
||||
def time_filter_price(aw,day,nd):
|
||||
###print('aw=',aw)
|
||||
before=front_step_time(day)
|
||||
@@ -670,10 +650,12 @@ def common_rent_price(ct,rl,p,timd,timm,region_feature,mongodb):
|
||||
else:
|
||||
return -1
|
||||
|
||||
#aw 是样本的DataFrame
|
||||
#nd 是通过均价计算出的平米租金
|
||||
|
||||
def map_time_price(aw,nd):
|
||||
|
||||
|
||||
#提取金30天的租金房源数据并计算平米租金
|
||||
price_15= time_filter_price(aw,30,nd)
|
||||
price_30= time_filter_price(aw,60,nd)
|
||||
price_60= time_filter_price(aw,120,nd)
|
||||
@@ -691,13 +673,14 @@ def common_rent_price(ct,rl,p,timd,timm,region_feature,mongodb):
|
||||
return price_60
|
||||
else:
|
||||
return k_means_clus(aw["total_price"],3)
|
||||
|
||||
#对平米租金的上限进行控制,不大于用均价反算值的三倍
|
||||
def t_xigma(mp,nd):
|
||||
|
||||
if mp<=3*nd:
|
||||
return mp
|
||||
else:
|
||||
return nd
|
||||
#通过样本房源计算平米租金
|
||||
if cc.shape[0]>6:
|
||||
|
||||
a_set={'room','total_price','area','rent_type','hall','fitment','p_date'}
|
||||
@@ -706,14 +689,17 @@ def common_rent_price(ct,rl,p,timd,timm,region_feature,mongodb):
|
||||
if a_set & b_set==a_set:
|
||||
ap=cc[['room','total_price','area','rent_type','hall','fitment','p_date']]
|
||||
#f=time.time()
|
||||
#从数据库中读取小区特征,这个特征来自于小区画像库
|
||||
ook=take_some_district_featture(ct,rl,p[0])
|
||||
#print('f_time=',time.time()-f)
|
||||
##print('ook=',ook)
|
||||
#从样本中分离出可行的数据
|
||||
av=rent_filter(ap,ook)
|
||||
av['total_price']=av['total_price']/av['area']
|
||||
location=take_location(ct,rl,p)
|
||||
if location.shape[0]==1:
|
||||
g=time.time()
|
||||
#g=time.time()
|
||||
#通过租售比计算平米租金,用于计算和过滤
|
||||
nd=no_source_price(ct,rl,location,region_feature,p,timd,timm)
|
||||
#print('g_time=',time.time()-g)
|
||||
if type(nd)!=str:
|
||||
@@ -721,8 +707,9 @@ def common_rent_price(ct,rl,p,timd,timm,region_feature,mongodb):
|
||||
aw=av[av['total_price']>0.6*nd]
|
||||
###print('aw=',aw[['p_date','total_price','area','rent_type']])
|
||||
if aw.shape[0]>5:
|
||||
#参考时间 计算价格
|
||||
mp=map_time_price(aw,nd)
|
||||
|
||||
#对平米租金的上限进行控制
|
||||
md=t_xigma(mp,nd)
|
||||
##print('EEEEEEEEEEEE')
|
||||
return round(md,2)
|
||||
@@ -756,12 +743,14 @@ def common_rent_price(ct,rl,p,timd,timm,region_feature,mongodb):
|
||||
################################################################################################################
|
||||
|
||||
|
||||
|
||||
#将数据保存到数据库
|
||||
def save_to_mongo(ct,rl,lk,timd,timm):
|
||||
|
||||
|
||||
|
||||
lg_db=fangjia.get_collection("fangjia",config.get_sqm_rentprice_db(), config.get_mongodb_host(),27017)
|
||||
|
||||
#读取某个小区的信息
|
||||
def region_district_list(ct,rl,lk):
|
||||
#3333333333333333333333333333333333333333333333
|
||||
#import pandas
|
||||
@@ -790,7 +779,10 @@ def save_to_mongo(ct,rl,lk,timd,timm):
|
||||
b=list(kkg.loc[[i],:].values.flatten())
|
||||
a.append(b)
|
||||
return a
|
||||
def add_alias(ppd): #4444444444444444444444444444444444444444444444444444444
|
||||
#在计算平米租金时,传入的变量是一个数组['小区名','别名','物业类型']
|
||||
#严格按照这样的格式输入,有些小区没有别名,为保证格式的一致性,需要在这个列表上增加别名这一个位置
|
||||
|
||||
def add_alias(ppd):
|
||||
if 'alias' not in ppd.columns:
|
||||
ppd['alias']='无'
|
||||
return ppd
|
||||
@@ -806,13 +798,13 @@ def save_to_mongo(ct,rl,lk,timd,timm):
|
||||
del qq['region']
|
||||
return qq
|
||||
|
||||
|
||||
#本函数同add_alias,增加物业类型
|
||||
def add_eatype(ppd):
|
||||
if 'estate_type2' not in ppd.columns:
|
||||
ppd['estate_type2']='普通住宅'
|
||||
|
||||
return ppd
|
||||
|
||||
#本函数用于处理时间
|
||||
def before_month_lastday(ti,num):
|
||||
from dateutil.parser import parse
|
||||
today=dateutil.parser.parse(str(ti))
|
||||
@@ -837,7 +829,7 @@ def save_to_mongo(ct,rl,lk,timd,timm):
|
||||
return pacific.localize(bb)
|
||||
|
||||
|
||||
|
||||
#将时间转换为int
|
||||
def timestamp_to_int(dd):
|
||||
|
||||
dd['week1']=1
|
||||
@@ -932,6 +924,7 @@ def save_to_mongo(ct,rl,lk,timd,timm):
|
||||
pacific = pytz.timezone('Asia/Shanghai')
|
||||
return pacific.localize(bb)
|
||||
'''
|
||||
#提取区域典型特征,包扩租售比和最大频率的平米租金
|
||||
def region_feature(ct,rl):
|
||||
#client182 = pymongo.MongoClient('192.168.10.182',27017)
|
||||
#db182 = client182.fangjia_stat.rent_1221
|
||||
|
||||
Reference in New Issue
Block a user