层次分析法详解(AHP),python代码,楼盘综合水平评价算法
层次分析法详解(AHP),python代码,楼盘综合⽔平评价算
该⽂章讲述了层次分析法代码讲解,以及如何应⽤到楼盘综合⽔平分析当中
转载请说明原⽂出处!!
⽬录
⼀、层次分析法代码
1.1 ⽣成判断矩阵的简单⽅法
1.1.1 代码
def get_judgement_matrix(scores):
'''
get judgement matrix  according to personal score.
:
param scores: a list, the item is the score range 1 to 10 means the importance of each sub-indicator.
:return: judgement matrix, item range 1 to 9.
- more: in judgement matrix:
1 means two sub-indicators are the same important.
3 means the first sub-indicator is a little important than another one.
5 means the first sub-indicator is apparently important than another one.
7 means the first sub-indicator is strongly significant than another one.
9 means the first sub-indicator is extremely significant than another one.
and 2, 4, 6, 8 are in the middle degree.
'''
# 评分1——10
length = len(scores)
array = np.zeros((length, length))
for i in range(0, length):
for j in range(0, length):
point1 = scores[i]
point2 = scores[j]
deta = point1 - point2
if deta < 0:
continue
elif deta == 0 or deta == 1:
array[i][j] = 1
如果当时歌词array[j][i] = 1
else:
array[i][j] = deta
array[j][i] = 1 / deta
return array
1.1.2 讲解
原先的⽅法需要构建出判断矩阵,需要填⼊⼀个个矩阵元素,表⽰谁⽐谁的重要程度,在这⾥是利⽤重要性相减。第⼀步,对指标的重要性进⾏打分1-10,以list存。
第⼆步,将list喂给函数,得到判断矩阵。
1.2 获得判断矩阵的最⼤特征值和对应的特征向量
1.2.1 代码
def get_tezheng(array):
'''
get the max eigenvalue and eigenvector
:param array: judgement matrix
:return: max eigenvalue and the corresponding eigenvector
'''
# 获取最⼤特征值和对应的特征向量
te_val, te_vector = np.linalg.eig(array)
list1 = list(te_val)
max_val = np.max(list1)
index = list1.index(max_val)
max_vector = te_vector[:, index]
return max_val, max_vector
1.2.2 讲解
把矩阵喂给函数就⾏了。
1.3 获得RI值
1.3.1 代码
def RImatrix(n):
'''
get RI value according the the order
:param n: matrix order
:return: Random consistency index RI of a n order matrix
'''
n1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
n2 = [0, 0, 0.52, 0.89, 1.12, 1.26, 1.36, 1.41, 1.46, 1.49, 1.52, 1.54, 1.56, 1.58, 1.59, 1.60]
d = dict(zip(n1, n2))
return d[n]
1.3.2 讲解
字典,根据矩阵的维度返回RI值。
1.4 进⾏⼀致性检验
1.4.1 代码
def consitstence(max_val, RI, n):
'''
use the CR indicator to test the consistency of a matrix.
:
param max_val: eigenvalue
:param RI: Random consistency index
:param n: matrix order
:return: true or false, denotes whether it meat the validation of consistency    '''
CI = (max_val - n) / (n - 1)
if RI == 0:
return True
else:
CR = CI / RI
if CR < 0.10:
return True
一把手多少钱
else:
池贤宇整容return False
1.4.2 讲解
CR⼩于0.1,⼀致性检验过关,否则不过关
1.5 最⼤特征值对应的特征向量的归⼀化
1.5.1 代码
def normalize_vector(max_vector):
'''
normalize the vector, the sum of elements is 1.0
:param max_vector: a eigenvector
:return: normalized eigenvector
'''
vector = []
for i in max_vector:
vector.al)
vector_after_normalization = []
sum0 = np.sum(vector)
for i in range(len(vector)):
vector_after_normalization.append(vector[i] / sum0)
vector_after_normalization = np.array(vector_after_normalization)
return vector_after_normalization
1.5.2 讲解
归⼀化后的向量就是⼦指标权重向量,元素加起来值为1。
1.6 以上⼏步的综合
1.6.1 代码
def get_weight(score):
'''
get weight vector according to personal score.
:param score: a list, the item is the score range 1 to 10 means the importance of each sub-indicator.
长春购物
:return: a list, the item is the weight range 0.0 to 1.0.
'''
n = len(score)
array = get_judgement_matrix(score)
max_val, max_vector = get_tezheng(array)
RI = RImatrix(n)
if consitstence(max_val, RI, n) == True:
feature_weight = normalize_vector(max_vector)
购物指数return feature_weight
else:
return [1 / n] * n
1.6.2 讲解
就是把上⾯⼏步放到了⼀个函数中,输⼊⼦指标的打分向量,得到重要性权重向量。
⼆、基于重构的多层次分析法的楼盘综合⽔平评价算法
2.1 说明
这是2018年-2019年的⼀个⼤创项⽬,⽤爬⾍把安居客-长春地区的楼盘信息全给扒下来了,然后做数据分析,项⽬以⼀篇论⽂和软著结题了。
这个⽅法还挺好⽤,可以适⽤于结构化数据和⾮结构化数据,例如数值的,离散特征的,⽂本的等等。
⽂中瞎捣⿎了⼀个基于⼼理满⾜度的归⼀化函数,强⾏塞了个sigmoid函数和⼀个线性函数,现在看着挺low的,我的数学不好,有数学好的同学可以⾃⼰再弄个归⼀化函数。
楼盘的⼆级三级指标挺多的,好些年前,数据没那么复杂,现在数据变复杂多了,指标也挺多样化。⽬前我在实习,需要对邮政业务进⾏综合指标的建⽴,层次达到了4-5层,指标多达百来个,看着头疼。
AHP经常出现在数学建模⾥⾯,同学们可以看看,直接调⽤get_weight函数就能得到权重,挺⽅便的。
⼤创是个好东西,建议参加,我们这个论⽂因为赶时间没能好好搞,建议⽴项后早完成,别⽴项了就不管了。
开源了算法,没开源java平台:个性化辅助购房平台,项⽬设计是在平台上打分,然后http请求将json
个性偏好数据传到python算法中,将评价结果返回。java的开源后续可能有,请关注与期待。
最近还会开源我本科所做的所有项⽬,⼗分丰富,内容包括机器学习⽅⾯的聚类,分类,决策树,SVM,L2回归,ARIMA等等;还包括深度学习⽅⾯的LSTM交通流,包裹量预测,TextCNN⽂本分类,知识图谱KGE,KG-Completion算法等。⼯程上也有,例如基于⼩程序的电商平台。总之东西有点多,反正现在也没书读,⼲脆做整理与总结吧,后续还会建⽴⼀个个⼈主页。
请来个关注,点赞,收藏,有问题欢迎留⾔。
2.2 论⽂
2.3 开源
2.4 代码
# -*- coding: utf-8 -*-
# @Time : 2019/1/5 11:28
# @Author : RedCedar
# @File : run.py
# @Software: PyCharm
# @note:
import pandas as pd
from math import *
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import numpy as np
from scipy.optimize import  fsolve
import math
def get_judgement_matrix(scores):
'''
get judgement matrix  according to personal score.
:param scores: a list, the item is the score range 1 to 10 means the importance of each sub-indicator.    :return: judgement matrix, item range 1 to 9.
- more: in judgement matrix:
1 means two sub-indicators are the same important.
3 means the first sub-indicator is a little important than another one.
5 means the first sub-indicator is apparently important than another one.
7 means the first sub-indicator is strongly significant than another one.
9 means the first sub-indicator is extremely significant than another one.
and 2, 4, 6, 8 are in the middle degree.
'''
# 评分1——10
length = len(scores)
array = np.zeros((length, length))
for i in range(0, length):
for j in range(0, length):
point1 = scores[i]
point2 = scores[j]
deta = point1 - point2
if deta < 0:
continue
elif deta == 0 or deta == 1:
array[i][j] = 1
array[j][i] = 1
else:
array[i][j] = deta
array[j][i] = 1 / deta
return array
def get_tezheng(array):
'''
get the max eigenvalue and eigenvector
:param array: judgement matrix
:return: max eigenvalue and the corresponding eigenvector头发多适合什么发型
'''
# 获取最⼤特征值和对应的特征向量
te_val, te_vector = np.linalg.eig(array)
list1 = list(te_val)
max_val = np.max(list1)
index = list1.index(max_val)
max_vector = te_vector[:, index]
return max_val, max_vector

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。