从函数规则创建数组是非常方便的方法。在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. 基于吉日嘎拉的通用权限管理WebForm版扩展:字典选项管理和缓存管理

    关于字典管理,其实就是2个表,一个表记录字典和对应表,另一个表记录字典内容.我这里改名为字典选项,其实是一个意思.直接上图: 这里的字典选项是分子系统的,每个子系统可以有自己的单独字典,方便管理.但是 ...

  2. div浮动在页面底部

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. java.lang.NullPointerException org.apache.struts2.impl.StrutsActionProxy.getErrorMessage(StrutsActionProxy.java:69)

    采用SSH框架时出现了 java.lang.NullPointerException org.apache.struts2.impl.StrutsActionProxy.getErrorMessage ...

  4. Android使用SQLite数据库(2)

    打开SQLite数据库,首先要建立一个DatabaseHelper类的实例,然后,再获得数据库: DatabaseHelper mDBH; SQLiteDatabase db; mDBH = new ...

  5. cookies

    Cookie[] cookies=request.getCookies(); for(Cookie c:cookies) out.println(c.getValue()+" ") ...

  6. TabControl 显示彩色的图示 (XE6 Firemonkey)

    提示:Delphi 10 Seattle 透过 TImageList 来指定图标,就能显示原来图标的颜色. 下列方法只适用于 XE6 XE6 Firemonkey 里的 TabControl 可以将切 ...

  7. 设置Xshell中支持中文

    执行echo $LANG命令输出的是当前的编码方式,执行locale命令得到系统中所有可用的编码方式.要让Xshell不显示乱码,则要将编码方式改为UTF-8. 在Xshell中[file]-> ...

  8. 框架Maven笔记系列 一 基础

    主题:SpringMVC 学习资料参考网址: 1.http://www.icoolxue.com 2.http://maven.apache.org/ 1.Maven解决了什么问题? Maven基于项 ...

  9. 《Head First Java》——对象的行为

    类所描述的是 对象知道什么与执行什么! 调用两个参数的方法,并传入两个参数 void go(){ TestStuff t = new TestStuff(); t.takeTwo(12,34); } ...

  10. Guava学习笔记:复写的Object常用方法

    在Java中Object类是所有类的父类,其中有几个需要override的方法比如equals,hashCode和toString等方法.每次写这几个方法都要做很多重复性的判断, 很多类库提供了覆写这 ...