"""
Created on Thu Dec 7 13:10:20 2017
@author: Administrator
"""
import pymongo
import pandas
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import pylab as pl
import datetime
ct='上海'
rl='长宁'
lk='天山河畔花园'
client1 = pymongo.MongoClient('192.168.0.136',27017)
db1 = client1.fangjia_stat
stat = db1.test2_1013_lg
client = pymongo.MongoClient('192.168.0.136',27017)
db = client.fangjia
seawater = db.district_stat
def some_district_information(ct,rl,lk):
query = {"city":ct,"region":rl,"district_name":lk}
fields1 = {"city":1,"region":1, "district_name":1,
'calc_value':1,'calc_value':1,'weekend':1}
lt= seawater.count(query)
pos = list()
for s in seawater.find(query,fields1).limit(lt):
pos.append(s)
data=pandas.DataFrame(pos)
del pos
return data
dd1=some_district_information(ct,rl,lk)
aset={'weekend','region','district_name','city','calc_value'}
bset=set(dd1.columns)
if aset & bset == aset:
dd=dd1[['weekend','region','district_name','city','calc_value']].dropna()
if dd.shape[0]>5:
vv=dd.sort_values(by=['weekend'])
fr=vv.shape[0]
print('fr=',fr)
print('****************')
num=min(120,fr)
df=vv.iloc[fr-num:fr]
print('PPPPPPPPPPPPPPPPP')
df.index=list(range(num))
x=list(df.index)
y=list(df['calc_value'])
z1 = np.polyfit(x, y, 10)
p1 = np.poly1d(z1)
print(z1)
print(p1)
xx=df['weekend']
plt.figure(num=None, figsize=(10, 10))
plt.rcParams['font.sans-serif'] = ['FangSong']
plt.rcParams['axes.unicode_minus'] = False
plt.figure()
plt.plot(xx,y,label= '原数据')
c=p1(x)
def choose(x,f,cal):
a=f(x)
if abs(a-cal)/cal<0.03:
return cal
else:
return a
cc=list()
for i in df.index:
xx=i
f=p1
cal=df['calc_value'][i]
b=choose(xx,f,cal)
cc.append(int(b))
df['拟合过滤']=cc
"""
timeStamp = int(time.mktime(timeArray))
timeStamp == 1381419600
"""
leng=list(range(len(cc)-1))
def rate(i):
a0=cc[i]
a1=cc[i+1]
return int(10000*(a1-a0)/a0)
dd=list(map(rate,leng))
dg=[0]
dg.extend(dd)
del dd,leng,y,vv
df['增长率']=dg
xx=df['weekend']
plt.plot(xx,c,c='black',label= '拟合后')
pl.legend()
plt.show()
import dateutil
def before_month_lastday(ti):
today=dateutil.parser.parse(str(ti))
first = datetime.date(day=1, month=today.month, year=today.year)
lastMonth = first + datetime.timedelta(days=ti*31)
print(lastMonth)
cc=str(lastMonth.year)+str(lastMonth.month)+str(lastMonth.day)
return (lastMonth)
def fga(p):
return p*4+120
bb=df[['calc_value','weekend']]
bb1=bb.iloc[80:120]
bby=list(bb1['calc_value'])
bbx=list(bb1.index)
bbz1 = np.polyfit(bbx, bby,2)
bbp1 = np.poly1d(bbz1)
xxx=x[80:120]
bbc=bbp1(xxx)
xcx=df['weekend'][80:120]
plt.figure(num=None, figsize=(8, 6))
plt.rcParams['font.sans-serif'] = ['FangSong']
plt.rcParams['axes.unicode_minus'] = False
plt.plot(xcx,bbc,c='black',label='拟合后')
plt.plot(xcx,bb1['calc_value'])
bnb=[1,2,3,4,5]
bnb1=list(map(before_month_lastday,bnb))
bnb2=list(map(fga,bnb))
vvb=bbp1(bnb2)
vvb=list(map(int,vvb))
print('预测价格',vvb)
print('日期',bnb1)
plt.figure(num=None, figsize=(10, 6))
plt.plot(bnb1,vvb)
"""
xcx=df['weekend']
plt.plot(xcx,c,c='black',label= '拟合后')
"""
print(before_month_lastday(1))
"""
Created on Sun Nov 12 16:21:34 2017
@author: Administrator
"""
import pymongo
import pandas
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import pylab as pl
import sys, time
import pp
ct='上海'
rl='闵行'
lk='和风家园'
def pply(ct,rl,lk):
client1 = pymongo.MongoClient('192.168.0.136',27017)
db1 = client1.fangjia_stat
stat = db1.test2_1013_lg
client = pymongo.MongoClient('192.168.0.60',27017)
db = client.fangjia
seawater = db.district_stat
def some_district_information(ct,rl,lk):
query = {"city":ct,"region":rl,"district_name":lk}
fields1 = {"city":1,"region":1, "district_name":1,
'calc_value':1,'calc_value':1,'weekend':1}
lt= seawater.count(query)
pos = list()
for s in seawater.find(query,fields1).limit(lt):
pos.append(s)
data=pandas.DataFrame(pos)
del pos
return data
dd1=some_district_information(ct,rl,lk)
aset={'weekend','region','district_name','city','calc_value'}
bset=set(dd1.columns)
if aset & bset == aset:
dd=dd1[['weekend','region','district_name','city','calc_value']].dropna()
if dd.shape[0]>5:
vv=dd.sort_values(by=['weekend'])
fr=vv.shape[0]
print('fr=',fr)
print('****************')
num=min(120,fr)
df=vv.iloc[fr-num:fr]
print('PPPPPPPPPPPPPPPPP')
df.index=list(range(num))
x=list(df.index)
y=list(df['calc_value'])
z1 = np.polyfit(x, y, 36)
p1 = np.poly1d(z1)
print(z1)
print(p1)
plt.rcParams['font.sans-serif'] = ['FangSong']
plt.rcParams['axes.unicode_minus'] = False
plt.figure()
plt.plot(x,y,label= '原数据')
plt.scatter(x,y,s=20,c='red',marker='o',alpha=0.9,label= '原数据')
"""
p=p1
def ff(x):
a=len(p)
b=p[0]*1+p[1]*x+p[2]*x**2+p[3]*x**3+p[4]*x**4+p[5]*x**5
return b
c=list(map(ff,x))
"""
c=p1(x)
def choose(x,f,cal):
a=f(x)
if abs(a-cal)/cal<0.03:
return cal
else:
return a
cc=list()
for i in df.index:
xx=i
f=p1
cal=df['calc_value'][i]
b=choose(xx,f,cal)
cc.append(int(b))
df['拟合过滤']=cc
"""
timeStamp = int(time.mktime(timeArray))
timeStamp == 1381419600
"""
leng=list(range(len(cc)-1))
def rate(i):
a0=cc[i]
a1=cc[i+1]
return int(10000*(a1-a0)/a0)
dd=list(map(rate,leng))
dg=[0]
dg.extend(dd)
del dd,leng,y,vv
df['增长率']=dg
"""
def fgg(df,cc):
leng=len(cc)-1
df['增长率']=0
for i in range(leng):
a0=cc[i]
a1=cc[i+1]
ff=(a1-a0)/a0
df['增长率'][i+1]=ff
return df
#dff=fgg(df,cc)
"""
def save_to_mongo(i):
ct=df['city'][i]
rl=df['region'][i]
d_name=df['district_name'][i]
py=cc[i]
ratio=dg[i]
date=df['weekend'][i]
stat.save({"city":ct,"region":rl,"district_name":d_name,
"weeedend":date,"拟合后的价格":py,'增长率':ratio})
"""
region=region_list(i)[0:]
for j in region:
lf=region_district_list(i,j)
testFL=lf['name']
import time
from multiprocessing import Pool
if __name__ == "__main__" :
startTime = time.time()
testFL = df.index
pool = Pool(10)#可以同时跑10个进程
pool.map(save_to_mongo,testFL)
pool.close()
pool.join()
endTime = time.time()
print ("time :", endTime - startTime)
"""
"""
import time
startTime = time.time()
list(map(save_to_mongo,df.index))
endTime = time.time()
print ("time :", endTime - startTime)
"""
"""
import time
startTime = time.time()
for i in df.index:
save_to_mongo(i)
endTime = time.time()
print ("time :", endTime - startTime)
"""
"""
import time
from multiprocessing import Pool
if __name__ == "__main__" :
startTime = time.time()
testFL = df.index
pool = Pool(10)#可以同时跑10个进程
pool.map(save_to_mongo,testFL)
pool.close()
pool.join()
endTime = time.time()
print ("time :", endTime - startTime)
"""
"""
#print(df)
for i in df.index:
ct=df['city'][i]
rl=df['region'][i]
d_name=df['district_name'][i]
py=cc[i-380]
ratio=dg[i-380]
date="2017-11-17"
client1 = pymongo.MongoClient('192.168.0.136',27017)
db1 = client1.fangjia_stat
stat = db1.test2_1013_lg
stat.save({"city":ct,"region":rl,"district_name":d_name,
"weeedend":date,"pp":py
})
#stat.save({"city":ct,"region":rl,
#"district_name":d_name,
#"拟合过滤":python_cal, "weeedend":date})
"""
plt.plot(x,c,c='black',label= '拟合后')
plt.scatter(x,cc,s=20,c='green',marker='*',alpha=0.9,label= '原数据')
pl.legend()
plt.show()
def region_list(city):
client = pymongo.MongoClient('192.168.0.136',27017)
db = client.fangjia
region = db.region_block
query = {"city":city,"category":"region"}
fields1 = {"name":1}
pos = list()
for s in region.find(query,fields1):
pos.append(s)
data=pandas.DataFrame(pos)
del pos
return data['name']
def region_district_list(ct,rl):
import pandas
client1 = pymongo.MongoClient('192.168.0.136',27017)
db1 = client1.fangjia
seaweed1 = db1.seaweed
query1 = {"status":0,"cat":"district","city":ct,"region":rl}
fields1 = {"lat2":1,"lng2":1, "city":1,"region":1,
"cat":1,"name":1,'estate_type2':1,'alias':1}
lct= list()
for s in seaweed1.find(query1, fields1):
lct.append(s)
lf=pandas.DataFrame(lct)
return lf
"""
cti=['上海']
for ct in cti:
region=region_list(ct)
for rl in region[10:]:
dictn=region_district_list(ct,rl)
disname=dictn['name']
for lk in disname:
print('ct=',ct,'rl=',rl,'lk=',lk)
pply(ct,rl,lk)
"""
ct='上海'
rl='闵行'
lk='上海康城'
print('ct=',ct,'rl=',rl,'lk=',lk)
pply(ct,rl,lk)