numpy---one
import numpy as np#创建数组(给array函数传递Python序列对象) a = np.array([1,2,3,4,5]) b = np.array((1,2,3,4,5,6)) c = np.array([ [1,2,3,4,5], [6,7,8,9,10] ])#数组的大小用shape属性获得 print(type(a), a.shape, a, '\n') print(type(b), b.shape, b,'\n') print(type(c),c.shape, c,'\n')#改变数组的shape属性,改变自身元素排列 c.shape = 2, 5 print(c.shape, c)c.shape = 10, -1 print(c.shape, c)#通过reshape改变数组排序,赋值给新数组,但是共享同一块内存 d = b.reshape((2,3)) print(d.shape, d) b[1]=100 print(b,d) 输出:
<class 'numpy.ndarray'> (5,) [1 2 3 4 5]
<class 'numpy.ndarray'> (6,) [1 2 3 4 5 6]
<class 'numpy.ndarray'> (2, 5) [[ 1 2 3 4 5]
[ 6 7 8 9 10]]
(2, 5) [[ 1 2 3 4 5]
[ 6 7 8 9 10]]
(10, 1) [[ 1]
[ 2]
[ 3]
[ 4]
[ 5]
[ 6]
[ 7]
[ 8]
[ 9]
[10]]
(2, 3) [[1 2 3]
[4 5 6]]
[ 1 100 3 4 5 6] [[ 1 100 3]
[ 4 5 6]]
import numpy as np#创建数组(通过numpy函数) a = np.arange(0, 1, 0.1) #不包括终值 b = np.linspace(0, 1, 10) #包括终值,等差10个数 c = np.logspace(0, 2, 10) #从1到100,等比10个数 s = "abcdef" d = np.fromstring(s, dtype=np.int8) e = np.fromstring(s, dtype=np.int16) print(a,'\n',b,'\n',c,'\n',d,'\n',e,'\n')
输出:
[ 0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
[ 0. 0.11111111 0.22222222 0.33333333 0.44444444 0.55555556
0.66666667 0.77777778 0.88888889 1. ]
[ 1. 1.66810054 2.7825594 4.64158883 7.74263683
12.91549665 21.5443469 35.93813664 59.94842503 100. ]
[ 97 98 99 100 101 102]
[25185 25699 26213]
import numpy as np#创建10个元素的一维数组 def func(i):return i%4+1print ( np.fromfunction(func,(10,)) )
输出:
[ 1. 2. 3. 4. 1. 2. 3. 4. 1. 2.]
import numpy as npdef func(i,j):return (i + 1) * (j + 1)print(np.fromfunction(func, (9,9)))
输出:
[[ 1. 2. 3. 4. 5. 6. 7. 8. 9.]
[ 2. 4. 6. 8. 10. 12. 14. 16. 18.]
[ 3. 6. 9. 12. 15. 18. 21. 24. 27.]
[ 4. 8. 12. 16. 20. 24. 28. 32. 36.]
[ 5. 10. 15. 20. 25. 30. 35. 40. 45.]
[ 6. 12. 18. 24. 30. 36. 42. 48. 54.]
[ 7. 14. 21. 28. 35. 42. 49. 56. 63.]
[ 8. 16. 24. 32. 40. 48. 56. 64. 72.]
[ 9. 18. 27. 36. 45. 54. 63. 72. 81.]]
ndim:维度,shape:(行数,列数),size:元素总个数 dtype:指定数据类型
# -*- coding: utf-8 -*- import numpy as npmatrix = np.array([[1,2,3], [4,5,6]]) #矩阵 print("dim; ",matrix.ndim) print("shape: ",matrix.shape) print("size: ",matrix.size)list1 = np.array([1,2,3,4],dtype=np.int32) print("list1 dtype: ",list1.dtype)list2 = np.array([1,2,3,4]) print("list2 dtype: ",list2.dtype)list3 = np.array([1,2,3,4],dtype=np.float) print("list3 dtype: ",list3.dtype)list4 = np.array([1,2,3,4],dtype=np.float32) print("list4 dtype: ",list4.dtype)list5 = np.ones((3,4),dtype=np.int) print("list5: ",list5)list6 = np.empty((3,4)) print("list6: ",list6)list7 = np.arange(5,15).reshape((2,5)) print("list7: ",list7)list8 = np.linspace(1,11,10) print("list8: ",list8)
输出;
dim; 2
shape: (2, 3)
size: 6
list1 dtype: int32
list2 dtype: int32
list3 dtype: float64
list4 dtype: float32
list5: [[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
list6: [[ 6.95332630e-310 1.69118108e-306 2.04722549e-306 1.29061142e-306]
[ 2.22522597e-306 1.33511969e-306 1.29061753e-306 1.11261027e-306]
[ 9.34609790e-307 1.11260619e-306 1.42410974e-306 8.34449381e-308]]
list7: [[ 5 6 7 8 9]
[10 11 12 13 14]]
list8: [ 1. 2.11111111 3.22222222 4.33333333 5.44444444
6.55555556 7.66666667 8.77777778 9.88888889 11. ]
# -*- coding: utf-8 -*- import numpy as npa = np.arange(5) b = np.array([1,2,3,4,5])print("a: ",a) print("b: ",b) addc = a + b print("add: ", addc)minusc = a -b print("minus: ",minusc)timec = a * b print("times: ",timec)squc = a**2 print("square: ",squc)sinc = 10 * np.sin(a) print("sin: ",sinc)print("compare: ",a<3)matrix1 = np.array([[1,2,3,4],[5,6,7,8]]) matrix2 = np.arange(8).reshape((4,2)) print("matrix *: ",np.dot(matrix1,matrix2)) print("matrix *",matrix1.dot(matrix2))suiji = np.random.random((2,4)) print("suiji: ",suiji) print("max: ",np.max(suiji)) print("min: ",np.min(suiji)) print("sum: ",np.sum(suiji)) print("col: ",np.min(suiji,axis=0)) print("row: ",np.max(suiji,axis=1))
a: [0 1 2 3 4]
b: [1 2 3 4 5]
add: [1 3 5 7 9]
minus: [-1 -1 -1 -1 -1]
times: [ 0 2 6 12 20]
square: [ 0 1 4 9 16]
sin: [ 0. 8.41470985 9.09297427 1.41120008 -7.56802495]
compare: [ True True True False False]
matrix *: [[ 40 50]
[ 88 114]]
matrix * [[ 40 50]
[ 88 114]]
suiji: [[ 0.79302826 0.02704441 0.19401082 0.02216562]
[ 0.66149996 0.77353779 0.66565688 0.53205038]]
max: 0.793028259974
min: 0.0221656169264
sum: 3.66899411306
col: [ 0.66149996 0.02704441 0.19401082 0.02216562]
row: [ 0.79302826 0.77353779]
转载于:https://www.cnblogs.com/crazybird123/p/7471956.html
numpy---one相关推荐
- OpenCV 笔记(07)— Mat 对象输出格式设置(Python 格式、CSV 格式、NumPy 格式、C 语言格式)
首先是下面代码中将要使用的 r 矩阵的定义.需要注意,我们可以通过用 randu 函数产生的随机值来填充矩阵, 需要给定一个上限和下限来确保随机值在期望的范围内. Mat r = Mat(2, 3, ...
- NumPy — 创建全零、全1、空、arange 数组,array 对象类型,astype 转换数据类型,数组和标量以及数组之间的运算,NumPy 数组共享内存
NumPy 简介 一个用 python 实现的科学计算包.包括: 1.一个强大的 N 维数组对象 Array : 2.比较成熟的(广播)函数库: 3.用于整合 C/C++ 和 Fortran 代码的工 ...
- Python+OpenCV 图像处理系列(8)—— Numpy 对象及图像对象创建与赋值
1. Numpy 相关知识 1.1 Ndarray 对象 在了解 OpenCV 的图像对象之前我们先对 NumPy 的基础知识做一回顾,方便我们后续更进一步理解图像对象的一系列操作. In [2]: ...
- dataframe,python,numpy 问题索引2
20220330 print(frame3.isnull().any()) 找出存在空值的列 print("========================") print(fra ...
- dataframe,python,numpy 问题索引1
# 找出只有赌场数据的账户 gp=data.groupby(['查询账号','场景标签'],as_index=True) tj=gp.size().reset_index()按查询账号和场景标签分组并 ...
- 数组的拼接合并 numpy
Python中numpy数组的合并有很多方法,如 np.append() np.concatenate() np.stack() np.hstack() np.vstack() np.dstack() ...
- view(*args)改变张量的大小和形状_pytorch reshape numpy
20201227 这个方法是在不改变数据内容的情况下,改变一个数组的格式,参数及返回值,官网介绍: a:数组–需要处理的数据 newshape:新的格式–整数或整数数组,如(2,3)表示2行3列,新的 ...
- 用NumPy genfromtxt导入数据
用NumPy genfromtxt导入数据 NumPy provides several functions to create arrays from tabular data. We focus ...
- 【CV】Numpy|Python中矩阵和数组乘法及向量相关问题
在numpy中 一维数组不是向量,但可以通过reshape转换成行向量或者列向量 Numpy库学习--向量表示_o_Eagle_o-CSDN博客 一维数组左乘矩阵时,把数组看做行向量 一维数组右乘矩阵 ...
- Python:numpy生成正态分布的平均数
首先复习一下正态分布(百度百科):https://baike.baidu.com/item/%E6%AD%A3%E6%80%81%E5%88%86%E5%B8%83/829892?fr=aladdin ...
最新文章
- 判断CSS与JS是否加载完毕的方法
- Java几种常见的编码方式
- 浅谈L0,L1,L2范数及其应用
- 为bootstrap的tab增加请求操作
- ZooKeeper available but no active master location found
- 区块链技术指南 第一章 区块链和比特币的初体验
- 对接闪送_中国快递业加速出海,圆通上线“全球闪送”,与顺丰、申通抢市场...
- centos恢复图形界面_CentOS 7 启动与切换图形界面
- C语言单片机串转并,74ls164与单片机的串并转换(串转并\串进并出)
- 域名转移应该怎么做?域名转入是什么意思?手把手教你将阿里云备案域名转入到腾讯云
- SPSS联合Excel进行logistic回归亚组交互效应(交互作用)的可视化分析
- Java:使用POI实现word的docx文件的模板功能
- 有关网络安全方面的书籍(凯文·米特尼克作品)
- vue通过v-if隐藏的元素在网页加载时闪现
- 自定义控件其实很简单 二
- 智能工厂:怎样服装ERP软件的价格距离这么大?
- 5篇关于特征嵌入的研究论文推荐
- 北大青鸟深圳嘉华学校分享Hibernate查询
- uni-app+优博讯DT50U进行二次开发
- 为什么Eureka是AP,zookeeper是CP
热门文章
- LOL手游超燃测试好评如潮,没拿到资格不用急,期待一手不删档
- 第九届蓝桥杯决赛JavaC组真题——详细答案对照(完整版)
- Git利用命令行提交代码完整步骤
- 分界符 EOF 不使用反斜杠转义的技巧
- dbms_xplan.display_cursor 获取执行过的sql的执行计划
- 一个释放临时表空间的实例
- ADO.NET连接数据库
- 同步屏障CyclicBarrier
- 2017-7-18-每日博客-关于Linux下的鲜为人知的10条命令.doc
- Redis 3.0.501 学习使用(1)