今日内容概要

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)的更多相关文章

  1. 修改Anaconda中Jupyter Notebook默认工作路径

    修改Anaconda中Jupyter Notebook默认工作路径 1.打开 Anaconda Prompt 2.输入命令 jupyter notebook --generate-config 这个命 ...

  2. anaconda及jupyter notebook的了解及使用方法(1)

    今日内容 anaconda软件使用 jupyter notebook基本使用及快捷键 numpy anaconda软件使用 1.进入anaconda主页点击jupyter启动即可 呼起一个jupyte ...

  3. 关于anaconda中jupyter notebook错误

    anaconda这个软件是真的坑,其中的jupyter notebook每次都会出错,不知道,为什么,可惜我的pycharm装tensorflow一直有错误,不然,真想卸了这个软件. 会莫名其妙闪退, ...

  4. 配置Anaconda的jupyter notebook默认主页

    1. 在Anaconda Prompt里输:jupyter notebook --generate-config 然后找到这个文件:C:\Users\{YOUR NAME}\.jupyter\jupy ...

  5. anaconda和jupyter notebook使用方法

    查看安装的conda版本 conda --version 如果没有安装anaconda,可以从以下链接下载anaconda或者miniconda,两者安装一个就可以了 miniconda大约50M h ...

  6. 怎么修改Anaconda 中 jupyter notebook 文件的保存位置

    安装完 anaconda ,在jupyter notebook 中创建的文件的默认保存位置为C:\User\电脑名 修改保存位置 1.打开 anaconda prompt 2.输入 jupyter n ...

  7. 为anaconda的jupyter notebook设置初始化目录

    在使用jupyter进行编程时,初始化目录可能不是自己想要的目录,那么下面讲解修改成自己想要的目录. 1) 在命令行中输入jupyter notebook --generate-config,会产生一 ...

  8. anaconda重装jupyter notebook后启动jupyter报错的问题

    问题描述: 由于jupyter出现难以解决的问题,采用重新安装来解决问题,但是重装之后启动jupyter报错ImportError: libsodium.so.23: cannot open shar ...

  9. python、anaconda、jupyter notebook、pycharm、spyder

    说明: 1.anaconda把任何东西都当做包来管理. 2.anaconda本省集成了python和conda.spyder.numpy等. 3.pip只用于python,conda可用于多种语言. ...

随机推荐

  1. 深入理解 React 的 Virtual DOM

    React在前端界一直很流行,而且学起来也不是很难,只需要学会JSX.理解State和Props,然后就可以愉快的玩耍了,但想要成为React的专家你还需要对React有一些更深入的理解,希望本文对你 ...

  2. 中文分词,自然语言处理器NLP。 新版本已上线,增加二级行业分类。

    一  cacl2新版本上线,在一级行业的基础上深度挖掘,新增了对应的二级分类. 可以查看一级行业[农林牧渔],下面对应的所有二级行业的词库.这里拿[林业]来观察效果. 具体分词的效果. 二    Gi ...

  3. Redis 源码简洁剖析 06 - quicklist 和 listpack

    quicklist 为什么要设计 quicklist 特点 数据结构 quicklistCreate quicklistDelIndex quicklistDelEntry quicklistInse ...

  4. 深入解析HashMap、HashTable (转)

    集合类之番外篇:深入解析HashMap.HashTable Java集合类是个非常重要的知识点,HashMap.HashTable.ConcurrentHashMap等算是集合类中的重点,可谓&quo ...

  5. NSArray 与字符串

    1.把数组元素链接成字符串 - (NSString )componentsJoinedByString:(NSString )separator; 这是NSArray的方法, 用separator作拼 ...

  6. 云端iclound使用-陈棚

    使用NSmetadataQuery查询文档,增加,删除,编辑 界面设计完成后效果如图: 程序清单:FKDiary.h @interface FKDiary : UIDocument @property ...

  7. spring5+Struts2+hibernate5

    一,加jar包 加入顺序,个人推荐,spring->struts2->hibernate spring的jar包:基本包共21个+用到的aspectj注解的包2个+日志包1个 ------ ...

  8. iOS 如何监听用户在手机设置里改变了系统的时间?

    如何监听用户未退出APP但通过Home键在手机设置里改变了系统的时间? 用户虽未退出APP,但是当它按Home键退到后台时 ,会调用该方法: - (void)applicationDidEnterBa ...

  9. postman中环境变量的设置方法、使用方法和实际中常见使用场景

    文中共介绍2种添加环境变量的方法.2种使用环境变量的方法,以及不同方法的适用范围. 文中给出了环境变量的两种常见使用场景:切换环境.动态参数关联(前一个请求的响应作为下一个请求的入参) 2种添加环境变 ...

  10. swoole错误“Uncaught Error: Class 'swoole_server' not found”的解决办法

    如果你在执行swoole对应文件时,报下面的错误, PHP Fatal error: Uncaught Error: Class 'swoole_server' not found in /mnt/w ...