anaconda及jupyter notebook的使用之numpy模块的用法(2)
今日内容概要
numpy模块结束
ndarray创建
numpy内置方法
索引与切片(花式索引、布尔索引)
常用函数
统计方法
随机数
numpy的内置方法
import numpy as np
1.
# 1.ndarray的创建
np.array([1,2,3,4,5,6,7],ndmin=3)
array([[[1,2,3,4,5,6,7]])
2.
# 2.python中的range
# for i in range(10):
# print(i)
np.arange(1,7) # arange比python中的range更好用
array([1,2,3,4,5,6])
np.arange(1.0,8.0)
array([1.,2.,3.,4.,5.,6.,7.])
int是否包含最后一个数
# 3.linspace
np.linspace(1,10,num=20,retstep=True,endpoint=False) # retstep展示间隔数 endpoint是否包含最后一个数
(array([1. , 1.45, 1.9 , 2.35, 2.8 , 3.25, 3.7 , 4.15, 4.6 , 5.05, 5.5 ,
5.95, 6.4 , 6.85, 7.3 , 7.75, 8.2 , 8.65, 9.1 , 9.55]), 0.45)
# 4.zeros
np.zeros(10,dtype=int) # 默认都是float
res = np.zeros((3,5),dtype=int) # 默认都是二维
res
array([[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]])
# 5.ones 用法跟zeros一致
np.ones(10)
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
# 6.empty
np.empty((3,3)) # 默认也是二维
array([[0.00000000e+000, 0.00000000e+000, 0.00000000e+000],
[0.00000000e+000, 0.00000000e+000, 6.14617663e-321],
[8.34448532e-308, 1.69105613e-306, 2.56765117e-312]])
# 7.eye
np.eye(10,dtype=int)
array([[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1]])
ndarray跟标量运算
统一的规律都是
数组内的每一个元素都跟该元素(数字)做运算
数组与数组之间做运算
两个数组的大小得一致
运算的时候按照对应的位置计算
索引与切片
2 索引
# python中索引:从0开始的
# l = [111,222,333,444,555,666]
# l[1]
res = np.arrray([111,222,333,444,555,666,777])
res[2] # numpy中索引也是从0开始
333
res1 = np.array([[1,2,3,4],[5,6,7,8]])
res1
'''
在numpy索引的规律
0 1 2 3 列索引
行索引 [
0 [1, 2, 3, 4],
1 [5, 6, 7, 8]
]
'''
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
面是列索引
# 求上述二维数字里面的元素7
# res[1][2] # 写法1
res1[1,2] # 写法2 逗号前面是行索引,逗号后面是列索引
7
3 切片
'''
切片取值
l1 = [11,22,33,44,55,66,77,88]
l1[1:3]
numpy切片操作跟python中的切片操作一致
'''
res2 = np.array([11,22,33,44,55,66,77])
res2[1:6]
array([22, 33, 44, 55, 66])
res3 = np.array([[1,2,3,4],[5,6,7,8],[9,11,22,33]])
res3
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 11, 22, 33]])
# 获取上述ndarray里面的6,7,11,22
res3[1:3,1:3]
array([[ 6, 7],
[11, 22]])
4 运算
res * 3
res = np.array([[1,2,3],[4,5,6]])
res * 3
array([[ 3, 6, 9],
[12, 15, 18]])
res1 + res2
res1 = np.array([[11,22,33],[44,55,66]])
res2 = np.array([[1,2,3],[4,5,6]])
res1 + res2
array([[12, 24, 36],
[48, 60, 72]])
res1 * res2
array([[ 11, 44, 99],
[176, 275, 396]])
res3 = np.array([[1,2,3,4],[5,6,7,8]])
res1 + res3
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-56-b5f7cab23041> in <module>
1 res3 = np.array([[1,2,3,4],[5,6,7,8]])
----> 2 res1 + res3
ValueError: operands could not be broadcast together with shapes (2,3) (2,4)
reshap
res4 = np.array([111,222,333,444,555,666])
# 转换数组的维数 转的时候一定要注意元素的个数到底够不够 不够直接报错
res4.reshape(2,3)
array([[111, 222, 333],
[444, 555, 666]])
res4.reshape(3,3)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-60-274f26b3bc7a> in <module>
----> 1 res4.reshape(3,3)
ValueError: cannot reshape array of size 6 into shape (3,3)
布尔型索引
import random
li = [random.randint(1,10) for i in range(30)]
'''
python里面的列表生成式
new_list = []
for i in range(30):
new_list.append(random.randint(1,10)) # 往列表中添加30个1到10的随机数
'''
res = np.array(li)
res
array([ 3, 8, 7, 10, 6, 2, 7, 4, 1, 10, 5, 3, 6, 6, 2, 9, 6,
8, 1, 5, 7, 8, 2, 6, 6, 4, 5, 5, 9, 5])
# 求出数字中大于5的数
# res > 5 # 就是跟数组里面的每一个元素作比较 结果是布尔值索引
res[res>5] # 布尔值索引取值
# 布尔型索引:将同样大小的布尔数组传进索引,会返回一个有True对应位置的元素的数组
array([ 8, 7, 10, 6, 7, 10, 6, 6, 9, 6, 8, 7, 8, 6, 6, 9])
花式索引
res5 = np.array([1,2,3,4,5,6,7,8,9,10])
# 拿出 2 6 9 10
res5[[1,5,8,9]] # 花式索引
array([ 2, 6, 9, 10])
# 在python中如何获取元素的索引值
# l = [111,222,333,444,555,666]
# l.index(333)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-70-305672a1ca18> in <module>
2 # l = [111,222,333,444,555,666]
3 # l.index(333)
----> 4 res5.index(3)
AttributeError: 'numpy.ndarray' object has no attribute 'index'
通用函数
# 1.绝对值
np.abs([-1,-2,-3])
np.fabs([-11.,-22.,-33.])
array([11., 22., 33.])
# 2.平方根
np.sprt(2)
1.4142135623730951
# 3 平方
np.square(4)
16
# 4 e的次方
np.exp(1)
2.718281828459045
# 5 自然对数
np.log(4)
1.3862943611198906
# 6 ceil取整 取大
np.ceil(5.4)
6.0
# 7 floor取整 取小
np.floor(4.6)
4.0
# 8 四舍五入
np.rint(4.6)
5.0
# 9 将小数分割成整数和小数
np.modf(4.5)
(0.5, 4.0)
np.modf([3.4,5.6])
(array([0.4, 0.6]), array([3., 5.]))
# 10 isnan 判断当前数据是否缺失
np.isnan(56)
False
# 11 cos sin tan
np.cos(45)
0.5253219888177297
补充
1.自动提示
按TAB键
2.自动查看方法的使用方式
在方法名后面加?运行即可
3.代码的注释
ctrl + ?
anaconda及jupyter notebook的使用之numpy模块的用法(2)的更多相关文章
- 修改Anaconda中Jupyter Notebook默认工作路径
修改Anaconda中Jupyter Notebook默认工作路径 1.打开 Anaconda Prompt 2.输入命令 jupyter notebook --generate-config 这个命 ...
- anaconda及jupyter notebook的了解及使用方法(1)
今日内容 anaconda软件使用 jupyter notebook基本使用及快捷键 numpy anaconda软件使用 1.进入anaconda主页点击jupyter启动即可 呼起一个jupyte ...
- 关于anaconda中jupyter notebook错误
anaconda这个软件是真的坑,其中的jupyter notebook每次都会出错,不知道,为什么,可惜我的pycharm装tensorflow一直有错误,不然,真想卸了这个软件. 会莫名其妙闪退, ...
- 配置Anaconda的jupyter notebook默认主页
1. 在Anaconda Prompt里输:jupyter notebook --generate-config 然后找到这个文件:C:\Users\{YOUR NAME}\.jupyter\jupy ...
- anaconda和jupyter notebook使用方法
查看安装的conda版本 conda --version 如果没有安装anaconda,可以从以下链接下载anaconda或者miniconda,两者安装一个就可以了 miniconda大约50M h ...
- 怎么修改Anaconda 中 jupyter notebook 文件的保存位置
安装完 anaconda ,在jupyter notebook 中创建的文件的默认保存位置为C:\User\电脑名 修改保存位置 1.打开 anaconda prompt 2.输入 jupyter n ...
- 为anaconda的jupyter notebook设置初始化目录
在使用jupyter进行编程时,初始化目录可能不是自己想要的目录,那么下面讲解修改成自己想要的目录. 1) 在命令行中输入jupyter notebook --generate-config,会产生一 ...
- anaconda重装jupyter notebook后启动jupyter报错的问题
问题描述: 由于jupyter出现难以解决的问题,采用重新安装来解决问题,但是重装之后启动jupyter报错ImportError: libsodium.so.23: cannot open shar ...
- python、anaconda、jupyter notebook、pycharm、spyder
说明: 1.anaconda把任何东西都当做包来管理. 2.anaconda本省集成了python和conda.spyder.numpy等. 3.pip只用于python,conda可用于多种语言. ...
随机推荐
- kubernetes之部署dashboard 和heapster
部署dashboard之前,先确保traefik https方式部署成功,这样就可以通过 https 域名的方式访问dashboard,无需kube-proxy转发了.假设traefik-ingres ...
- 阿里巴巴发布最佳实践 | 阿里巴巴DevOps实践指南
编者按:本文源自阿里云云效团队出品的<阿里巴巴DevOps实践指南>,扫描上方二维码或前往:https://developer.aliyun.com/topic/devops,下载完整版电 ...
- 利用JavaWeb实现课程信息添加
整体架构 :HTML+JAVABEAN+SERVLET 一.首先先简单介绍一下所需要的组件 原文地址 https://www.cnblogs.com/zll20153246/p/ ...
- [HNOI2009]双递增序列
不难发现本题贪心是不好做的,可以考虑 \(dp\). 首先的一个想法就是令 \(dp_{i, j, k, l}\) 表示当前选到第 \(i\) 个位置,当前第一个序列选了 \(j\) 个数,当前第一个 ...
- tomcat访问所有的资源,都是用Servlet来实现的
感谢大佬:https://www.zhihu.com/question/57400909 tomcat访问所有的资源,都是用Servlet来实现的. 在Tomcat看来,资源分3种 静态资源,如css ...
- TCP连接的状态转换图深度剖析
转载请注明来源:https://www.cnblogs.com/hookjc/ 在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接,如图1所示. (1)第一次握手:建立连接时 ...
- iOS中利用CoreTelephony获取用户当前网络状态(判断2G,3G,4G) by徐文棋
前言: 在项目开发当中,往往需要利用网络.而用户的网络环境也需要我们开发者去注意,根据不同的网络状态作相应的优化,以提升用户体验. 但通常我们只会判断用户是在WIFI还是移动数据,而实际上,移动数据也 ...
- android TextView属性详解
RelativeLayout用到的一些重要的属性: 第一类:属性值为true或false android:layout_centerHrizontal 水平居中 android:l ...
- 人工智能与智能系统3-> 机器人学3 | 移动机器人平台
机器人学的基本工具已经了解完毕,现在开始了解移动机器人,这部分包括机器人平台.导航.定位. 所谓机器人平台就是指机器人的物理结构及其驱动方式.本文将学习两种典型移动机器人平台(四旋翼和轮式车)的运动与 ...
- Docker 中的问题:”invalid reference format: repository name must be lowercase”
在导入镜像的时候出现问题:invalid reference format: repository name must be lowercase 问题解决:镜像命名不能出现大写字母,将大写改为小写即可 ...