Python扩展包
Python扩展包
1、NumPy
NumPy提供了多种python本身不支持的多种集合,有list、ndarray和ufunc。
list
更加灵活的数组,支持多维,数据可不同型,存储数量远大于array。array只支持同型数据,空间有限。
ndarray
多维数组类,方便操纵多维数组,数据必须同型,操纵高效。
ufunc
对数组进行高效处理的函数。主要用于高维数组的访问,底层使用c/c++实现。
1.1 构造数组
import numpy as np
# 一维数组
arr = np.array([2,0,1,5,8,3], dtype=np.float64)
# 二维数组
arr = np.array([[1, 2, 3, 4], [4, 5, 6, 7], [7, 8, 9, 10]], dtype=np.float64)
1.2 max函数
# 提取数组最大值
arr.max()
1.3 min
arr.min()
1.4 sort
arr.sort()
1.5 数组形状
# 属性
arr.shape
# 内置函数
type(a)
1.6 切片
切片本质上就是去数组的子集。
# 全部元素
arr[:]
# 开始至导数第二个元素
arr[:-2]
# 最后的两个元素
arr[-2:]
# 第一个元素
arr[:1]
# 二维数组切片
arr[:][:]
#
arr[1][:-2]
1.7 数学函数
Numpy封装了很多数学函数。
正弦函数
np.sin(np.pi / 6)
zeros函数
# 初始化所有元素为0。
np.zeros((3,4) ,dtype=np.float64),
ones
# 初始化所元素为1
np.ones((3,4) ,dtype=np.float64)
1.8 数组与标量值计算
同型数组相乘就是对应元素相乘
arr1 = np.array([[1,2,3],[4,5,6]])
arr2 = arr1 * arr1
# 结果
[
[1,4,9] ,
[16,25,36]
]
同型数组相减对应元素相减
arr1 = np.array([[1,2,3],[4,5,6]])
arr2 = arr1 - arr1
# 结果
[
[0,0,0] ,
[0,0,0]
]
数组的倒数对应每个元素的倒数
arr1 = np.array([[1,2,3],[4,5,6]])
arr2 = 1 / arr1
# 结果
[
[1,0.5,0.33333] ,
[0.25,0.2,0.166666]
]
1.9 布尔索引
# 名称数组
names = np.array(['Bob','Joe','Bob','Joe'])
names == 'Bob'
# 结果
array([ True, False, True, False])
1.10 随机数组
# 随机数组
arr = np.random.randn(4,4)
# 结果
array([[-0.63024369, -0.46411696, 0.39215462, -1.59666929],
[-0.66560699, -0.39085017, 1.44804687, -0.32239144],
[-0.23065156, -2.05660209, -0.59777198, 0.36496691],
[-2.21541391, 1.61258062, -0.43754217, -0.58834426]])
# 同布尔型数组组合
arr[names=='Bob']
array([[-0.63024369, -0.46411696, 0.39215462, -1.59666929],
[-0.23065156, -2.05660209, -0.59777198, 0.36496691]])
1.11 空数组
空数组赋值,是对每个元素都进行赋值。
# 空数组,里面没有任何内容
arr = np.empty((5,4))
for i in range(5):
arr[i] = i
# 结果
array([[0., 0., 0., 0.],
[1., 1., 1., 1.],
[2., 2., 2., 2.],
[3., 3., 3., 3.],
[4., 4., 4., 4.]])
1.12 通用函数
np.arange
arr = np.arange(10)
# 结果
array([1,2,3,4,5,6,7,8,9])
np.sqrt
np.sqrt(array([1,4,9]))
# 结果
array([1,2,3])
np.random.randn(8)
arr = np.random.randn(8)
# 结果
array([ 0.67089833, 0.29382861, 0.83511179, -0.44452724, 0.12621945,
-0.7167797 , -0.42452128, -0.63488661])
np.maximum(a,b)
x = np.array([1,2,3])
y = np.array([5,0,5])
np.maximum(x,y
# 结果
array([5, 2, 5])
一元函数
函数 解释 abs 绝对值 exp 指数 ceil ceil floor floor cos、sin、cosh、sinh 普通型和双曲型三角函数 arccos 反余弦 arcsin 反正弦 二元函数
函数 解释 add multiply divide、floor_divide
2、pandas
pandas 是panel data的之意。它是Python最强大的数据分析和探索工具,因金融数据分析工具而开发,支持类似SQL的数据增删改查功能,支持时间序列分析,灵活处理缺失数据。pandas有两种数据类型,Series和DataFrame。
2.1 Series
Series是一列数据,相当于表格中的一列。
s1 = Series([1,2,3,4])
# key是索引列
s1 = Series({'a':100 , 'b':200 , 'c':300 ,'d':400})
# 所有值
s1.values
# 所有索引
s1.index
# 通过索引访问
s1['b']
2.2 DataFrame
数据框是二维表格,类似于关系型数据库中的表。
# 构造数据
data = {"name":["yahoo","google","facebook"], "marks":[200,400,800], "price":[9, 3, 7]}
# 数据全部列
df1 = DataFrame(data)
# 使用指定的列
pds=pd.DataFrame(data,columns=['name','price','marks'])
数据框结构如下:
| name | marks | price |
|---|---|---|
| yahoo | 200 | 9 |
| 400 | 3 | |
| 800 | 7 |
Python扩展包的更多相关文章
- LFD,非官方的Windows二进制文件的Python扩展包
LFD,非官方的Windows二进制文件的Python扩展包 LFD,非官方版本.32和64位.Windows.二进制文件.科学开源.Python扩展包 克里斯托夫·戈尔克(by Christoph ...
- TensorFlow常用Python扩展包
TensorFlow常用Python扩展包 TensorFlow 能够实现大部分神经网络的功能.但是,这还是不够的.对于预处理任务.序列化甚至绘图任务,还需要更多的 Python 包. 下面列出了一些 ...
- 机器学习常用Python扩展包
在Ubuntu下安装Python模块通常有3种方法:1)使用apt-get:2)使用pip命令(推荐);3)easy_instal 可安装方法参考:[转]linux和windows下安装python集 ...
- windows下的python扩展包下载地址
比如lxml什么的 Unofficial Windows Binaries for Python Extension Packages pip install xxx.whl
- Windows二进制文件的Python扩展包
http://www.lfd.uci.edu/~gohlke/pythonlibs/ https://pypi.python.org/simple/
- 用于Python扩展包的非官方Windows二进制文件
https://www.lfd.uci.edu/~gohlke/pythonlibs/ Index by date: peewee aiohttp indexed_gzip pygit2 pymatg ...
- python扩展包的升级
检查更新:pip list --outdated 更新: pip install --upgrade xxxx
- linux和windows下安装python拓展包及requirement.txt安装类库
python拓展包安装 直接安装拓展包默认路径: Unix(Linux)默认路径:/usr/local/lib/pythonX.Y/site-packagesWindows默认路径:C:\Python ...
- 使用pip安装扩展包
pip可以对python扩展包进行查找.下载.安装.卸载等
随机推荐
- json几种读取方式,ArrayList循环读取【转】
在之前写过提取json数据格式的文章,这次对jmeter读取json数据格式进行整理. 举例一个接口的response 格式如下: { "data" : { "devic ...
- vscode 注册表
Windows Registry Editor Version 5.00 ; Open files [HKEY_CLASSES_ROOT\*\shell\Open with VS Code] @=&q ...
- 75th LeetCode Weekly Contest All Paths From Source to Target
Given a directed, acyclic graph of N nodes. Find all possible paths from node 0 to node N-1, and re ...
- http转https的各种应用
http://www.lccee.com/content-57.html https://www.gworg.com/ssl/127.html apach: LoadModule socache_sh ...
- Troubleshooting ORA-201 and ORA-202 Error
---- 3. When lowering the value of COMPATIBLE: You cannot start the database with lower compatibilit ...
- 微服务(Micro Service Architecture)浅析
最近一段时间,微服务的概念很火,可能是跟Docker技术的快速发展和壮大有一定的关系,同时借助于Uber.Netflix.Groupon等公司的实践.宣传和推广,使得MSA渐渐地成为企业或者架构师讨论 ...
- DedeCMS织梦自定义图片字段调用出现{dede:img ..}
做站过程中碰到这样一个问题,找到解决办法收藏分享:为什么在首页用自定义列表调用出来的图片字段不是正确的图片地址,而是类似于: {dede:img text='' width='270' height= ...
- 类模版的static成员
类模版中声明static成员 template <class T> class Foo { public: static size_t count() { ++ctr; cout < ...
- HDU 5407——CRB and Candies——————【逆元+是素数次方的数+公式】
CRB and Candies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- element中遇到的表格问题总结
1.列表表头的颜色自定义 <el-table :data="pageData" style="width: 100%;" height="500 ...