Python實(shí)現(xiàn)TOPSIS分析法的示例代碼
一 開(kāi)發(fā)環(huán)境
集成開(kāi)發(fā)工具:jupyter notebook 6.2.5
集成開(kāi)發(fā)環(huán)境:python 3.10.6
第三方庫(kù):numpy、matplotlib.pyplot
二 題目
題目:評(píng)價(jià)下表中20條河流的水質(zhì)情況。
注:含氧量越高越好(極大型指標(biāo)),PH值越接近7越好(中間型指標(biāo)),細(xì)菌總數(shù)越少越好(極小型指標(biāo)),植物性營(yíng)養(yǎng)物量介于10~20之間最佳,超過(guò)20或低于10均不好(范圍型指標(biāo))。

三 具體實(shí)現(xiàn)
3.1 獲取數(shù)據(jù)
因?yàn)閿?shù)據(jù)量不大,所以本文選擇直接創(chuàng)建20x4的數(shù)據(jù)矩陣
# 含氧量 PH值 細(xì)菌總數(shù) 植物性營(yíng)養(yǎng)物量
def get_matrix() -> np.array:
return np.array([
[4.69, 6.59, 51, 11.94],
[2.03, 7.86, 19, 6.46],
[9.11, 6.31, 46, 8.91],
[8.61, 7.05, 46, 26.43],
[7.13, 6.5, 50, 23.57],
[2.39, 6.77, 38, 24.62],
[7.69, 6.79, 38, 6.01],
[9.3, 6.81, 27, 31.57],
[5.45, 7.62, 5, 18.46],
[6.19, 7.27, 17, 7.51],
[7.93, 7.53, 9, 6.52],
[4.4, 7.28, 17, 25.3],
[7.46, 8.24, 23, 14.42],
[2.01, 5.55, 47, 26.31],
[2.04, 6.4, 23, 17.91],
[7.73, 6.14, 52, 15.72],
[6.35, 7.58, 25, 29.46],
[8.29, 8.41, 39, 12.02],
[3.54, 7.27, 54, 3.16],
[7.44, 6.26, 8, 28.41]
])3.2 數(shù)據(jù)展示
matrix = get_matrix()
print(f"數(shù)據(jù)矩陣為:\n{matrix}")
array([[ 4.69, 6.59, 51. , 11.94],
[ 2.03, 7.86, 19. , 6.46],
[ 9.11, 6.31, 46. , 8.91],
[ 8.61, 7.05, 46. , 26.43],
[ 7.13, 6.5 , 50. , 23.57],
[ 2.39, 6.77, 38. , 24.62],
[ 7.69, 6.79, 38. , 6.01],
[ 9.3 , 6.81, 27. , 31.57],
[ 5.45, 7.62, 5. , 18.46],
[ 6.19, 7.27, 17. , 7.51],
[ 7.93, 7.53, 9. , 6.52],
[ 4.4 , 7.28, 17. , 25.3 ],
[ 7.46, 8.24, 23. , 14.42],
[ 2.01, 5.55, 47. , 26.31],
[ 2.04, 6.4 , 23. , 17.91],
[ 7.73, 6.14, 52. , 15.72],
[ 6.35, 7.58, 25. , 29.46],
[ 8.29, 8.41, 39. , 12.02],
[ 3.54, 7.27, 54. , 3.16],
[ 7.44, 6.26, 8. , 28.41]])3.3 對(duì)矩陣進(jìn)行正向化




