正向云模型(生成云)计算及绘图

import csv

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import style

import math


def forwardCloud(Ex,En,He,N):       #正向云模型(生成云)
    cloudpoint=[];
    plt.rcParams['font.sans-serif'] = ['SimHei']
    #  用来正常显示中文标签
    plt.rcParams['axes.unicode_minus'] = False
    # 用来正常显示负号
    fig = plt.figure(0)
    ax = fig.add_subplot(111)
    for i in range(N):
        En_1=np.random.normal(En,He)
        x=np.random.normal(Ex,abs(En_1))
        y=math.exp(-pow(x-Ex,2)/(2*pow(En_1,2)))
        cloudpoint.append([x,y])
    for w in range(len(cloudpoint)):

        ax.scatter(cloudpoint[w][0],cloudpoint[w][1], s=10, alpha=0.5, c='r', marker='o')
    plt.show()
    return cloudpoint
def plot_cloud_model(Ex, En, He, n, title='', grid=False, moni=False):
    Y = np.zeros((1, n))
    X = np.random.normal(loc=En, scale=He, size=n)
    Y = Y[0]
    plt.rcParams['font.sans-serif'] = ['SimHei']
    #  用来正常显示中文标签
    plt.rcParams['axes.unicode_minus'] = False
    # 用来正常显示负号
    fig = plt.figure(0)
    ax = fig.add_subplot(111)
    for i in range(n):
        Enn = X[i]
        X[i] = np.random.normal(loc=Ex, scale=np.abs(Enn), size=1)
        Y[i] = np.exp(-(X[i] - Ex) * (X[i] - Ex) / (2 * Enn * Enn))
        # if X[i]>Ex:
        #             Y[i]=2-Y[i]
        ax.scatter(X[i], Y[i], s=10, alpha=0.5, c='black', marker='o')
        # ax.scatter(X[i], Y[i], s=10, alpha=0.5, c='black', marker='o')
    # if title == '':
    #     title = '期望:%.2f,熵:%.2f,超熵:%.2f,云滴数:%d' % (Ex, En, He, n)
    # ax.scatter(0.162169504,0.48860678,s=10, alpha=0.5, c='black', marker='o')
    # ax.scatter(0.459859948, 1.156647128, s=10, alpha=0.5, c='black', marker='o')
    # ax.scatter(0.464770087, 1.171339248, s=10, alpha=0.5, c='black', marker='o')
    # ax.scatter(0.499928759, 1.51139322, s=10, alpha=0.5, c='black', marker='o')#社会影响力的三人数据
    # ax.scatter(0.164790462, 0.919005218, s=10, alpha=0.5, c='black', marker='o')
    # ax.scatter(0.08666426, 0.734359773, s=10, alpha=0.5, c='black', marker='o')
    # ax.set_title(title)
    # ax.arrow(0,0,0.162169504,0.48860678,length_includes_head = True,head_width = 0.05,head_length = 0.1,fc = 'black',ec = 'black')
    # ax.arrow(0.5, 0, 0.459859948-0.5, 1.156647128, length_includes_head=True, head_width=0.05, head_length=0.1, fc='black',
    #          ec='black')
    # ax.arrow(1, 1.5, 0.464770087-1, 1.171339248-1.5, length_includes_head=True, head_width=0.05, head_length=0.1, fc='black',
    #          ec='black')
    # ax.text(0,0,'柯平',fontsize=12)
    #
    # ax.text(0.5,0, '章成志',fontsize=12)
    # ax.text(1, 1.5, '马海群 ',fontsize=12)
    ax.set_xlabel('评价值')
    ax.set_ylabel('    ')
    ax.grid(False)

    plt.savefig('y11.svg', dpi=600, format='svg', bbox_inches='tight')
    plt.show()
# def Cloud_compute(xl):
#     '''计算云滴的数字特征'''
#     xl = np.array(xl)
#     # S2 = np.var(xl)   #用的方差
#     S2 = np.std(xl)   #用的标准差
#     Ex = np.mean(xl)
#     En = np.sqrt(np.pi/2)  * np.mean( np.abs(xl-Ex) )
#     He = np.sqrt( np.abs(S2*S2 - En*En) )
#     return (Ex, En, He)

