从函数规则创建数组是非常方便的方法。在numpy中我们常用fromfunction函数来实现这个功能。

在numpy的官网有这么一个例子。

 >>> def f(x,y):
... return 10*x+y
...
>>> b = fromfunction(f,(5,4),dtype=int)
>>> b
array([[ 0, 1, 2, 3],
[10, 11, 12, 13],
[20, 21, 22, 23],
[30, 31, 32, 33],
[40, 41, 42, 43]])

查找help()解释如下:

numpy.fromfunction(functionshape**kwargs)[source]

Construct an array by executing a function over each coordinate.

The resulting array therefore has a value fn(x, y, z) at coordinate (x, y, z).

Parameters:

function : callable

The function is called with N parameters, where N is the rank of shape. Each parameter represents the coordinates of the array varying along a specific axis. For example, if shape were (2, 2), then the parameters in turn be (0, 0), (0, 1), (1, 0), (1, 1).

shape : (N,) tuple of ints

Shape of the output array, which also determines the shape of the coordinate arrays passed to function.

dtype : data-type, optional

Data-type of the coordinate arrays passed to function. By default, dtype is float.

Returns:

fromfunction : any

The result of the call to function is passed back directly. Therefore the shape of fromfunction is completely determined by function. If function returns a scalar value, the shape of fromfunction would match the shape parameter.

主要是第二个参数shape,(N,)定义了fromfunction的输出数据形式。

说起来比较绕口,下面用几个例子说明。

 # -*- coding: utf-8 -*-
from numpy import * def f1(x,y):
return x def f2(x,y):
return y def f3(x,y):
return 2*x+y

运行测试:

>>> b=fromfunction(f1, (5,5), dtype = int)
>>> b
array([[0, 0, 0, 0, 0],
[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2],
[3, 3, 3, 3, 3],
[4, 4, 4, 4, 4]])

>>> b=fromfunction(f1, (5,4), dtype = int)
>>> b
array([[0, 0, 0, 0],
[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3],
[4, 4, 4, 4]])

>>> b=fromfunction(f2, (5,5), dtype = int)
>>> b
array([[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]])
>>> b=fromfunction(f3, (5,5), dtype = int)
>>> b
array([[ 0, 1, 2, 3, 4],
[ 2, 3, 4, 5, 6],
[ 4, 5, 6, 7, 8],
[ 6, 7, 8, 9, 10],
[ 8, 9, 10, 11, 12]])
>>>

  从上面的测试可以看出,shape()定义了输出矩阵的大小。如shape(5,4),则x参数是5行1列行列式[0,1,2,3,4]. y参数1行4列行列式[0,1,2,3].

将x,y带人func函数计算,最后结果的每个元素是根据func 函数来计算得出。

numpy函数fromfunction分析的更多相关文章

  1. 『Numpy』内存分析_高级切片和内存数据解析

    在计算机中,没有任何数据类型是固定的,完全取决于如何看待这片数据的内存区域. 在numpy.ndarray.view中,提供对内存区域不同的切割方式,来完成数据类型的转换,而无须要对数据进行额外的co ...

  2. Numpy函数库基础

    利用Numpy函数库构造4*4随机数组,然后将数组转化为矩阵,然后矩阵与其逆矩阵相乘,计算机处理的误差 from numpy import * random.rand(4,4) print(rando ...

  3. [转]Python numpy函数hstack() vstack() stack() dstack() vsplit() concatenate()

    Python numpy函数hstack() vstack() stack() dstack() vsplit() concatenate() 觉得有用的话,欢迎一起讨论相互学习~Follow Me ...

  4. Numpy 函数总结 (不断更新)

    本篇主要收集一些平时见到的 Numpy 函数. numpy.random.seed & numpy.random.RandomState np.random.seed() 和 np.rando ...

  5. (转)x264源码分析(1):main、parse、encode、x264_encoder_open函数代码分析

    转自:http://nkwavelet.blog.163.com/blog/static/2277560382013103010312144/ x264版本:   x264-snapshot-2014 ...

  6. 大数据学习之Scala中main函数的分析以及基本规则(2)

    一.main函数的分析 首先来看我们在上一节最后看到的这个程序,我们先来简单的分析一下.有助于后面的学习 object HelloScala { def main(args: Array[String ...

  7. [学习笔记] numpy次成分分析和PCA降维

    存个代码,以后参考. numpy次成分分析和PCA降维 SVD分解做次成分分析 原图: 次成分复原图: 代码: import numpy as np from numpy import linalg ...

  8. numpy函数库中一些经常使用函数的记录

    ##numpy函数库中一些经常使用函数的记录 近期才開始接触python,python中为我们提供了大量的库,不太熟悉.因此在<机器学习实战>的学习中,对遇到的一些函数的使用方法进行记录. ...

  9. numpy函数库中一些常用函数的记录

    ##numpy函数库中一些常用函数的记录 最近才开始接触Python,python中为我们提供了大量的库,不太熟悉,因此在<机器学习实战>的学习中,对遇到的一些函数的用法进行记录. (1) ...

随机推荐

  1. SQL Server2000导出数据时包含主键、字段默认值、描述等信息

    时经常用SQL Server2000自带的导出数据向导将数据从一台数据库服务器导出到另一台数据库服务器: 结果数据导出了,但表的主键.字段默认值.描述等信息却未能导出,一直没想出什么方法,今天又尝试了 ...

  2. 如何使用C#创建WebService

    使用C#创建WebService,服务端的webservice是必须,中间的soap,Xml我们不用去关心.下面是使用C#创建WebService的简单介绍. AD:51CTO技术沙龙 | 赋予APP ...

  3. 介绍开源的.net通信框架NetworkComms框架 源码分析(六)SendReceiveOptions

    原文网址: http://www.cnblogs.com/csdev Networkcomms 是一款C# 语言编写的TCP/UDP通信框架  作者是英国人  以前是收费的 目前作者已经开源  许可是 ...

  4. SQL(触发器)

    触发器语法格式(实例): ALTER TRIGGER tri_edituserON dbo.sys_UserFOR INSERT,UPDATE,DELETEAS BEGIN ---判断是新增?修改?删 ...

  5. 疯狂Android讲义 - 学习笔记(一)

    常用开发工具的用法 android : 启动Android SDK管理器 android --help  查看具体用法 android create avd -n t10 -t 10 -b armea ...

  6. MySQL Cursor

    MySQL Cursor Summary: in this tutorial, you will learn how to use MySQL cursor in stored procedures ...

  7. IOS 2D游戏开发框架 SpriteKit-->续(postion,锚点,游戏滚动场景)

    一. Postion 这里的postion和app开发中的layer.postion是一样的,postion的值代表该视图在父节点中的相对位置, 比如一个试图的父节点是self.view, 这里我们想 ...

  8. (原) 1.2 Zookeeper伪集群安装

    本文为原创文章,转载请注明出处,谢谢 Zookeeper伪集群安装 zookeeper单机安装配置可以查看 1.1 zookeeper单机安装 1.复制三份zookeeper,分别为zookeeper ...

  9. 设计人员应该看的15个很酷的 iOS 8 设计

    苹果新一代智能手机 iPhone 6 发布已经有一段时间了,一些创意设计师已经开始在设计中采用 iOS 8 设计理念.当然,其中有些是对于未来的展望和大胆的设计.我在这里收集了15个很酷的 iOS 8 ...

  10. 给li设置float浮动属性之后,无法撑开外层ul的问题。

    最近在项目中有好几次遇到这个问题,感觉是浮动引起的,虽然用<div style="clear:both"></div>解决了,但自己不是特别明白,又在网上查 ...