python -- map(), numpy -- flatten()

本文介绍了Python内置函数map()如何对序列进行函数映射,并展示了numpy的flatten()方法用于降维numpy数组的不同方式。通过实例演示了使用lambda表达式和numpy数组操作技巧。
摘要由CSDN通过智能技术生成

1 map()

map() 会根据提供的函数对指定序列做映射;

map(function, iterable, ...)

参数:

  • function -- 函数
  • iterable -- 一个或多个序列

第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表;

在python 3中用法:

>>> def square(x) : 
...     return x ** 2
... 

# 返回迭代器
>>> map(square, [1,2,3,4,5])
<map object at 0x7fe223869520>

#使用 list() 转换为列表
>>> list(map(square, [1,2,3,4,5])) 
[1, 4, 9, 16, 25]

# 使用 lambda 匿名函数
>>> list(map(lambda x: x ** 2, [1, 2, 3, 4, 5]))
[1, 4, 9, 16, 25]

2 numpy -- flatten()

flatten是numpy.ndarray.flatten的一个函数,即返回一个一维数组;

        flatten只能适用于numpy对象,即array或者mat,普通的list列表不适用!

        a.flatten():a是个数组,a.flatten()就是把a降到一维,默认是按行的方向降 ;

        a.flatten().A:a是个矩阵,降维后还是个矩阵,矩阵.A(等效于矩阵.getA())变成了数组;

>>> from numpy import *
>>> a=array([[1,2],[3,4],[5,6]])
>>> a
array([[1, 2],
       [3, 4],
       [5, 6]])
>>> a.flatten() #默认按行的方向降维
array([1, 2, 3, 4, 5, 6])
>>> a.flatten('F') #按列降维
array([1, 3, 5, 2, 4, 6])
>>> a.flatten('A') #按行降维
array([1, 2, 3, 4, 5, 6])
>>> a=mat([[1,2,3],[4,5,6]])
>>> a
matrix([[1, 2, 3],
        [4, 5, 6]])
>>> a.flatten()
matrix([[1, 2, 3, 4, 5, 6]])
>>> y=a.flatten().A 
>>> y
array([[1, 2, 3, 4, 5, 6]])
>>> shape(y)
(1, 6)
>>> shape(y[0]) 
(6,)
>>> a.flatten().A[0] 
array([1, 2, 3, 4, 5, 6])

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
K-means clustering is a popular unsupervised machine learning algorithm used for clustering data points into distinct groups or clusters. In OpenCV-Python, you can perform K-means clustering using the `kmeans` function from the `cv2` module. Here is an example code snippet to demonstrate how to use K-means clustering in OpenCV-Python: ```python import cv2 import numpy as np # Load the image image = cv2.imread('image.jpg') # Reshape the image to a 2D array of pixels pixels = image.reshape(-1, 3) # Convert the pixel values to float32 pixels = np.float32(pixels) # Define the criteria for the algorithm (number of iterations and epsilon) criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) # Set the number of clusters k = 5 # Perform K-means clustering _, labels, centers = cv2.kmeans(pixels, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS) # Convert the centers to uint8 centers = np.uint8(centers) # Map each pixel to its corresponding cluster center segmented_image = centers[labels.flatten()] # Reshape the segmented image back to its original shape segmented_image = segmented_image.reshape(image.shape) # Display the original image and the segmented image cv2.imshow('Original Image', image) cv2.imshow('Segmented Image', segmented_image) cv2.waitKey(0) cv2.destroyAllWindows() ``` Make sure to replace `'image.jpg'` with the path to your own image. The code reads an image, reshapes it into a 2D array of pixels, performs K-means clustering, maps each pixel to its corresponding cluster center, and displays the original image and the segmented image. Feel free to ask if you have any further questions!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值