numpy1

1、NumPy包含的内容

  1、ndarrray,高效的多维数组,提供了基于数组的便捷算术操作以及灵活的广播功能;

  2、对所有数组对象进行快速的矩阵计算,而无需编写循环;

  3、提供对硬盘中的数据的读写工具,并对内存映射文件进行操作;

  4、可实现线性变换、随机数生成以及傅里叶变换功能;

  特点:NumPy在内部将数据存储在连续的内存块上,这与其他的Python内建序列是不同的,这使得NumPy数组对象的计算速度比其他同内容同操作的对象快(快10—100倍)。

 

2、每个ndarray对象具备的属性

  1、shape属性用来描述数组的维数;

  2、dtype属性描述数组的数据类型,它包含了ndarray需要为某一种类型数据所申明的内存块信息,是NumPy能够与其他系统数据灵活交互的原因;

1 import numpy as np
2 data = np.random.randn(2,3)
3 data.shape
4 Out[14]: 
5 》(2, 3)
6 data.dtype
7 Out[15]: 
8 》dtype('float64')

 

3、生成ndarray

  1、array函数,array函数接受任意的序列型对象(包括数组),生成一个包含传递数据的NumPy数组;接受嵌套序列时,自动转换成多维数组。

 1 data1 = [1,2,7.9,0,1]
 2 arr1 = np.array(data1)
 3 arr1
 4 Out[18]: 
 5 array([1. , 2. , 7.9, 0. , 1. ])
 6 data2 = [[1,2,3],[1,2,3]]
 7 np.array(data2)                 # 接受嵌套序列,自动转成二维数组
 8 Out[20]: 
 9 》array([[1, 2, 3],
10        [1, 2, 3]])

  2、zeros()、ones()、empty()、full()等函数,注意zeros()、ones()、empty()等函数接受的参数只有一个,如果要生成多维数组,则需要为shape传递一个元组指定维数;

  3、ones_like()根据所给的数据数组生成一个形状一摸一样的全1数组;zeros_like(),empty_like()、full_like()也类似;

  4、arange()生成python内建函数range的数组版,返回一个数组。

 

4、ndarray数据类型

  1、python数据数据类型众多,可以使用astype方法显示地转换数组地数据类型,注意:浮点型转整型时,小数点后部分将被消除。使用astype时,返回一个新的数组,而原数组不变。

1 arr = np.array([1, 2, 3, 4, 5])
2 print(arr.dtype)
3 float_arr = arr.astype(np.float64)
4 print(float_arr.dtype)
5 》》int32
6 》》float64

  2、如果一个数组里面地元素都是表达数字含义地字符串,也可以将字符串转换成数字;

1 numeric_strings = np.array(['1.25', '-9.6', '42'], dtype=np.string_)
2 numeric_strings
3 Out[30]: 
4 >>array([b'1.25', b'-9.6', b'42'], dtype='|S4')
5 numeric_strings.astype(np.float)
6 Out[31]: 
7 >>array([ 1.25, -9.6 , 42.  ])

 

5、NumPy数组算术

  数组之间可以直接进行批量操作而无需任何for循环;

  任何在两个等尺寸数组之间的算术操作都运用了逐元素操作的方式;

  带有标量的算术操作,会把计算参数传递给每一个元素;

  同尺寸数组之间的比较,会产生一个布尔类型的数组;

 

6、数组索引与切片

  1、数组的索引与列表的索引类似,但不相同。区别于python数组的内建列表,数组的切片是原数组的视图,即数组并不是被复制了,任何对于视图的修改都会反应到原数组上。

 1 arr = np.arange(10)
 2 arr[5:8] = 12
 3 arr_slice = arr[5:8]
 4 print(arr_slice)
 5 arr_slice[1] = 12345    # 对视图的修改后,原数组也会被改变
 6 arr
 7 [12 12 12]
 8 Out[38]: 
 9 array([    0,     1,     2,     3,     4,    12, 12345,    12,     8,
10            9])

  2、如果相对数组进行复制而不是得到一份视图,应该使用copy方法,eg:arr[5:8].copy()

  3、多维数组的索引方法,以二维数组为例,数组名[index][index]或者数组名[index,index]的方式。

 

7、布尔索引

  1、在索引数组时,可以传入布尔值数组,返回的是True对应的内容。

