优化分数

This commit is contained in:
沈健
2025-04-15 18:23:32 +08:00
parent 713e6ce9ba
commit fb8f307596
+58 -12
View File
@@ -87,22 +87,31 @@ def calc_consumption_potential(city):
# community_summary_df['estate_charge_fixed'] = QuantileTransformer(output_distribution="normal").fit_transform(
# community_summary_df[['estate_charge_fix']])
# 异常值处理
total_price_fixed_threshold = community_summary_df['total_price_fixed'].quantile(0.99)
community_summary_df['total_price_fixed'] = np.where(community_summary_df['total_price_fixed'] > total_price_fixed_threshold, total_price_fixed_threshold, community_summary_df['total_price_fixed'])
max_total_price_fixed = community_summary_df['total_price_fixed'].max()
max_avg_cost_fixed = community_summary_df['avg_cost_fixed'].max()
max_estate_charge_fixed = community_summary_df['estate_charge_fixed'].max()
max_relative_price_ranking = community_summary_df['relative_price_ranking'].max()
community_summary_df['total_price_fixed'] = 1 - np.log(community_summary_df['total_price_fixed'] + 1) / np.log(max_total_price_fixed + 1)
# 人均消费极大值处理
community_summary_df['avg_cost_fixed'] = np.where(community_summary_df['avg_cost_fixed'] > 250, 250, community_summary_df['avg_cost_fixed'])
community_summary_df['total_price_fixed'] = np.log(community_summary_df['total_price_fixed'] + 1) / np.log(max_total_price_fixed + 1)
# 人均消费极大值处理
avg_cost_fixed_threshold = community_summary_df['avg_cost_fixed'].quantile(0.99)
community_summary_df['avg_cost_fixed'] = np.where(community_summary_df['avg_cost_fixed'] > avg_cost_fixed_threshold, avg_cost_fixed_threshold, community_summary_df['avg_cost_fixed'])
max_avg_cost_fixed = community_summary_df['avg_cost_fixed'].max()
try:
community_summary_df['avg_cost_fixed'] = np.log1p(community_summary_df['avg_cost_fixed'])
#community_summary_df['avg_cost_fixed'] = np.log1p(community_summary_df['avg_cost_fixed'])
community_summary_df['avg_cost_fixed'] = np.log(community_summary_df['avg_cost_fixed'] + 1) / np.log(max_avg_cost_fixed + 1)
except Exception:
community_summary_df['avg_cost_fixed'] = None
pass
#community_summary_df['avg_cost_fixed'] = 1 - np.log(community_summary_df['avg_cost_fixed'] + 1) / np.log(max_avg_cost_fixed + 1)
community_summary_df['estate_charge_fixed'] = 1 - np.log(community_summary_df['estate_charge_fixed'] + 1) / np.log(max_estate_charge_fixed + 1)
community_summary_df['relative_price_ranking'] = 1 - np.log(community_summary_df['relative_price_ranking'] + 1) / np.log(max_relative_price_ranking + 1)
estate_charge_fixed_threshold = community_summary_df['estate_charge_fixed'].quantile(0.99)
community_summary_df['estate_charge_fixed'] = np.where(community_summary_df['estate_charge_fixed'] > estate_charge_fixed_threshold, estate_charge_fixed_threshold, community_summary_df['estate_charge_fixed'])
max_estate_charge_fixed = community_summary_df['estate_charge_fixed'].max()
community_summary_df['estate_charge_fixed'] = np.log(community_summary_df['estate_charge_fixed'] + 1) / np.log(max_estate_charge_fixed + 1)
#relative_price_ranking_threshold = community_summary_df['relative_price_ranking'].quantile(0.99)
#community_summary_df['relative_price_ranking'] = np.where(community_summary_df['relative_price_ranking'] > relative_price_ranking_threshold, relative_price_ranking_threshold, community_summary_df['relative_price_ranking'])
max_relative_price_ranking_fixed = community_summary_df['relative_price_ranking'].max()
community_summary_df['relative_price_ranking'] = np.log(community_summary_df['relative_price_ranking'] + 1) / np.log(max_relative_price_ranking_fixed + 1)
community_summary_df['estate_charge_mean'] = community_summary_df.groupby(by=['city'])['estate_charge_fixed'].transform(
np.mean)
@@ -197,6 +206,9 @@ def calc_consumption_rating(community_summary_df):
score_df['robust_total_price_score'] = scaler.fit_transform(score_df[['total_price_score']])
score_df = min_max_scale(score_df, 'robust_total_price_score', 'total_price_score_minmax')
df = df.copy()
df_threshold = community_summary_df['consumption_potential'].quantile(0.99)
community_summary_df['consumption_potential'] = np.where(community_summary_df['consumption_potential'] > df_threshold, df_threshold, community_summary_df['consumption_potential'])
df.loc[:, 'normal_consumption_potential'] = QuantileTransformer(output_distribution="normal").fit_transform(df[['consumption_potential']])
if len(df.dropna(subset=['consumption_potential'])) >= total_rate:
df = gmm_train(total_rate, df.dropna(subset=['consumption_potential']), source, target)
@@ -268,9 +280,6 @@ def calc_consumption_rating(community_summary_df):
count += len(actions)
actions.clear()
log.info("评级完成,共更新: {}".format(count))
#df['avg_cost_score_minmax'].hist(bins=100)
#df['consumption_score'].hist(bins=100)
#plt.show()
return pd.merge(community_summary_df, df, how='left', on=['_id', 'city', 'consumption_potential'])
@@ -284,6 +293,7 @@ def sync_by_city(city_now):
if len(community_summary_df) == 0:
log.warning(f"{city_now}无法评级,数量低于5")
return
#plot_img(community_summary_df)
save_png(community_summary_df)
nextcloudutils.up_to_nextcloud(save_png_path + "/" + city_now + ".png", nextcloud_aim_file_path + "/" + city_now + ".png")
@@ -378,6 +388,42 @@ def save_png(community_city):
plt.close()
def plot_img(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_score'].hist(bins=100)
plt.xlabel('consumption_rating')
plt.tight_layout()
plt.subplot(3, 2, 3)
community_city['total_price_score_minmax'].hist(bins=100)
plt.xlabel('total_price')
plt.tight_layout()
plt.subplot(3, 2, 4)
community_city['estate_charge_score_minmax'].hist(bins=100)
plt.xlabel('estate_charge')
plt.tight_layout()
plt.subplot(3, 2, 5)
community_city['avg_cost_score_minmax'].hist(bins=100)
plt.xlabel('avg_cost')
plt.tight_layout()
plt.subplot(3, 2, 6)
community_city['relative_price_ranking_score_minmax'].hist(bins=100)
plt.xlabel('relative_price_ranking')
plt.tight_layout()
# 关窗口
plt.show()
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(",")