python 数学建模
python 常用库
数值计算-Numpy
帮助文档
常用操作
1、创建数组
1 2 3 arr = np.array([1 , 2 , 3 , 4 , 5 ]) print (type (arr))
多维
1 2 3 4 arr = np.array([[1 , 2 , 3 , 4 , 5 ], [2 , 3 , 4 , 5 , 6 ]]) print (arr.shape)
2、索引和切片
1 2 3 4 5 6 7 8 9 10 11 12 13 arr1 = np.array([1 , 2 , 3 , 4 , 5 ]) arr2 = np.array([[1 , 2 , 3 ], [4 , 5 , 6 ], [7 , 8 , 9 ]]) print (arr1[0 ]) print (arr1[0 : 3 ]) print (arr2[0 ]) print (arr2[0 : 2 ])"""" [[1 2 3] [4 5 6]] """ "
3、运算
python中列表加法为拼接,np中为向量加法
1 2 3 4 5 arr1 = np.array([1 , 2 , 3 ]) arr2 = np.array([4 , 5 , 6 ]) print (arr1 + arr2) print (arr1 * arr2)
点乘
1 2 3 arr1 = np.array([1 , 2 , 3 ]) arr2 = np.array([4 , 5 , 6 ]) print (np.dot(arr1, arr2))
1 2 3 4 5 6 7 8 9 A = np.array([[1 , 2 ], [3 , 4 ]]) B = np.array([[5 , 6 ], [7 , 8 ]]) C = np.dot(A, B) print (C)''' [[19 22] [43 50]] '''
均值、标准差、求和、最大值、最小值
1 2 3 4 5 6 arr1 = np.array([1 , 2 , 3 ]) print (arr1.mean()) print (arr1.std()) print (arr1.sum ())print (arr1.max ()) print (arr1.min ())
排序
4、形状操作
1 2 3 4 5 6 7 8 9 arr = np.array([[1 , 2 , 3 ], [4 , 5 , 6 ], [7 , 8 , 9 ], [10 , 11 , 12 ]]) print (arr.shape)new_arr = arr.reshape(2 , 6 ) print (new_arr)arr.reshape(-1 )
转置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 print (new_arr)print (new_arr.transpose())''' [[ 1 2 3 4 5 6] [ 7 8 9 10 11 12]] [[ 1 7] [ 2 8] [ 3 9] [ 4 10] [ 5 11] [ 6 12]] '''
5、元素筛选
1 2 3 4 5 6 7 8 9 10 arr1 = np.array([[1 , 4 , 3 ], [2 , 5 , 6 ]]) print (arr1 > 3 )print (arr1[arr1 > 3 ])''' [[False True False] [False True True]] [4 5 6] '''
6、导出和导入
1 2 3 4 5 A = np.array([[1 , 2 ], [3 , 4 ]]) B = np.array([[5 , 6 ], [7 , 8 ]]) C = np.dot(A, B) np.save("arr" , C)
1 2 arr = np.load("arr.npy" ) print (arr)
数据处理-Pandas
帮助文档
1、读取excel(其他数据格式可查看文档)
1 2 3 4 5 import pandas as pddf = pd.read_excel("1.xlsx" , "Sheet1" , engine="openpyxl" ) print (df.head(5 ))print (type (df))
将数据转换为pandas格式
1 2 3 data = {'姓名' : [1 , 2 , 3 ], '成绩' : [1 , 2 , 3 ]} data_df = pd.DataFrame(data) print (data_df)
2、查看信息(样本数,数据类型等)
1 2 df = pd.read_excel("1.xlsx" , "Sheet1" , engine="openpyxl" ) print (df.info())
3、处理数据
缺失值
1 2 df = pd.read_excel("1.xlsx" , "Sheet1" , engine="openpyxl" ) df = df.dropna()
类型转换
1 2 3 df = pd.read_excel("1.xlsx" , "Sheet1" , engine="openpyxl" ) df['成绩' ] = df['成绩' ].astype(float ) print (df.info())
4、数据选择和过滤
1 2 3 df = pd.read_excel("1.xlsx" , "Sheet1" , engine="openpyxl" ) avg = df['成绩' ].mean() print (df[df['成绩' ] >= avg])
可按照3 σ 3\sigma 3 σ 原则筛选异常值
可视化-Matplotlib
帮助文档
折线图
1 2 3 4 5 6 7 8 9 10 import numpy as npimport matplotlib.pyplot as pltx = np.linspace(0 , 10 , 100 ) y = np.sin(x) plt.plot(x, y) plt.title("y = sin(x)" ) plt.xlabel("x" ) plt.ylabel("y" ) plt.show()
散点图
1 2 3 4 5 6 7 x = np.linspace(0 , 10 , 10 ) y = np.sin(x) plt.scatter(x, y) plt.title("y = sin(x)" ) plt.xlabel("x" ) plt.ylabel("y" ) plt.show()
结合(拟合图像绘制)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.pylab import mpl mpl.rcParams['font.sans-serif' ] = ['SimHei' ] mpl.rcParams['axes.unicode_minus' ] = False x = np.linspace(0 , 10 , 10 ) y = np.sin(x) x2 = np.linspace(0 , 10 , 100 ) y2 = np.sin(x2) plt.scatter(x, y, marker='*' , c='r' , label="数据点" ) plt.plot(x2, y2, linestyle='--' , label="折线" ) plt.legend() plt.title("y = sin(x)" ) plt.xlabel("x" ) plt.ylabel("y" ) plt.show()
多图绘制
1 2 3 4 5 6 7 8 9 10 11 12 x = np.linspace(0 , 10 , 10 ) y = np.sin(x) x2 = np.linspace(0 , 10 , 100 ) y2 = np.sin(x2) fig, axes = plt.subplots(1 , 2 ) axes[0 ].scatter(x, y, marker='*' , c='r' , label="数据点" ) axes[0 ].set_title("数据点" ) axes[1 ].plot(x2, y2, linestyle='--' , label="折线" ) axes[1 ].set_title("拟合曲线" ) fig.legend() plt.show()
直方图
1 2 3 4 x = [1 , 2 , 3 ] y = [2 , 4 , 10 ] plt.bar(x, y) plt.show()
常见模型
评价决策类
归一化->设置权重->加权评价
层次分析
自顶向下分为目标层(分析问题的预定目标或理想结果)、准则层(所需考虑的准则)、方案层(可供选择的决策方案)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import numpy as np A = np.array([[1 , 2 , 3 , 5 ], [1 /2 , 1 , 1 /2 , 2 ], [1 /3 , 2 , 1 , 2 ], [1 /5 , 1 /2 , 1 /2 , 1 ]]) n = A.shape[0 ] eig_val, eig_vec = np.linalg.eig(A) Max_eig = max (eig_val) CI = (Max_eig - n) / (n - 1 ) RI = [0 , 0.0001 , 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 ] CR = CI / RI[n-1 ] if CR < 0.10 : print ("一致性比例为" , CR, ',一致性可以接受' ) else : print ("一致性比例为" , CR, ',需要进行修改' )
算术平均法
权重向量为W i = 1 n ∑ j = 1 n a i j ∑ k = 1 n a k j ( i = 1 , 2 , 3 , … , n ) W_i = \frac{1}{n} \sum_{j=1}^{n} \frac{a_{ij}}{\sum_{k=1}^{n} a_{kj}} \quad (i = 1, 2, 3, \ldots, n) W i = n 1 ∑ j = 1 n ∑ k = 1 n a k j a i j ( i = 1 , 2 , 3 , … , n )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import numpy as np A = np.array([[1 , 2 , 3 , 5 ], [1 /2 , 1 , 1 /2 , 2 ], [1 /3 , 2 , 1 , 2 ], [1 /5 , 1 /2 , 1 /2 , 1 ]]) ASum = np.sum (A, axis=0 ) n = A.shape[0 ] Stand_A = A / ASum ASumr = np.sum (Stand_A, axis=1 ) weights = ASumr / n print (weights)
几何平均法
w i = ( ∏ j = 1 n a i j ) 1 n ∑ k = 1 n ( ∏ j = 1 n a k j ) 1 n ( i = 1 , 2 , … , n ) w_i = \frac{\left(\prod_{j=1}^{n} a_{ij}\right)^{\frac{1}{n}}}{\sum_{k=1}^{n}\left(\prod_{j=1}^{n} a_{kj}\right)^{\frac{1}{n}}} \quad (i = 1, 2, \ldots, n) w i = ∑ k = 1 n ( ∏ j = 1 n a k j ) n 1 ( ∏ j = 1 n a i j ) n 1 ( i = 1 , 2 , … , n )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import numpy as np A = np.array([[1 , 2 , 3 , 5 ], [1 /2 , 1 , 1 /2 , 2 ], [1 /3 , 2 , 1 , 2 ], [1 /5 , 1 /2 , 1 /2 , 1 ]]) prod_A = np.prod(A, axis=1 ) n = A.shape[0 ] prod_n_A = np.power(prod_A, 1 /n) re_prod_A = prod_n_A / np.sum (prod_n_A) print (re_prod_A)
特征值法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import numpy as np A = np.array([[1 , 2 , 3 , 5 ], [1 /2 , 1 , 1 /2 , 2 ], [1 /3 , 2 , 1 , 2 ], [1 /5 , 1 /2 , 1 /2 , 1 ]]) n = A.shape[0 ] eig_val, eig_vec = np.linalg.eig(A) max_index = np.argmax(eig_val) max_vector = eig_vec[:, max_index] weights = max_vector / np.sum (max_vector) print (weights)
TOPSIS法
通过最接近理想解且最远离负理想解确定最优选择
1 2 3 4 5 6 7 8 9 10 11 12 13 import numpy as npn = int (input ("输入参评数目:" )) m = int (input ("输入指标数目:" )) kind = input ("输入类型矩阵:1、极大型 2、极小型 3、中间型 4、区间型" ).split(" " ) A = np.zeros(shape=(n, m)) for i in range (n): A[i] = input (f"输入第{i+1 } 人各指标" ).split(" " ) A[i] = list (map (float , A[i])) print (f"参数矩阵为:\n{A} " )
1 2 3 4 5 def minTomax (max_x, x ): x = list (x) ans = [[(max_x - e)] for e in x] return np.array(ans)
1 2 3 4 5 6 7 8 9 def midTomax (best_x, x ): x = list (x) h = [abs (e - best_x) for e in x] M = max (h) if M == 0 : M = 1 ans = [[(1 -e/M)] for e in h] return np.array(ans)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 def regTomax (low_x, high_x, x ): x = list (x) M = max (low_x-min (x), max (x)-high_x) if M == 0 : M = 1 ans = [] for i in range (len (x)): if x[i]<low_x: ans.append([(1 -(low_x-x[i])/M)]) elif x[i]>high_x: ans.append([(1 -(x[i]-high_x)/M)]) else : ans.append([1 ]) return np.array(ans)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 for i in range (m): if kind[i]=="1" : v = np.array(A[:, i]) elif kind[i] == "2" : maxA = max (A[:, i]) v = minTomax(maxA, A[:, i]) elif kind[i] == "3" : bestA = eval (input ("输入最优值:" )) v = midTomax(bestA, A[:, i]) elif kind[i] == "4" : lowA = eval (input ("输入区间型下界:" )) highA = eval (input ("输入区间型上界:" )) v = regTomax(lowA, highA, A[:, i]) if i == 0 : X = v.reshape(-1 , 1 ) else : X = np.hstack([X, v.reshape(-1 , 1 )]) print (f"正向化后矩阵为:\n{X} " )
1 2 3 4 X = X.astype('float' ) for j in range (m): X[:, j] = X[:, j]/np.sqrt(sum (X[:, j]**2 )) print (f"标准化矩阵为:\n{X} " )
1 2 3 4 5 6 7 8 9 10 11 12 13 x_max = np.max (X, axis=0 ) x_min = np.min (X, axis=0 ) x_max_expanded = np.tile(x_max, (n, 1 )) x_min_expanded = np.tile(x_min, (n, 1 )) d_z = np.sqrt(np.sum (np.square(X - x_max_expanded), axis=1 )) d_f = np.sqrt(np.sum (np.square(X - x_min_expanded), axis=1 )) print ("每个指标最大值:" , x_max)print ("每个指标最小值:" , x_min)print ("d+向量:" , d_z)print ("d-向量:" , d_f)s = d_f/(d_z + d_f) Score = 100 *s/sum (s) print (Score)