优化
This commit is contained in:
@@ -30,6 +30,19 @@ reasonable_df = pd.read_excel(path + '/财力评级分布情况20221215.xlsx')
|
||||
reasonable_dict = reasonable_df.set_index('city')['是否合理'].to_dict()
|
||||
|
||||
|
||||
# Min-Max 归一化到 [1, 100]
|
||||
def min_max_scale(df, column, new_min=1, new_max=100):
|
||||
X_min = df[column].min()
|
||||
X_max = df[column].max()
|
||||
|
||||
# 防止分母为零(如果所有值相等)
|
||||
if X_max == X_min:
|
||||
df['consumption_score'] = new_min # 如果所有值相同,设为下界
|
||||
else:
|
||||
df['consumption_score'] = (new_min + (df[column] - X_min) * (new_max - new_min) / (X_max - X_min))
|
||||
df['consumption_score'] = df['consumption_score'].astype(int)
|
||||
return df
|
||||
|
||||
def calc_consumption_potential(city):
|
||||
"""
|
||||
计算消费潜力
|
||||
@@ -107,7 +120,14 @@ def calc_consumption_potential(city):
|
||||
|
||||
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]])
|
||||
gmm = GaussianMixture(
|
||||
n_components=total_rate,
|
||||
covariance_type='full', # 更灵活的协方差矩阵
|
||||
max_iter=200,
|
||||
n_init=10,
|
||||
reg_covar=1e-6, # 正则化防止奇异矩阵
|
||||
random_state=42
|
||||
).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)
|
||||
@@ -145,22 +165,18 @@ def calc_consumption_rating(community_summary_df):
|
||||
df = gmm_train(total_rate, df, "relative_price_ranking_score", "relative_price_rating")
|
||||
df = gmm_train(total_rate, df, "avg_cost_score", "avg_cost_rating")
|
||||
|
||||
# 总分数
|
||||
total_score = 100
|
||||
if len(df) < total_score:
|
||||
# 对于样本低于5的直接排名
|
||||
df["consumption_score"] = df['consumption_potential'].rank(method='dense', ascending=False).astype(int)
|
||||
else:
|
||||
# 极值设置
|
||||
df = filter_min_max(df)
|
||||
df = gmm_train(total_score, df, source, "consumption_score")
|
||||
# 填充缺失值
|
||||
df["consumption_rating"] = df["consumption_rating"].fillna(df["total_price_rating"])
|
||||
|
||||
# 应用归一化
|
||||
df = min_max_scale(df, 'consumption_potential')
|
||||
|
||||
# 批量更新
|
||||
for index, item in df.iterrows():
|
||||
row = dict()
|
||||
_id = str(item["_id"])
|
||||
row['consumption_rating'] = item['consumption_rating']
|
||||
row['consumption_score'] = 101 - item['consumption_score'] if pd.notna(item['consumption_score']) else None
|
||||
row['consumption_score'] = item['consumption_score'] if pd.notna(item['consumption_score']) else None
|
||||
row['consumption_potential'] = item['consumption_potential']
|
||||
row['m_date'] = dateutils.iso_format()
|
||||
row['sub_factor'] = {
|
||||
|
||||
@@ -165,7 +165,7 @@ def detail(city):
|
||||
"获取方式", "相对价格排名", "人均消费", "物业费", "物业费获取方式", '置信度'])
|
||||
nextcloudutils.up_to_nextcloud(save_png_path + "/" + aim_file, nextcloud_aim_file_path + "/" + aim_file)
|
||||
|
||||
dingutils.send_text("【统计】财力评级统计完毕,请点击 https://nextcloud.fangjia.com/s/nMbzk6ZKJDimapf 查看", "32400b949005817550937f82a82c064fe5be65c23204306442c15e8b485d7ef2")
|
||||
#dingutils.send_text("【统计】财力评级统计完毕,请点击 https://nextcloud.fangjia.com/s/nMbzk6ZKJDimapf 查看", "32400b949005817550937f82a82c064fe5be65c23204306442c15e8b485d7ef2")
|
||||
|
||||
|
||||
sys_city = sys.argv[1] if len(sys.argv) > 1 else None
|
||||
|
||||
Reference in New Issue
Block a user