names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])
data = np.random.randn(7, 4)
names == 'Bob'
Out[41]: 
array([ True, False, False,  True, False, False, False])
data[names == 'Bob']
Out[42]: 
array([[-0.71491699,  0.40102409, -0.42140722, -1.50136196],[ 0.21920979, -0.13960939,  1.60586575,  0.0712131 ]])

  2、基于常识来设置布尔数组的值也是可行的。

 1 data = np.random.randn(7, 4)
 2 data
 3 Out[44]: 
 4 >>array([[-0.83434737, -0.67205305,  0.17626815, -0.60448911],
 5        [ 0.30011278, -0.98530314, -0.58816207,  2.40943742],
 6        [-0.94236761,  1.12991724, -0.4361433 ,  0.75806253],
 7        [-0.7228912 ,  1.1955933 , -0.75127874, -0.73905711],
 8        [-0.4128283 , -0.15726642,  0.86381129, -1.2467569 ],
 9        [-0.3290692 , -1.03838623,  0.68320058, -0.58237692],
10        [ 1.40461917,  0.55720836,  0.39822819,  0.64182056]])
11 data[data < 0] = 0
12 data
13 Out[46]: 
14 >>array([[0.        , 0.        , 0.17626815, 0.        ],
15        [0.30011278, 0.        , 0.        , 2.40943742],
16        [0.        , 1.12991724, 0.        , 0.75806253],
17        [0.        , 1.1955933 , 0.        , 0.        ],
18        [0.        , 0.        , 0.86381129, 0.        ],
19        [0.        , 0.        , 0.68320058, 0.        ],
20        [1.40461917, 0.55720836, 0.39822819, 0.64182056]])

8、reshape函数,格式 reshape(a, newshape, order='C')

Parameters
----------
a : array_like
Array to be reshaped.
newshape : int or tuple of ints
The new shape should be compatible with the original shape. If
an integer, then the result will be a 1-D array of that length.
One shape dimension can be -1. In this case, the value is
inferred from the length of the array and remaining dimensions.
order : {'C', 'F', 'A'}, optional
1 a = np.arange(6) 2 a 3 Out[49]: 4 array([0, 1, 2, 3, 4, 5]) 5 a.reshape(3,2) 6 Out[50]: 7 array([[0, 1], 8 [2, 3], 9 [4, 5]])

 

9、数组转置和换轴

  1、转置是一种特殊的数据重组形式,也可以返回底层数据的视图而不需要复制任何内容,数组拥有transpose方法,也拥有T属性。当数组是一维或者二维时,数组名.T的形式直接返回转置后的数组(原来的数组不会修改),

 1 arr = np.arange(15).reshape((3, 5))2 arr3 Out[54]: 4 array([[ 0,  1,  2,  3,  4],5        [ 5,  6,  7,  8,  9],6        [10, 11, 12, 13, 14]]) 7 arr.T 8 Out[55]: 9 array([[ 0, 5, 10], 10 [ 1, 6, 11], 11 [ 2, 7, 12], 12 [ 3, 8, 13], 13 [ 4, 9, 14]])

  2、对于更高维的数组,转置使用transpose和swapxes方法。

转载于:https://www.cnblogs.com/Chris-01/p/11430904.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391485.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

我如何预测10场英超联赛的确切结果

Is there a way to predict the outcome of any soccer game with 100% accuracy? The honest and simplest answer is…. no. Regardless of what your fantasy football friends say, there is absolutely no way to be 100% certain, but there is a proven, mathematical …

多迪技术总监揭秘:PHP为什么是世界上最好的语言?

PHP这么一个脚本语言&#xff0c;虽然他是web开发中&#xff0c;使用者最多的语言&#xff0c;最快最简单的语言&#xff0c;生态环境和社区积累最深厚的语言&#xff0c;作为最好的编程语言&#xff0c;多迪技术总监为你介绍&#xff1a;PHP为什么是世界上最好的语言&#xff…

aws数据库同步区别_了解如何通过使用AWS AppSync构建具有实时数据同步的应用程序

aws数据库同步区别AWS AppSync automatically updates the data in web and mobile applications in real time, and updates data for offline users as soon as they reconnect. AWS AppSync会自动实时更新Web和移动应用程序中的数据&#xff0c;并在离线用户重新连接后立即为…

leetcode 153. 寻找旋转排序数组中的最小值(二分查找)

已知一个长度为 n 的数组&#xff0c;预先按照升序排列&#xff0c;经由 1 到 n 次 旋转 后&#xff0c;得到输入数组。例如&#xff0c;原数组 nums [0,1,2,4,5,6,7] 在变化后可能得到&#xff1a; 若旋转 4 次&#xff0c;则可以得到 [4,5,6,7,0,1,2] 若旋转 4 次&#xff0…

test1

test1 转载于:https://www.cnblogs.com/Forever77/p/11434403.html

打印风车旋转效果

1 while True: 2 for i in["/","-","\\","|"]: 3 print "%s\r" %i, 转载于:https://www.cnblogs.com/feifei-cyj/p/7469333.html

深度学习数据自动编码器_如何学习数据科学编码

深度学习数据自动编码器意见 (Opinion) When I first wanted to learn programming, I coded along to a 4 hour long YouTube tutorial.刚开始学习编程时&#xff0c;我编写了长达4个小时的YouTube教程。 “Great,” I thought after finishing the course. “I know how to …

Angular 5.0 学习2:Angular 5.0 开发环境的搭建和新建第一个ng5项目

