python 多维高斯分布数据生成方式

yipeiwu_com6年前Python基础

我就废话不多说了,直接上代码吧!

import numpy as np
import matplotlib.pyplot as plt


def gen_clusters():
  mean1 = [0,0]
  cov1 = [[1,0],[0,10]]
  data = np.random.multivariate_normal(mean1,cov1,100)
  
  mean2 = [10,10]
  cov2 = [[10,0],[0,1]]
  data = np.append(data,
           np.random.multivariate_normal(mean2,cov2,100),
           0)
  
  mean3 = [10,0]
  cov3 = [[3,0],[0,4]]
  data = np.append(data,
           np.random.multivariate_normal(mean3,cov3,100),
           0)
  
  return np.round(data,4)

def save_data(data,filename):
  with open(filename,'w') as file:
    for i in range(data.shape[0]):
      file.write(str(data[i,0])+','+str(data[i,1])+'\n')
      
def load_data(filename):
  data = []
  with open(filename,'r') as file:
    for line in file.readlines():
      data.append([ float(i) for i in line.split(',')])
  return np.array(data)

def show_scatter(data):
  x,y = data.T
  plt.scatter(x,y)
  plt.axis()
  plt.title("scatter")
  plt.xlabel("x")
  plt.ylabel("y")
  
data = gen_clusters()
save_data(data,'3clusters.txt')
d = load_data('3clusters.txt')
show_scatter(d)

以上这篇python 多维高斯分布数据生成方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

解决python2.7 查询mysql时出现中文乱码

问题: python2.7 查询或者插入中文数据在mysql中的时候出现中文乱码 --- 可能情况: 1.mysql数据库各项没有设置编码,默认为'latin' 2.使用MySQL...

Django中在xadmin中集成DjangoUeditor过程详解

Django中在xadmin中集成DjangoUeditor过程详解

环境 python版本:3.6 django:1.10.8 1.下载xadmin https://github.com/sshwsfc/xadmin 下载DjangoUeditor ht...

python操作日期和时间的方法

不管何时何地,只要我们编程时遇到了跟时间有关的问题,都要想到 datetime 和 time 标准库模块,今天我们就用它内部的方法,详解python操作日期和时间的方法。1.将字符串的时...

分析python切片原理和方法

分析python切片原理和方法

使用索引获取列表的元素(随机读取) 列表元素支持用索引访问,正向索引从0开始 colors=["red","blue","green"] colors[0] =="red"...

python对配置文件.ini进行增删改查操作的方法示例

前言 本文主要给大家介绍的是关于python对配置文件.ini增删改查操作的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍: 一、先导入configobj库文件...