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, .. ...
随机推荐
- i.MX RT600之DMIC外设介绍及应用
恩智浦的i.MX RT600是跨界处理器产品,同样也是i.MX RTxxx系列的开山之作.不同于i.MX RT1xxx系列单片机,i.MX RT600 采用了双核架构,将新一代Cortex-M33内核 ...
- Linux进程管理(二)
目录 Linux进程管理(二) 参考 vmstat命令 top命令 Linux进程管理(二)
- Ubuntu 解决TXT文本乱码问题
只要依次在终端输入这两行指令即可: gsettings set org.gnome.gedit.preferences.encodings auto-detected "['GB18030' ...
- markdown基本语法教程
标题 一级标题 二级标题 三级标题 以此类推,总共六级标题,建议在警号后面加一个空格,这是最标准的markdown语法 列表 在markdown下: 列表的显示只需要在文字前加上-.+或*即可变为无序 ...
- Android中利用正则表达式验证手机号是否合法
利用正则表达式来验证手机号是否合法,现在我们简单使用一下,这个在实际的应用中很实用. 例:在输入框中输入手机号,判断是否合法, MainActivity中: package com.example.m ...
- Tomcat能启动,无法访问方法,
好像没有扫描到controller 好像配置文件都没有加载成功 项目启动后,目录下多出一个ssmtest.xml文件 D:\Program Files\JDK-tomcat\apache-tomcat ...
- bzoj 4747: [Usaco2016 Dec]Counting Haybales
23333,在扒了一天题解之后发现我竟然还能秒题,虽然这是个pj的sb题... (排个序,然后upper_bound和lower_bound一用就行了(是不是有O(1)的查询方法啊??貌似要离散啊,一 ...
- wyh的dp入门刷题笔记
0: 靠前感觉之前dp抄题解都是抄的题解,自己从没有真正理解过dp.wyh下了很大决心从头学dp,于是便有了这篇文章. 1.背包 前四讲01背包&多重背包&完全背包(混合背包) :樱花 ...
- 5.7 Nginx 其他模块
- BUU re1
先shift+F12定位到关键句 然后crtl+X查看函数的交叉调用 定位到该函数处 F5查看伪代码 插入一段re1 re2题中都遇到的技巧: 很多时候出现的数字是asc码,热键R可以把数字转化成字母 ...