1.安装Node.js 在开始工作之前&#xff0c;我们必须设置好开发环境。如果你的机器上还没有Node.js和npm&#xff0c;请先安装它们。去Node.js的官网&#xff0c;https://nodejs.org/en/&#xff0c;点击下载按钮&#xff0c;下载最新版本&#xff0c;直接下一步下一步安装即可&…

leetcode 154. 寻找旋转排序数组中的最小值 II(二分查找)

已知一个长度为 n 的数组&#xff0c;预先按照升序排列&#xff0c;经由 1 到 n 次 旋转 后&#xff0c;得到输入数组。例如&#xff0c;原数组 nums [0,1,4,4,5,6,7] 在变化后可能得到&#xff1a; 若旋转 4 次&#xff0c;则可以得到 [4,5,6,7,0,1,4] 若旋转 7 次&#xff0…

robot:根据条件主动判定用例失败或者通过

场景&#xff1a; 当用例中的断言部分需要满足特定条件时才会执行&#xff0c;如果不满足条件时&#xff0c;可以主动判定该用例为passed状态&#xff0c;忽略下面的断言语句。 如上图场景&#xff0c;当每月1号时&#xff0c;表中才会生成上月数据&#xff0c;生成后数据不会再…

golang go语言_在7小时内学习快速简单的Go编程语言(Golang)

golang go语言The Go programming language (also called Golang) was developed by Google to improve programming productivity. It has seen explosive growth in usage in recent years. In this free course from Micheal Van Sickle, you will learn how to use Go step…

使用MUI框架,模拟手机端的下拉刷新,上拉加载操作。

套用mui官方文档的一句话&#xff1a;“开发者只需关心业务逻辑&#xff0c;实现加载更多数据即可”。真的是不错的框架。 想更多的了解这个框架&#xff1a;http://dev.dcloud.net.cn/mui/ 那么如何实现下拉刷新&#xff0c;上拉加载的功能呢&#xff1f; 首先需要一个容器&am…

图深度学习-第1部分

有关深层学习的FAU讲义 (FAU LECTURE NOTES ON DEEP LEARNING) These are the lecture notes for FAU’s YouTube Lecture “Deep Learning”. This is a full transcript of the lecture video & matching slides. We hope, you enjoy this as much as the videos. Of cou…

Git上传项目到github

2019独角兽企业重金招聘Python工程师标准>>> Git入门 个人理解git就是一个上传工具&#xff0c;同时兼具和svn一样的版本控制功能&#xff08;此解释纯属本人个人观点&#xff09; Github是什么 github就是一个分布式版本管理系统&#xff08;反正我就是这么认为的…

ionic4 打包ios_学习Ionic 4并开始创建iOS / Android应用

ionic4 打包iosLearn how to use Ionic 4 in this full course for beginners from Awais Mirza. Ionic Framework is the free, open source mobile UI toolkit for developing high-quality cross-platform apps for native iOS, Android, and the web—all from a single Ja…

robot:当用例失败时执行关键字(发送短信)

使用场景&#xff1a; 当用例失败时需要通知对应人员&#xff0c;则需要在Teardown中&#xff0c;使用关键字Run Keyword If Test Failed Send Message关键字为自定义关键字&#xff0c;${content}为短信内容&#xff0c;${msg_receiver}为短信接收者列表。 当然执行成功时需要…

leetcode 263. 丑数

给你一个整数 n &#xff0c;请你判断 n 是否为 丑数 。如果是&#xff0c;返回 true &#xff1b;否则&#xff0c;返回 false 。 丑数 就是只包含质因数 2、3 和/或 5 的正整数。 示例 1&#xff1a; 输入&#xff1a;n 6 输出&#xff1a;true 解释&#xff1a;6 2 3 …

NTP同步

RedHat Linux NTP实施步骤1、 查看本系统与NTP服务器的时间偏差 ntpdate -d 192.168.142.114 [rootzabbix-proxy ~]# ntpdate -d 192.168.142.114 24 Aug 17:26:45 ntpdate[3355]: ntpdate 4.2.6p51.2349-o Fri Apr 13 12:52:28 UTC 2018 (1) Looking for host 192.168.142.…

项目经济规模的估算方法_估算英国退欧的经济影响

项目经济规模的估算方法On June 23 2016, the United Kingdom narrowly voted in a country-wide referendum to leave the European Union (EU). Economists at the time warned of economic losses; the Bank of England produced estimates that that GDP could be as much …

Oracle宣布新的Java Champions

\看新闻很累&#xff1f;看技术新闻更累&#xff1f;试试下载InfoQ手机客户端&#xff0c;每天上下班路上听新闻&#xff0c;有趣还有料&#xff01;\\\Oracle宣布了2017年新接纳的Java Champion的综述。这次宣布了40位新的成员&#xff0c;包括InfoQ的贡献者Monica Beckwith。…