优化极值过滤

This commit is contained in:
shenjian@fangjia.com
2022-11-25 18:12:49 +08:00
parent 695bb85484
commit 4a849da890
+45 -23
View File
@@ -44,13 +44,13 @@ def calc_consumption_potential(city):
columns = community_summary_df.columns
if 'total_price' not in columns or 'estate_charge' not in columns or 'relative_price_ranking' not in columns or 'avg_cost' not in columns:
return []
community_summary_df = community_summary_df.dropna(how='any').copy()
# 户均总价计算时候改成万
community_summary_df['total_price'] = round(community_summary_df['total_price'] / 10000, 0)
# 极值设置
community_summary_df = filter_min_max(city, community_summary_df).copy()
# 户均总价计算时候改成万
community_summary_df['total_price'] = community_summary_df['total_price'] / 10000
community_summary_df['total_price_mean'] = community_summary_df.groupby(by=['city'])['total_price'].transform(
np.mean)
community_summary_df['total_price_std'] = community_summary_df.groupby(by=['city'])['total_price'].transform(np.std)
@@ -165,23 +165,40 @@ def filter_min_max(city, community_summary_df):
"""
过滤极值
"""
if city in factor_dict:
community_summary_df.loc[
community_summary_df['total_price'] < factor_dict[city]['min_total_price'], 'total_price'] = \
factor_dict[city]['min_total_price']
community_summary_df.loc[
community_summary_df['total_price'] > factor_dict[city]['max_total_price'], 'total_price'] = \
factor_dict[city]['max_total_price']
community_summary_df.loc[
community_summary_df['estate_charge'] < factor_dict[city]['min_estate_charge'], 'estate_charge'] = \
factor_dict[city]['min_estate_charge']
community_summary_df.loc[
community_summary_df['estate_charge'] > factor_dict[city]['max_estate_charge'], 'estate_charge'] = \
factor_dict[city]['max_estate_charge']
community_summary_df.loc[community_summary_df['avg_cost'] < factor_dict[city]['min_avg_cost'], 'avg_cost'] = \
factor_dict[city]['min_avg_cost']
community_summary_df.loc[community_summary_df['avg_cost'] > factor_dict[city]['max_avg_cost'], 'avg_cost'] = \
factor_dict[city]['max_avg_cost']
total_price5 = np.percentile(a=community_summary_df['total_price'], q=2, axis=0)
total_price95 = np.percentile(a=community_summary_df['total_price'], q=98, axis=0)
community_summary_df.loc[community_summary_df['total_price'] > total_price95, 'total_price'] = total_price95
community_summary_df.loc[community_summary_df['total_price'] < total_price5, 'total_price'] = total_price5
estate_charge5 = np.percentile(a=community_summary_df['estate_charge'], q=2, axis=0)
estate_charge95 = np.percentile(a=community_summary_df['estate_charge'], q=98, axis=0)
community_summary_df.loc[community_summary_df['estate_charge'] > estate_charge95, 'estate_charge'] = estate_charge95
community_summary_df.loc[community_summary_df['estate_charge'] < estate_charge5, 'estate_charge'] = estate_charge5
avg_cost5 = np.percentile(a=community_summary_df['avg_cost'], q=2, axis=0)
avg_cost95 = np.percentile(a=community_summary_df['avg_cost'], q=98, axis=0)
community_summary_df.loc[community_summary_df['avg_cost'] > avg_cost95, 'avg_cost'] = avg_cost95
community_summary_df.loc[community_summary_df['avg_cost'] < avg_cost5, 'avg_cost'] = avg_cost5
#if city in factor_dict:
# 户均总价按极值设置可靠性不高,当户均总价补充后,图中大于最大值的量非常多
# community_summary_df.loc[
# community_summary_df['total_price'] < factor_dict[city]['min_total_price'], 'total_price'] = \
# factor_dict[city]['min_total_price']
# community_summary_df.loc[
# community_summary_df['total_price'] > factor_dict[city]['max_total_price'], 'total_price'] = \
# factor_dict[city]['max_total_price']
# community_summary_df.loc[
# community_summary_df['estate_charge'] < factor_dict[city]['min_estate_charge'], 'estate_charge'] = \
# factor_dict[city]['min_estate_charge']
# community_summary_df.loc[
# community_summary_df['estate_charge'] > factor_dict[city]['max_estate_charge'], 'estate_charge'] = \
# factor_dict[city]['max_estate_charge']
# community_summary_df.loc[community_summary_df['avg_cost'] < factor_dict[city]['min_avg_cost'], 'avg_cost'] = \
# factor_dict[city]['min_avg_cost']
# community_summary_df.loc[community_summary_df['avg_cost'] > factor_dict[city]['max_avg_cost'], 'avg_cost'] = \
# factor_dict[city]['max_avg_cost']
return community_summary_df
@@ -189,6 +206,11 @@ def save_png(community_city):
"""
生成财力评级图表
"""
curr_city = community_city['city'].unique()[0]
# 解决中文显示问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.title(curr_city)
plt.subplot(3, 1, 1)
community_city['consumption_rating'].hist(bins=100)
plt.xlabel('consumption_rating')
@@ -214,7 +236,7 @@ def save_png(community_city):
plt.xlabel('relative_price_ranking')
plt.tight_layout()
plt.savefig(save_png_path + "/" + community_city['city'].unique()[0] + ".png")
plt.savefig(save_png_path + "/" + curr_city + ".png")
# 清图
plt.clf()
# 清坐标轴
@@ -223,4 +245,4 @@ def save_png(community_city):
plt.close()
sync()
dingutils.send_text("【分布图生成完毕】财力评级已完成, 请点击 https://nextcloud.fangjia.com/s/Jqs5BzXi8xLYeH9 查看分布图", "c7ac6c86bfbe49a95e30a68616c4d784ae7781c715430dc9c5f6b46d3e6ebd78")
#dingutils.send_text("【分布图生成完毕】财力评级已完成, 请点击 https://nextcloud.fangjia.com/s/Jqs5BzXi8xLYeH9 查看分布图", "c7ac6c86bfbe49a95e30a68616c4d784ae7781c715430dc9c5f6b46d3e6ebd78")