95 lines
3.1 KiB
Python
95 lines
3.1 KiB
Python
"""
|
|
楼盘价格评级
|
|
"""
|
|
"""
|
|
月消费潜力、评级计算
|
|
"""
|
|
from goojia_utils import ElasticSearchClient
|
|
import pandas as pd
|
|
import logging
|
|
from elasticsearch import helpers
|
|
import sys
|
|
|
|
from sklearn.mixture import GaussianMixture
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
es206_client = ElasticSearchClient(host='192.168.0.89', username='elastic', password='elasticsearch')
|
|
index_name = "community_summary"
|
|
|
|
|
|
def gmm_train(total_rate, consumption_df, source, target):
|
|
# 高斯混合模型
|
|
gmm = GaussianMixture(n_components=total_rate, covariance_type='tied', max_iter=200, n_init=10).fit(consumption_df[[source]])
|
|
consumption_df.loc[:, 'cluster'] = gmm.predict(consumption_df[[source]])
|
|
consumption_df.loc[:, 'price_mean_cluster'] = consumption_df.groupby('cluster')[source].transform('mean')
|
|
consumption_df.loc[:, target] = consumption_df['price_mean_cluster'].rank(method='dense', ascending=False).astype(int)
|
|
consumption_df = consumption_df.drop(columns=['cluster', 'price_mean_cluster'], axis=1)
|
|
return consumption_df
|
|
|
|
|
|
def calc_price_rating(city=None):
|
|
"""
|
|
计算消费等级
|
|
"""
|
|
sql = "select id, city, region, name, avg_price from community_summary"
|
|
if pd.notna(city):
|
|
sql += " where city='" + city + "'"
|
|
community_summary_df = es206_client.scan_by_sql_to_data_frame(sql)
|
|
|
|
consumption_aim_df = community_summary_df[['_id', 'city', 'avg_price']]
|
|
consumption_aim_df = consumption_aim_df.dropna(subset=['avg_price'])
|
|
cities = consumption_aim_df['city'].unique()
|
|
source = "avg_price"
|
|
target = "price_rating"
|
|
score_target = "price_score"
|
|
actions = list()
|
|
count = 0
|
|
|
|
for city in cities:
|
|
df = consumption_aim_df[consumption_aim_df['city'] == city].copy()
|
|
if len(df) < 5:
|
|
continue
|
|
df = gmm_train(5, df, source, target)
|
|
if len(df) >= 5:
|
|
df = gmm_train(5, df, source, score_target)
|
|
df['price_score'] /= 5
|
|
|
|
# 批量更新
|
|
for index, item in df.iterrows():
|
|
row = dict()
|
|
_id = str(item["_id"])
|
|
row['price_rating'] = item['price_rating']
|
|
if 'price_score' in item:
|
|
row['price_score'] = item['price_score']
|
|
|
|
action = {'_op_type': 'update',
|
|
'_index': index_name,
|
|
'_id': _id, # 请勿忘记ID
|
|
'doc': row,
|
|
"_retry_on_conflict": 5}
|
|
actions.append(action)
|
|
if len(actions) == 5000:
|
|
helpers.bulk(client=es206_client.elastic_client, actions=actions)
|
|
count += len(actions)
|
|
actions.clear()
|
|
log.info("评级中: 当前已评级{}".format(count))
|
|
if len(actions) > 0:
|
|
helpers.bulk(client=es206_client.elastic_client, actions=actions)
|
|
count += len(actions)
|
|
actions.clear()
|
|
log.info("评级完成,共更新: {}".format(count))
|
|
|
|
|
|
def sync(city=None):
|
|
calc_price_rating(city)
|
|
|
|
|
|
sys_city = sys.argv[1] if len(sys.argv) > 1 else None
|
|
if pd.notna(sys_city) and "," in sys_city:
|
|
cities = sys_city.split(",")
|
|
for city in cities:
|
|
sync(city)
|
|
else:
|
|
sync(sys_city)
|