首页 > 系统相关 >python多进程map用户 scatter绘图 make_blobs聚类数据生成

python多进程map用户 scatter绘图 make_blobs聚类数据生成

时间:2022-11-20 21:56:30浏览次数:60  
标签:map plt blobs idx python label array scatter

python multiprocessing

map(func,iterable [,chunksize ] )

map()内置函数的并行等价物(尽管它只支持一个可迭代的参数)。它会阻塞,直到结果准备就绪。此方法将iterable内的每一个对象作为单独的任务提交给进程池。可以通过将chunksize设置为正整数来指定这些块的(近似)大小。

from multiprocessing import Pool
def test(i):
    print(i)
if  __name__ == "__main__":
    lists = [1, 2, 3]
    pool = Pool(processes=2)       #定义最大的进程数
    pool.map(test, lists)          #p必须是一个可迭代变量。
    pool.close()
    pool.join()
-----------------------------------
©著作权归作者所有:来自51CTO博客作者Python热爱者的原创作品,请联系作者获取转载授权,否则将追究法律责任
python学习:multiprocessing多进程-Pool进程池模块
https://blog.51cto.com/u_14246112/5730105

 

Searching Arrays

You can search an array for a certain value, and return the indexes that get a match.

To search an array, use the where() method.

Example

Find the indexes where the value is 4:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 4, 4])

x = np.where(arr == 4)

print(x)

The example above will return a tuple: (array([3, 5, 6],)

Which means that the value 4 is present at index 3, 5, and 6.

Example

Find the indexes where the values are even:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])

x = np.where(arr%2 == 0)

print(x)     python scatter绘图 举个示例

本文记录了python中的数据可视化——散点图scatter,令x作为数据(50个点,每个30维),我们仅可视化前两维。labels为其类别(假设有三类)。

这里的x就用random来了,具体数据具体分析。

label设定为[1:20]->1, [21:35]->2, [36:50]->3,(python中数组连接方法:先强制转为list,用+,再转回array)

用matplotlib的scatter绘制散点图,legend和matlab中稍有不同,详见代码。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 x = rand(50,30) from numpy import * import matplotlib import matplotlib.pyplot as plt    #basic f1 = plt.figure(1) plt.subplot(211) plt.scatter(x[:,1],x[:,0])    # with label plt.subplot(212) label = list(ones(20))+list(2*ones(15))+list(3*ones(15)) label = array(label) plt.scatter(x[:,1],x[:,0],15.0*label,15.0*label)    # with legend f2 = plt.figure(2) idx_1 = find(label==1) p1 = plt.scatter(x[idx_1,1], x[idx_1,0], marker = 'x', color = 'm', label='1', s = 30) idx_2 = find(label==2) p2 = plt.scatter(x[idx_2,1], x[idx_2,0], marker = '+', color = 'c', label='2', s = 50) idx_3 = find(label==3) p3 = plt.scatter(x[idx_3,1], x[idx_3,0], marker = 'o', color = 'r', label='3', s = 15) plt.legend(loc = 'upper right')

result:

figure(1):


figure(2):

 

sklearn中的make_blobs函数主要是为了生成数据集的,具体如下:

调用make_blobs

make_blobs的用法

data, label = make_blobs(n_features=2, n_samples=100, centers=3, random_state=3, cluster_std=[0.8, 2, 5])

  • n_features表示每一个样本有多少特征值
  • n_samples表示样本的个数
  • centers是聚类中心点的个数,可以理解为label的种类数
  • random_state是随机种子,可以固定生成的数据
  • cluster_std设置每个类别的方差

 

 

 

标签:map,plt,blobs,idx,python,label,array,scatter
From: https://www.cnblogs.com/bonelee/p/16909698.html

相关文章

  • Miniconda & vs code _ How to Set up Python and Visual Studio Code IDE for Dat
    原文:HowtoSetupPythonandVisualStudioCodeIDEforDataScience-OneZeroBlog SettingupPythonandrunningitsmoothlyonyourPCisessentialford......
  • Golang实现hashmap
    golang实现hashmap思路:数组+链表->HashMap1.先看一下go里的map是怎么实现的go实现map采用拉链法的实现,如下图所示,键值对中的键会经过一个哈希函数,哈希函数会帮我们找到......
  • python3-基础篇-11-文件操作
    python中多file的操作:1使用open()方法用于打开一个文件,并返回文件对象(打开文件,得到文件句柄并赋值给一个变量)2.通过文件对象对文件进行一系列操作(通过句柄对文件进行操作)3......
  • python版nbtscan
    python版nbtscan#-*-coding:utf-8-*-importsocketimportsysfromdatetimeimportdatetime#importthreadingimporttimeimportipaddress#识别ip段模块......
  • Python学习笔记:删除多级索引
    在Python中使用stack/unstack/melt/pivot_talbe等函数进行聚合之后,计算得到的结果具有多层索引。一般情况下可以通过额外指定columns或者通过reset_index()可重置......
  • PYTHON_numpy
    C数组中数据类型一致;python列表可不一致,但numpy数组array中数据类型需一致如传进来的包含不同类型,则自动统一为同一类型,优先级:str>float>int1. array():importnum......
  • python算法题1:给定一个已按照升序排列的有序数组,找到两个数使得它们相加之和等于目标
    题目:给定一个已按照升序排列的有序数组,找到两个数使得它们相加之和等于目标数。 函数应该返回这两个下标值index1和index2,其中index1必须小于index2。 说明: ......
  • python中循环值的处理
    以python3为例关于循环中经常出现赋值问题的几个形式(要赋值的变量a,循环变量b)就比如foriinrange(n):相对于b来说 1:a+=b......
  • python-解析式
     列表解析式(ListComprehension) 列表解析式的优势比循环更节省时间和空间。需要更少的代码行。可将迭代语句转换为公式。 公式#不加条件[返回值for元素......
  • python 打包 exe程序 ——注意事项以及运行exe报错【ModuleNotFoundError: No module
    只说打包成单文件——【仅针对win系统】步骤:1、写脚本2、在当前环境下安装pyinstaller3、打开终端界面【terminal】(1)切换到当前程序所在路径(2)运行打包命令: ......