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设计】。

相关文章

Python批量更改文件名的实现方法

Python批量更改文件名的实现方法 前言: 由于后台数据有好多,但是文案提供过来的图片命名全部没有按照格式来命名,Python这么强大的语言,肯定是能够处理这个问题的,于是我就写了一个...

Python Mysql自动备份脚本

测试系统环境  Windows 2003   python 2.5.1  mysql ...

Python中工作日类库Busines Holiday的介绍与使用

Python中工作日类库Busines Holiday的介绍与使用

引言 大家在日常工作中,经常会碰到类似的场景,需要计算在某个时间段内的工作日以及确定某天是否为工作日,这里的介绍的工具包将很好的解决这个问题。 1. 工具包Business Holid...

浅谈django2.0 ForeignKey参数的变化

Django2.0中编写models类下的ForeignKey book = models.ForeignKey('BookInfo') django2.0与之前的1.8不同,...

Python RuntimeError: thread.__init__() not called解决方法

在写一个多线程类的时候调用报错 RuntimeError: thread.__init__() not called 复制代码 代码如下: class NotifyTread(thre...