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

yipeiwu_com5年前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 time模块详解(常用函数实例讲解,非常好)

Python time模块详解(常用函数实例讲解,非常好)

在开始之前,首先要说明这几点: 1.在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素。由于Python的ti...

python3中int(整型)的使用教程

Python3支持三种不同的数值类型: 整型(int)--通常被称为是整型或整数,可以是正整数或负整数,不带小数点。Python3整型是没有限制大小的,可以当做long类型使用,...

python实现二分查找算法

二分查找算法:简单的说,就是将一个数组先排序好,比如按照从小到大的顺序排列好,当给定一个数据,比如target,查找target在数组中的位置时,可以先找到数组中间的数array[mid...

用python + hadoop streaming 分布式编程(一) -- 原理介绍,样例程序与本地调试

用python + hadoop streaming 分布式编程(一) -- 原理介绍,样例程序与本地调试

MapReduce与HDFS简介 什么是Hadoop? Google为自己的业务需要提出了编程模型MapReduce和分布式文件系统Google File System,并发布了相关论文...

利用Python如何制作好玩的GIF动图详解

利用Python如何制作好玩的GIF动图详解

前言 之前我们分享过用Python进行可视化的9种常见方式。其实我们还能让可视化图形逼格更高一些,今天就分享一下如何让可视化秀起来:用Python和matplotlib制作GIF图表。...