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, .. ...
随机推荐
- Django 学习之用户认证组件auth与User对象
一.auth模块 from django.contrib import auth django.contrib.auth中提供了许多方法,这里主要介绍其中的三个. 1 .authenticate() ...
- Python - Mro
参考 https://stackoverflow.com/questions/2010692/what-does-mro-do http://python.jobbole.com/85685/ 问题: ...
- Go语言 | 哪些大公司在用go语言?
Go大概09年面世以来,已经8年了,也算是8年抗战.在这8年中,已经有很多公司开始使用Go语言开发自己的服务,甚至完全转向Go开发,也诞生了很多基于Go的服务和应用,比如Dokcer.k8s等,现在我 ...
- VUE 使用axios请求第三方接口数据跨域问题解决
VUE是基于node.js,所以解决跨域问题,设置一下反向代理即可. 我这里要调用的第三方接口地址为 http://v.juhe.cn/toutiao/index?type=top&key=1 ...
- 41和为S的连续正数序列+注意循环条件记一下这题特殊解法
题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久,他 ...
- SPringBootJPA的使用快速开发
一文搞懂如何在 Spring Boot 中正确使用 JPA JPA 这部分内容上手很容易,但是涉及到的东西还是挺多的,网上大部分关于 JPA 的资料都不是特别齐全,大部分用的版本也是比较落后的.另外, ...
- [转载]JDK自带的实用工具——native2ascii.exe
做Java开发的时候,常常会出现一些乱码,或者无法正确识别或读取的文件,原因是编码方式的不一致.native2ascii是sun java sdk提供的一个工具.用来将别的文本类文件(比如*.txt, ...
- html5和html4.0.1的<html>标记的区别
html5新增了一个 manifest属性,定义了缓存信息. html5中废弃了xmlns属性,这个属性在html转换成xhtml是非常有用的,但是html5中并不需要,因此不用写. 但是如果非要写那 ...
- 搜索await page.waitForSelector(allResultsSelector);
/** * Copyright 2017 Google Inc. All rights reserved. * * Licensed under the Apache License, Version ...
- Python基础week2
本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 ...