# 定義position接收需要進(jìn)行正向化處理的列 position = np.array([1, 2, 3]) # 定義處理類型:1 - > 極小型 2 - > 中間型 3 - > 區(qū)間型 Type = np.array([2, 1, 3])
# 定義正向化函數(shù)
def positivization(x: np.array, pos: int, type: int) -> np.array:
if type == 1:
print(f"第{pos}列是極小型,正在正向化")
x = x.max() - x
elif type == 2:
print(f"第{pos}列是中間型,正在正向化")
best = 7 # 最佳值
abs_max = np.max(np.abs(x - best))
x = 1 - np.abs(x - best) / abs_max
else:
print(f"第{pos}列是區(qū)間型,正在正向化")
left, right = 10, 20 # 區(qū)間的上界和下界
max_tem = max(left - x.min(), x.max() - right)
x = np.where(x < left, 1 - (left - x) / max_tem, x)
x = np.where(x > right, 1 - (x - right) / max_tem, x)
x = np.where(x > 1, 1, x)
print(f"處理后的數(shù)據(jù)為:\n{x}")
return xfor i in range(len(position)):
print(f"當(dāng)前處理的列為:\n{matrix[:, position[i]]}")
matrix[:, position[i]] = positivization(matrix[:, position[i]], position[i], Type[i])
print(f"正向化后的矩陣為:\n{matrix}")
array([[ 4.69 , 0.71724138, 3. , 1. ],
[ 2.03 , 0.40689655, 35. , 0.6940363 ],
[ 9.11 , 0.52413793, 8. , 0.90579084],
[ 8.61 , 0.96551724, 8. , 0.44425238],
[ 7.13 , 0.65517241, 4. , 0.69144339],
[ 2.39 , 0.84137931, 16. , 0.60069144],
[ 7.69 , 0.85517241, 16. , 0.65514261],
[ 9.3 , 0.86896552, 27. , 0. ],
[ 5.45 , 0.57241379, 49. , 1. ],
[ 6.19 , 0.8137931 , 37. , 0.78478825],
[ 7.93 , 0.63448276, 45. , 0.69922213],
[ 4.4 , 0.80689655, 37. , 0.54191876],
[ 7.46 , 0.14482759, 31. , 1. ],
[ 2.01 , 0. , 7. , 0.45462403],
[ 2.04 , 0.5862069 , 31. , 1. ],
[ 7.73 , 0.40689655, 2. , 1. ],
[ 6.35 , 0.6 , 29. , 0.18236819],
[ 8.29 , 0.02758621, 15. , 1. ],
[ 3.54 , 0.8137931 , 0. , 0.4088159 ],
[ 7.44 , 0.48965517, 46. , 0.27312014]])3.4 對(duì)正向化后的數(shù)據(jù)進(jìn)行標(biāo)準(zhǔn)化

Z = matrix / np.sum(matrix * matrix, axis=0) ** 0.5
print(f"標(biāo)準(zhǔn)化后的矩陣為:\n{Z}")
array([[0.16218592, 0.24825528, 0.02454403, 0.30645756],
[0.07019987, 0.14083713, 0.28634707, 0.21269267],
[0.3150349 , 0.18141732, 0.06545076, 0.27758645],
[0.29774429, 0.3341898 , 0.06545076, 0.1361445 ],
[0.24656409, 0.22677165, 0.03272538, 0.21189806],
[0.08264911, 0.29122254, 0.13090152, 0.18408644],
[0.26592957, 0.29599668, 0.13090152, 0.20077341],
[0.32160534, 0.30077082, 0.22089631, 0. ],
[0.18846764, 0.19812681, 0.4008859 , 0.30645756],
[0.21405774, 0.28167426, 0.30270976, 0.24050429],
[0.27422907, 0.21961044, 0.36816052, 0.21428191],
[0.15215736, 0.27928719, 0.30270976, 0.1660751 ],
[0.25797589, 0.05012847, 0.25362169, 0.30645756],
[0.06950825, 0. , 0.05726941, 0.13932297],
[0.07054569, 0.20290095, 0.25362169, 0.30645756],
[0.26731282, 0.14083713, 0.01636269, 0.30645756],
[0.21959074, 0.20767509, 0.237259 , 0.05588811],
[0.2866783 , 0.00954828, 0.12272017, 0.30645756],
[0.12241751, 0.28167426, 0. , 0.12528473],
[0.25728427, 0.16948197, 0.37634187, 0.08369973]])3.5 計(jì)算與最大值的距離和最小值的距離,并算出得分

