Files
PriceSystem/PriceSystem/fix/price_fix.py
T

285 lines
5.7 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 28 16:40:25 2018
@author: luogan
"""
from pymongo import MongoClient
import numpy as np
import pandas as pd
from pandas import DataFrame,Series
from numpy import row_stack,column_stack
from dateutil.parser import parse
import pandas
# import matplotlib.pyplot as plt
import numpy as np
import numpy
import collections
import time
import fangjia
import random
import datetime
'''
ct='上海'
rl='闵行'
lk=['上海康城']
estype='普通住宅'
#上海闵行上海康城上海青浦南山雨果
'''
client = MongoClient('192.168.10.182',27017)
#dt=client.fangjia.district_stat_test
#dt=client.fangjia.district_stat
dt=client.fangjia.district_stat_all_0416
def price_list(ct,rl,lk):
#query = {'city':ct,'region':rl,'district_name':lk}
query = {'city':ct,'region':rl ,'district_name': lk }
fields={'_id':1,'calc_value':1,'weekend':1,'block':1,'level':1,'avg_price':1,'credit':1}
dl = dt.find(query,fields).sort([("weekend",-1)] ).limit(10)
#dl = dt.find(query).sort([("weekend",1)] ).limit(100)
dl1=list(dl)
##print(len(dl1))
df=pandas.DataFrame(dl1)
#print('df=',df[['weekend','avg_price','calc_value']])
if df.shape[0]>0:
def add_block(df):
if 'block' in df.columns:
return df
else:
df['block']='no'
return df
df=add_block(df)
#df2=df[['_id','calc_value','weekend','block','']]
df3=df.sort_values(by='weekend')
if df.shape[0]>1:
return df3
else:
return pd.DataFrame()
else:
return pd.DataFrame()
def plot1(x,y):
z1 = np.polyfit(x, y,3)
# 生成多项式对象
p1 = np.poly1d(z1)
pp1=p1(x)
##################################
#plt.plot(x,y,color='g',linestyle='-',marker='',label=u'理想曲线')
plt.plot(x,y,color='m',linestyle='-',marker='o',label=u'拟合数据')
plt.plot(x,pp1,color='b',linestyle='-',marker='.',label=u"拟合曲线")
# 把拟合的曲线在这里画出来
plt.legend(loc='upper left')
plt.show()
def line_regre(x,y,num):
z1 = np.polyfit(x, y, num)#用3次多项式拟合
p1 = np.poly1d(z1)
oe=p1(len(x))
return oe
##print(p1) #在屏幕上打印拟合多项式
#上海经济建设小区崇明
'''
ct='上海'
rl='闵行'
lk='上海康城'
estype='普通住宅'
'''
'''
ct='上海'
rl='崇明'
lk='上海经济建设小区'
estype='普通住宅'
'''
def compare(dd1,name,credit,calc_value):
y=list(dd1[name].dropna())
#print('y=',y)
kk=len(y)
if kk>2:
#kk1=kk-1
kk1=kk
y1=y[:kk1]
x1=list(range(kk1))
#print('y1=',y1)
#plot1(list(range(kk)) ,y)
fo=line_regre(x1,y1,1)
ratio=calc_value/fo
#print('fo=',fo,name)
if credit>=0.75 and(ratio>0.8 and ratio<1.4) :
#print('cccccc')
return calc_value,1
elif ratio>0.85 and ratio<1.2:
##print('ratio',ratio)
#print('ddddddddddddd')
return calc_value,1
else:
#print('eeeeeeeeeeeeee')
return fo,0
else:
return calc_value,0
def avg_calc(dd1,credit,calc_value):
m=compare(dd1,'calc_value',credit,calc_value)
n=compare(dd1,'avg_price',credit,calc_value)
#print('m=',m)
#print('n=',n)
if m[1]==1 and n[1]==1:
return m[0]
else :
return n[0]
def out_price(ct,rl,lk,calc):
dd=price_list(ct,rl,lk)
#print('dd',dd)
if dd.shape[0]>0:
near_id=dd['_id'].iloc[-1]
##print('near_id',near_id)
credit=dd['credit'].iloc[-1]
##print('credit',credit)
#calc_value=dd['calc_value'].iloc[-1]
calc_value=calc
##print('calc_value',calc_value)
if 'level' in dd.columns:
#dd1=dd[(dd['level']==1) |(dd['credit']>=0.8)]
dd1=dd[dd['level']==1]
if dd1.shape[0]>1:
##print('####################')
return compare(dd1,'calc_value',credit,calc_value)[0],near_id
else:
return avg_calc(dd1,credit,calc_value),near_id
else:
return avg_calc(dd,credit,calc_value),near_id
else:
return []
#pp=out_price(ct,rl,lk)
def update(ct,rl,lk,calc):
dd= out_price(ct,rl,lk,calc)
if len(dd)>0:
return dd[0]
else:
return -1
'''
def update(ct,rl,lk,calc):
dd= out_price(ct,rl,lk,calc)
#return dd[0]
print(dd)
if len(dd)>0:
data={}
data['avg_price']=int(dd[0])
##print(data)
if len(dd)>0:
dt.update_one({"_id": dd[1]},
{"$set": data})
'''
#洛阳西工人名西路8号院
'''
ct='洛阳'
rl='西工'
lk='人民西路8号院'
update(ct,rl,lk)
'''
'''
ct='上海'
rl='闵行'
lk='上海康城'
estype='普通住宅'
calc=38000
update(ct,rl,lk,calc)
'''
'''
calc=38000
def get_district():
seaweeds = []
keys = {"city": 1, "region": 1, "name": 1, "_id": 0}
for dis in fangjia.get_collection("fangjia", "seaweed").find({ "cat": "district", "status": 0,'city':'上海'}, keys):
#for dis in fangjia.get_collection("fangjia", "seaweed").find({ "city": "上海","cat": "district", "status": 0}, keys):
seaweeds.append(dis)
return seaweeds
dd=get_district()
for k in dd:
ct=k['city']
rl=k['region']
lk=k['name']
update(ct,rl,lk,calc)
#update(ct,rl,lk)
'''