211 lines
4.4 KiB
Python
211 lines
4.4 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
Created on Mon May 28 17:26:56 2018
|
|
|
|
@author: luogan
|
|
"""
|
|
import pymongo
|
|
import pytz
|
|
import pandas as pd
|
|
import numpy
|
|
import collections
|
|
import pandas
|
|
import time
|
|
|
|
|
|
|
|
def get_weekend(tt):
|
|
|
|
from dateutil.parser import parse
|
|
bb=parse(tt)
|
|
pacific = pytz.timezone('Asia/Shanghai')
|
|
return pacific.localize(bb)
|
|
|
|
|
|
weekend = get_weekend('20180309')
|
|
client=pymongo.MongoClient('192.168.10.182',27017)
|
|
db=client.fangjia.district_stat_0604
|
|
|
|
|
|
|
|
def data(ct,rl,lk):
|
|
|
|
query={'weekend':{'$gte':weekend},'city':ct,'region':rl,'district_name':lk}
|
|
#query={'weekend':weekend,'city':'上海'}
|
|
|
|
ll=list(db.find(query))
|
|
|
|
pl=pd.DataFrame(ll)
|
|
return pl
|
|
|
|
|
|
|
|
#a=pd.read_excel('0529.xls')
|
|
|
|
#a1=a[a['出现问题次数']>1]
|
|
|
|
|
|
def k_means(pp1):
|
|
from sklearn.cluster import KMeans
|
|
pv = list(pp1)
|
|
if len(set(pv)) > 3:
|
|
gf = numpy.array([pv]).T
|
|
estimator = KMeans(n_clusters=3) # 构造聚类器
|
|
estimator.fit(gf) # 聚类
|
|
label_pred = estimator.labels_ # 获取聚类标签
|
|
aa = collections.Counter(label_pred)
|
|
v = pandas.Series(aa)
|
|
gg = list(v)
|
|
index_max = gg.index(max(gg))
|
|
|
|
centroids = estimator.cluster_centers_ # 获取聚类中心
|
|
# inertia = estimator.inertia_ # 获取聚类准则的总和
|
|
center = centroids[index_max][0]
|
|
return (float(center))
|
|
else:
|
|
return float(pp1.mean())
|
|
|
|
|
|
def modify(df,k,km_price):
|
|
|
|
#print(k)
|
|
|
|
this_value=df['avg_price'][k]
|
|
|
|
rat=abs(this_value-km_price)/min(this_value,km_price)
|
|
|
|
if rat>0.08:
|
|
df['avg_price'][k]=km_price
|
|
return df
|
|
else:
|
|
return df
|
|
|
|
def mm(df,j,km_price):
|
|
if j==0:
|
|
df1=modify(df,j,km_price)
|
|
|
|
df2=modify(df1,j+1,km_price)
|
|
return df2
|
|
elif j==df.shape[0]-1:
|
|
df0=modify(df,j-1,km_price)
|
|
df1=modify(df0,j,km_price)
|
|
#df2=modify(df1,j+1,km_price)
|
|
return df1
|
|
else:
|
|
print('#'*10)
|
|
df=modify(df,j-1,km_price)
|
|
df=modify(df,j,km_price)
|
|
df=modify(df,j+1,km_price)
|
|
return df
|
|
|
|
'''
|
|
cl=pymongo.MongoClient('192.168.10.182',27017)
|
|
db182=cl.lg.district_stat
|
|
'''
|
|
|
|
'''
|
|
for i in a.index:
|
|
|
|
b1=a.iloc[i]
|
|
ct=b1['city']
|
|
rl=b1['region']
|
|
lk=b1['district_name']
|
|
print(ct,rl,lk)
|
|
modify_and_save(ct,rl,lk)
|
|
'''
|
|
|
|
def modify_and_save(ct,rl,lk):
|
|
|
|
print(ct,rl,lk)
|
|
df=data(ct,rl,lk)
|
|
|
|
#df_out=df[(df['markup']>1000) |(df['markup']<-1000) ]
|
|
|
|
|
|
|
|
km_price= k_means(df['calc_value'].dropna())
|
|
|
|
|
|
'''
|
|
if df_out.shape[0]>0:
|
|
|
|
for j in df_out.index:
|
|
df= mm(df,j,km_price)
|
|
#print(df)
|
|
#time.sleep(3)
|
|
'''
|
|
for n in df.index:
|
|
|
|
df=modify(df,n,km_price)
|
|
|
|
df=df.sort_values(by='weekend',ascending=False)
|
|
|
|
for m in range(df.shape[0]-1):
|
|
p1=df.iloc[m]
|
|
price=p1['avg_price']
|
|
_id=p1['_id']
|
|
|
|
def mark_up(df,m):
|
|
|
|
p2=df.iloc[m+1]
|
|
price_2=p2['avg_price']
|
|
t1=p1['weekend']
|
|
t2=p2['weekend']
|
|
|
|
delta=t1-t2
|
|
|
|
print(delta.days)
|
|
if abs(delta.days)==7:
|
|
make_up = 10000 * (price - price_2) / price_2
|
|
|
|
return int(make_up)
|
|
else:
|
|
return int(0)
|
|
|
|
|
|
mp=mark_up(df,m)
|
|
|
|
print(mp)
|
|
|
|
if abs(mp)>1000:
|
|
print(mp)
|
|
df['avg_price'].iloc[m]=km_price
|
|
|
|
p2o=df.iloc[m+1]
|
|
price_2o=p2o['avg_price']
|
|
mpo=10000 * (km_price - price_2o) / price_2o
|
|
df['markup'].iloc[m]=mpo
|
|
else:
|
|
df['markup'].iloc[m]=mp
|
|
|
|
#print('df=',df)
|
|
for q in range(df.shape[0]-1):
|
|
|
|
|
|
avg=df['avg_price'].iloc[q]
|
|
mmp=int(df['markup'].iloc[q])
|
|
_id=df['_id'].iloc[q]
|
|
|
|
db.update(
|
|
{"_id": _id},
|
|
{
|
|
|
|
"$set": {'avg_price':int(avg),'markup':mmp,
|
|
|
|
"weekend_markup":mmp,"changed_markup" :mmp }
|
|
|
|
},True
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|