max_score = np.max(Z, axis=0) min_score = np.min(Z, axis=0) max_dist = np.sum((max_score - Z) * (max_score - Z), axis=1) ** 0.5 min_dist = np.sum((min_score - Z) * (min_score - Z), axis=1) ** 0.5 final_score = (min_dist / (max_dist + min_dist)) final_score /= np.sum(final_score) final_score = np.around(final_score, decimals=3) # 保留精度為3
四 將最后結(jié)果可視化
x = np.arange(20) # 確定柱狀圖數(shù)量,可以認(rèn)為是x方向刻度
color=['red','black','peru','orchid','deepskyblue', 'orange', 'green', 'pink', 'rosybrown', 'gold', 'lightsteelblue', 'teal']
x_label = [chr(i) for i in range(65,85)]
plt.figure(figsize=(12, 8))
plt.xticks(x, x_label) # 繪制x刻度標(biāo)簽
plt.bar(x, final_score,color=color) # 繪制y刻度標(biāo)簽
#設(shè)置網(wǎng)格刻度
plt.grid(True,linestyle=':',color='r',alpha=0.6)
plt.title("TOPSIS's Score")
for xx, yy in zip(x, final_score):
plt.text(xx, yy + 0.001, str(yy), ha='center')
plt.show()
以上就是Python實(shí)現(xiàn)TOPSIS分析法的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Python TOPSIS分析法的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
matplotlib繪制鼠標(biāo)的十字光標(biāo)的實(shí)現(xiàn)(內(nèi)置方式)
這篇文章主要介紹了matplotlib繪制鼠標(biāo)的十字光標(biāo)的實(shí)現(xiàn)(內(nèi)置方式),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
python用for循環(huán)求和的方法總結(jié)
在本篇文章里小編給各位分享了關(guān)于python用for循環(huán)求和的方法以及相關(guān)實(shí)例代碼,需要的朋友們參考學(xué)習(xí)下。2019-07-07
數(shù)據(jù)挖掘之Apriori算法詳解和Python實(shí)現(xiàn)代碼分享
這篇文章主要介紹了數(shù)據(jù)挖掘之Apriori算法詳解和Python實(shí)現(xiàn)代碼分享,本文先是對(duì)Apriori算法做了詳細(xì)介紹,然后給出了Python版實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-11-11
Python OpenCV 基于圖像邊緣提取的輪廓發(fā)現(xiàn)函數(shù)
這篇文章主要介紹了Python OpenCV 基于圖像邊緣提取的輪廓發(fā)現(xiàn)函數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
夯實(shí)基礎(chǔ)Python列表的索引和切片使用示例
這篇文章主要為大家介紹了Python列表的索引和切片使用示例基礎(chǔ)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
Python使用Opencv實(shí)現(xiàn)圖像特征檢測(cè)與匹配的方法
這篇文章主要介紹了Python使用Opencv實(shí)現(xiàn)圖像特征檢測(cè)與匹配的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Python實(shí)現(xiàn)文件壓縮和解壓的示例代碼
這篇文章主要介紹了Python實(shí)現(xiàn)文件壓縮和解壓的方法,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2020-08-08
Python3多線程基礎(chǔ)知識(shí)點(diǎn)
在本篇內(nèi)容里小編給大家分享了關(guān)于Python3多線程基礎(chǔ)知識(shí)點(diǎn)內(nèi)容,需要的朋友們跟著學(xué)習(xí)參考下。2019-02-02
pytest參數(shù)化:@pytest.mark.parametrize詳解
pytest.mark.parametrize裝飾器能夠?qū)y(cè)試函數(shù)進(jìn)行參數(shù)化處理,使得一個(gè)測(cè)試函數(shù)可以用多組數(shù)據(jù)執(zhí)行多次,這有助于檢查不同輸入下的期望輸出是否匹配,提高測(cè)試的效率和覆蓋率,裝飾器可以應(yīng)用于函數(shù)、模塊或類,支持多個(gè)裝飾器組合使用,增強(qiáng)測(cè)試的靈活性和綜合性2024-10-10

