Python中的numpy函数的使用ones,zeros,eye
在看别人写的代码时,看到的不知道的函数,就在这里记下来。
原文是这样用的:
weights = ones((numfeatures,1))
在python中help():
import numpy as np
help(np.ones)
Help on function ones in module numpy.core.numeric: ones(shape, dtype=None, order='C')
Return a new array of given shape and type, filled with ones. Parameters
----------
shape : int or sequence of ints
Shape of the new array, e.g., ``(2, 3)`` or ``2``.
dtype : data-type, optional
The desired data-type for the array, e.g., `numpy.int8`. Default is
`numpy.float64`.
order : {'C', 'F'}, optional
Whether to store multidimensional data in C- or Fortran-contiguous
(row- or column-wise) order in memory. Returns
-------
out : ndarray
Array of ones with the given shape, dtype, and order. See Also
--------
zeros, ones_like Examples
--------
>>> np.ones(5)
array([ 1., 1., 1., 1., 1.]) >>> np.ones((5,), dtype=np.int)
array([1, 1, 1, 1, 1]) >>> np.ones((2, 1))
array([[ 1.],
[ 1.]]) >>> s = (2,2)
>>> np.ones(s)
array([[ 1., 1.],
[ 1., 1.]])
zeros:
Help on built-in function zeros in module numpy.core.multiarray: zeros(...)
zeros(shape, dtype=float, order='C') Return a new array of given shape and type, filled with zeros. Parameters
----------
shape : int or sequence of ints
Shape of the new array, e.g., ``(2, 3)`` or ``2``.
dtype : data-type, optional
The desired data-type for the array, e.g., `numpy.int8`. Default is
`numpy.float64`.
order : {'C', 'F'}, optional
Whether to store multidimensional data in C- or Fortran-contiguous
(row- or column-wise) order in memory. Returns
-------
out : ndarray
Array of zeros with the given shape, dtype, and order. See Also
--------
zeros_like : Return an array of zeros with shape and type of input.
ones_like : Return an array of ones with shape and type of input.
empty_like : Return an empty array with shape and type of input.
ones : Return a new array setting values to one.
empty : Return a new uninitialized array. Examples
--------
>>> np.zeros(5)
array([ 0., 0., 0., 0., 0.]) >>> np.zeros((5,), dtype=np.int)
array([0, 0, 0, 0, 0]) >>> np.zeros((2, 1))
array([[ 0.],
[ 0.]]) >>> s = (2,2)
>>> np.zeros(s)
array([[ 0., 0.],
[ 0., 0.]]) >>> np.zeros((2,), dtype=[('x', 'i4'), ('y', 'i4')]) # custom dtype
array([(0, 0), (0, 0)],
dtype=[('x', '<i4'), ('y', '<i4')])
eye:
Help on function eye in module numpy.lib.twodim_base: eye(N, M=None, k=0, dtype=<class 'float'>)
Return a 2-D array with ones on the diagonal and zeros elsewhere. Parameters
----------
N : int #行数
Number of rows in the output.
M : int, optional#列数
Number of columns in the output. If None, defaults to `N`.
k : int, optional#对角线的位置,0的时候是正对角线,+1就是对角线向上移,-1就是对角线向下移
Index of the diagonal: 0 (the default) refers to the main diagonal,
a positive value refers to an upper diagonal, and a negative value
to a lower diagonal.
dtype : data-type, optional
Data-type of the returned array. Returns
-------
I : ndarray of shape (N,M)
An array where all elements are equal to zero, except for the `k`-th
diagonal, whose values are equal to one. See Also
--------
identity : (almost) equivalent function
diag : diagonal 2-D array from a 1-D array specified by the user. Examples
--------
>>> np.eye(2, dtype=int)
array([[1, 0],
[0, 1]])
>>> np.eye(3, k=1)
array([[ 0., 1., 0.],
[ 0., 0., 1.],
[ 0., 0., 0.]])
Python中的numpy函数的使用ones,zeros,eye的更多相关文章
- python --- Python中的callable 函数
python --- Python中的callable 函数 转自: http://archive.cnblogs.com/a/1798319/ Python中的callable 函数 callabl ...
- python中使用zip函数出现<zip object at 0x02A9E418>
在Python中使用zip函数,出现<zip object at 0x02A9E418>错误的原因是,你是用的是python2点多的版本,python3.0对python做了改动 zip方 ...
- [转载]python中multiprocessing.pool函数介绍
原文地址:http://blog.sina.com.cn/s/blog_5fa432b40101kwpi.html 作者:龙峰 摘自:http://hi.baidu.com/xjtukanif/blo ...
- Python 中的isinstance函数
解释: Python 中的isinstance函数,isinstance是Python中的一个内建函数 语法: isinstance(object, classinfo) 如果参数object是cla ...
- Python中的map()函数和reduce()函数的用法
Python中的map()函数和reduce()函数的用法 这篇文章主要介绍了Python中的map()函数和reduce()函数的用法,代码基于Python2.x版本,需要的朋友可以参考下 Py ...
- python中multiprocessing.pool函数介绍_正在拉磨_新浪博客
python中multiprocessing.pool函数介绍_正在拉磨_新浪博客 python中multiprocessing.pool函数介绍 (2010-06-10 03:46:5 ...
- 举例详解Python中的split()函数的使用方法
这篇文章主要介绍了举例详解Python中的split()函数的使用方法,split()函数的使用是Python学习当中的基础知识,通常用于将字符串切片并转换为列表,需要的朋友可以参考下 函数:sp ...
- python中的生成器函数是如何工作的?
以下内容基于python3.4 1. python中的普通函数是怎么运行的? 当一个python函数在执行时,它会在相应的python栈帧上运行,栈帧表示程序运行时函数调用栈中的某一帧.想要获得某个函 ...
- python中的map()函数
MapReduce的设计灵感来自于函数式编程,这里不打算提MapReduce,就拿python中的map()函数来学习一下. 文档中的介绍在这里: map(function, iterable, .. ...
随机推荐
- 图解JVM--(二)垃圾回收
垃圾回收 1.如何判断对象可以回收 1.1 引用计数 在对象中添加一个引用计数器,每当有一个地方引用它,计数器值就加一,当引用失效时,计数器值就减一,任何时刻计数器为零的对象就不可能再被使用的,就可以 ...
- Redis 的使用
1. 概念: redis是一款高性能的NOSQL系列的非关系型数据库 1.1.什么是NOSQL NoSQL(NoSQL = Not Only SQL),意即"不仅仅是SQL",是一 ...
- Py西游攻关之基础数据类型(二)-列表
Py西游攻关之基础数据类型 - Yuan先生 https://www.cnblogs.com/yuanchenqi/articles/5782764.html 五 List(列表) OK,现在我们知 ...
- 单表千亿电信大数据场景,使用Spark+CarbonData替换Impala案例
[背景介绍] 国内某移动局点使用Impala组件处理电信业务详单,每天处理约100TB左右详单,详单表记录每天大于百亿级别,在使用impala过程中存在以下问题: 详单采用Parquet格式存储,数据 ...
- 哈希表,Java中的hashCode
哈希表: 将我们所需的键通过哈希函数转换索引,然后存储在一个数组中. 哈希表是时间和空间之间的平衡,体现空间换时间的算法思想(联想到预加载,缓存等,有时候多存储,预处理缓存一些东西,带来时间复杂度的改 ...
- 吴裕雄--天生自然HADOOP操作实验学习笔记:hbase简介
实验目的 了解hbase的概念 通过安装hbase了解hbase的原理 了解hbase与hadoop的关系 复习hadoop和zookeeper的运行 实验原理 hbase是bigtable的开源山寨 ...
- 使用anaconda 3安装tensorflow 1.15.0 (win10环境)
0.写在前面 之前其实安装过一次tensorflow,但是由于电脑中毒,重装了系统,把所有的环境全部删除了.之前在博客里转发了一篇别人在win10安装tensorflow的教程,但是版本比较旧了, ...
- TCP-IP ---三次握手和四次挥手来啦
- c++ char* 与LPCTSTR相互转化
] = "wo shi ni baba"; , , ch, -, NULL, ); wchar_t *wide = new wchar_t[num]; MultiByteToWid ...
- IdentityServer4专题之一:OAuth2.0介绍
1.OAuth 2.0授权方式介绍: OAuth 2.0 的标准是 RFC 6749 文件.该文件先解释了 OAuth 是什么: OAuth 引入了一个授权层,用来分离两种不同的角色:客户端和资源所有 ...