def plot_2d_cloud_model(Ex, En, He, n, title='', grid=False):
    Y = np.zeros((1, n))
    X0 = np.random.normal(loc=En[0], scale=He[0], size=n)
    Y = Y[0]
    X1 = np.random.normal(loc=En[1], scale=He[1], size=n)
    plt.rcParams['font.sans-serif'] = ['SimHei']
    # 用来正常显示中文标签
    plt.rcParams['axes.unicode_minus'] = False
    # 用来正常显示负号
    fig = plt.figure(0)
    ax = fig.add_subplot(111, projection='3d')

    for i in range(n):
        Enn0 = X0[i]
        X0[i] = np.random.normal(loc=Ex[0], scale=np.abs(Enn0), size=1)
        # Y0[i] = np.exp(-(X0[i]-Ex[0])*(X0[i]-Ex[0])/(2*Enn*Enn))
        Enn1 = X1[i]
        X1[i] = np.random.normal(loc=Ex[1], scale=np.abs(Enn1), size=1)
        Y[i] = np.exp(-(X0[i] - Ex[0]) * (X0[i] - Ex[0]) / (2 * Enn0 * Enn0) - (X1[i] - Ex[1]) * (X1[i] - Ex[1]) / (
                    2 * Enn1 * Enn1))
        ax.scatter(X0[i], X1[i], Y[i], s=10, alpha=0.5, c='r', marker='o')

    # if title == '':
    #     title = '期望:[%.2f,%.2f],熵:[%.2f,%.2f],超熵:[%.2f,%.2f],云滴数:%d' % (Ex[0], Ex[1], En[0], En[1], He[0], He[1], n)
    # X0=X0.tolist()
    # X1 = X1.tolist()
    # Y = Y.tolist()
    # a = [x for x in X0]
    # b = [y for y in X1]
    # c = [z for z in Y]
    # with open('D:\Python_DATA\datatest1.csv', 'a', encoding='utf-8', newline='') as csvfile:
    #     writer = csv.writer(csvfile)
    #     writer.writerow(X0)
    # with open('D:\Python_DATA\datatest2.csv', 'a', encoding='utf-8', newline='') as csvfile:
    #     writer = csv.writer(csvfile)
    #     writer.writerow(X1)
    # with open('D:\Python_DATA\datatest3.csv', 'a', encoding='utf-8', newline='') as csvfile:
    #     writer = csv.writer(csvfile)
    #     writer.writerow(Y)
    # dataframe = pd.DataFrame(a, b,c)
    # dataframe.to_csv('D:\Python_DATA\datatest5.csv', sep=',')
    ax.set_title(title)
    ax.set_xlabel('学术影响力')
    ax.set_ylabel('社会影响力')
    ax.set_zlabel('隶属度')
    ax.grid(True)
    ax.text(0.45,1,0.5,'柯平',fontsize=12)

    ax.text(-0.45,- 0.7, 0.45, '章成志',fontsize=12)
    ax.text(0.75, 0, -0.1, '马海群 ',fontsize=12)
    ax.quiver(0.45,1,0.5,0.009859948, 0.164790462-1 ,0.354144082,1,color='black')
    ax.quiver(-0.4, 0, 0.4, 0.562169504, 0.499928759, 0.012413722, 1, color='black')
    ax.quiver(0.75, 0, 0, 0.464770087-0.75, 0.08666426, 0.163588352, 1, color='black')
    # plt.arrow(0.459859948,0.164790462,0.854144082,0.5,0.5,0.5,fc='blue',ec='blue',alpha=0.9, overhang=0.5)
    # ax.annotate('柯平', xyz=(0.459859948,0.164790462,0.854144082), xyztext=(0.459859948,0.164790462,0.854144082),xyzcoords='data', arrowprops=dict(arrowstyle="->", connectionstyle="arc3"))
    plt.savefig('yun7.svg', dpi=600, format='svg', bbox_inches='tight')
    plt.show()
if __name__ == '__main__':
    # 二维云图
    # plot_cloud_model(0.362266513, 0.1671896068094979, 0.08904459487007008, 2000)
    # plot_cloud_model(0.2504611603333334, 0.20844084547405517, 0.10636381322231545, 2000)
    plot_cloud_model(0, 10, 1, 2000)
    # forwardCloud(0.362266513, 0.1671896068094979, 0.08904459487007008, 2000)
   # plot_2d_cloud_model([0.362266513, 0.2504611603333334], [0.00557298689364993, 0.006948028182468506], [0.14139436539913042, 0.17912579435447862], 2000)

    # print Cloud_compute(x)

正向云模型(生成云)计算及绘图_第1张图片

你可能感兴趣的:(python,机器学习,python,算法)