附录2:[开源代码]# 定义函数
def f(x):
return 0.9 * np.exp(-0.08 * (x-8)**2) + 0.1 * np.sin(0.1 *
网页链接 * x)
# 生成x的取值范围
x = np.arange(1, 40)
# 定义sigmoid函数转换后的新函数g(x)
def g(x):
return 1 / (1 + np.exp(-f(x)))
# 计算函数值
y = g(x)
# 定义大学名称对应表
universities = {
1: "北京大学", 2: "北京航空航天大学", 3: "北京理工大学", 4: "北京师范大学", 5: "重庆大学",
6: "东北大学", 7: "大连理工大学", 8: "东南大学", 9: "电子科技大学", 10: "复旦大学",
11: "国防科技大学", 12: "华东师范大学", 13: "哈尔滨工业大学", 14: "华南理工大学", 15: "湖南大学",
16: "华中科技大学", 17: "吉林大学", 18: "兰州大学", 19: "南京大学", 20: "南开大学",
21: "清华大学", 22: "四川大学", 23: "上海交通大学", 24: "天津大学", 25: "同济大学",
26: "武汉大学", 27: "西安交通大学", 28: "西北大学", 29: "西北工业大学", 30: "西北农林科技大学",
31: "厦门大学", 32: "中国海洋大学", 33: "中国科学技术大学", 34: "中国农业大学", 35: "中国人民大学",
36: "浙江大学", 37: "中南大学", 38: "中山大学", 39: "中央民族大学"
}
# g(x)计算得分
new_scores = {k: g(k) for k in universities.keys()}
# 更新DataFrame
new_university_ranking = pd.DataFrame(list(new_scores.items()), columns=["Rank", "New Score"])
new_university_ranking["University"] = new_university_ranking["Rank"].map(universities)
new_university_ranking = new_university_ranking[["Rank", "University", "New Score"]]
# 按新的得分重新排序
new_university_ranking.sort_values(by="New Score", ascending=False, inplace=True)
new_university_ranking.reset_index(drop=True, inplace=True)
new_university_ranking.head(39)
if __name__ == '__main__':
# 支持中文显示
matplotlib.rcParams['font.family'] = 'SimHei' # 使用黑体
matplotlib.rcParams['axes.unicode_minus'] = False # 正确显示负号
# 绘制图形
plt.figure(figsize=(10, 6))
plt.plot(x, y, marker='o')
plt.title("车南大学排行\n$f(x) = 0.9 \cdot e^{-0.08(x-8)^2} + 0.1 \cdot \sin(0.1 \pi x)$")
# plt.title("$f(x) = 0.9 \cdot e^{-0.08(x-8)^2} + 0.1 \cdot \sin(0.1 \pi x)$")
plt.xlabel("x")
plt.ylabel("g(x)")
plt.grid(True)
plt.show()