数据分析之Numpy、Matplotlib库
NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。
菜鸟教程:https://www.runoob.com/numpy/numpy-tutorial.html
Matplotlib 是 Python 的绘图库。 它可与 NumPy 一起使用,提供了一种有效的 MatLab 开源替代方案。它也可以和图形工具包一起使用,如 PyQt 和 wxPython。
菜鸟教程:https://www.runoob.com/numpy/numpy-matplotlib.html Numpy模块的简单使用
"""
菜鸟教程:https://www.runoob.com/numpy/numpy-tutorial.html
NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。 """
import numpy as np # pip install numpy # 1.numpy创建的数组类型为<class 'numpy.ndarray'> # (1)创建ndarray一维数组
ndarray_1 = np.array([1, 2, 4, 6])
print(ndarray_1)
# (2)创建ndarray二维数组(如果元素不对称,结果是一维列表数组)
ndarray_2 = np.array([[2, 4, 6], [1, 3, 5]])
print(ndarray_2,type(ndarray_2[1])) # ndarray_2=np.array([[2,4,6],[1,3,5,3]])
# print(ndarray_2,type(ndarray_2))#[list([2, 4, 6]) list([1, 3, 5, 3])] <class 'numpy.ndarray'>
# print(type(ndarray_2[1]))#<class 'list'> # (3)numpy默认ndarray的所有元素的类型是相同的
# 如果传进来的列表中包含不同的类型,则统一为同一类型,优先级:str>float>int
ndarray_3 = np.array([[2, 4, 6], [1, 3, 5]])
print(ndarray_3[1][2], type(ndarray_2[1][2])) # 5 <class 'numpy.int32'> ndarray_3 = np.array([[2, 4.8, 6], [1, 3, 5]])
print(ndarray_3[1][2], type(ndarray_2[1][2])) # 5.0 <class 'numpy.float64'> ndarray_3 = np.array([["a", 4, 6], [1, 3, 5]])
print(ndarray_3[1][2], type(ndarray_2[1][2])) # 5 <class 'numpy.str_'> #(4)numpy创建等差数组np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None) 等差数列
ndarray_4=np.linspace(0,10,num=20)
print(ndarray_4) #(5)numpy创建随机数组np.arange([start, ]stop, [step, ]dtype=None)
ndarray_5=np.arange(0,10,step=2)
print(ndarray_5)
ndarray_5=np.arange(0,10)
print(ndarray_5) #(6)numpy创建随机多维数组np.random.randint(low, high=None, size=None, dtype='l')
#如果指定了随机银子,就不在随机
# np.random.seed(10)#随机因子/时间种子
ndarray_6=np.random.randint(0,10,size=(3,4))#0到10之间3X4的数组
print(ndarray_6)
ndarray_6=np.random.randint(0,100,size=(3,3,3))#0到100之间3X3X3的三维数组
print(ndarray_6) #(7)numpy创建0-1之间的随机小数
ndarray_7=np.random.random()
print(ndarray_7,type(ndarray_7)) #2.ndarray数组的属性 #(8)ndarray数组的大小:ndarray.size
print(ndarray_1.size)
print(ndarray_2.size)
print(ndarray_3.size)
print(ndarray_4.size)
print(ndarray_5.size)
print(ndarray_6.size) #(9)ndarray数组的维度:ndarray.ndim
print(ndarray_1.ndim)
print(ndarray_2.ndim)
print(ndarray_3.ndim)
print(ndarray_4.ndim)
print(ndarray_5.ndim)
print(ndarray_6.ndim) #(10)ndarray数组的形状:ndarray.shape
print(ndarray_1.shape)
print(ndarray_2.shape)
print(ndarray_3.shape)
print(ndarray_4.shape)
print(ndarray_5.shape)
print(ndarray_6.shape) #(11)ndarray数组的元素类型:ndarray.dtype
print(ndarray_1.dtype)
print(ndarray_2.dtype)
print(ndarray_3.dtype)
print(ndarray_4.dtype)
print(ndarray_5.dtype)
print(ndarray_6.dtype) #(12)ndarray类型查看:type()
print(type(ndarray_1))
print(type(ndarray_2))
print(type(ndarray_3))
print(type(ndarray_4))
print(type(ndarray_5))
print(type(ndarray_6)) #3.ndarray数组的基本操作
# 对二维数组中括号第一个参数为行,第二个参数为列;
#对多维数组ndarray[,,,,],中括号中逗号分割的每个数字都指代一个维度 #(13)索引:①一维数组与列表一致;②多维数组与C语言一样
ndarray_13=np.random.randint(0,10,size=(3,4))
print(ndarray_13)#二维数组
print(ndarray_13[1])#获取二位数组第2行
print(ndarray_13[[1,2]])#获取二维数组第2和第3行
print(ndarray_13[1,3])#获取二维数组第2行第4个元素 #(14)切片
ndarray_14=np.random.randint(0,10,size=(5,4))
print(ndarray_14)
print(ndarray_14[:2])#获取二维数组的前两行
print(ndarray_14[:,:2])#获取二位数组的前两列
print(ndarray_14[:2,:2])#获取二位数组的前两行和前两列 print(ndarray_14[::-1])#对二维数组的行进行翻转
print(ndarray_14[:,::-1])#对二位数组的列进行翻转
print(ndarray_14[::-1,::-1])#对二维数组的行和列都进行翻转 #(15)变形:使用ndarray.reshape()函数,注意参数是一个tuple! ndarray_15=np.random.randint(0,10,size=10)
print(ndarray_15)
print(ndarray_15.reshape((2,5)))#一维数组变形成二位数组 ndarray_15=np.random.randint(0,10,size=(3,4))
print(ndarray_15)
print(ndarray_15.reshape((12,)))#二位数组变形成一位数组
print(ndarray_15.reshape((2,6)))#二位数组便形成其它二位数组 #(16)级联操作:对多个ndarray数组进行横向或者纵向的拼接np.concatenate((arr,arr),axis=0) #axis=0 列 1行
ndarray_16=np.random.randint(0,10,size=(3,4))
print(ndarray_16)
print(np.concatenate((ndarray_16,ndarray_16),axis=0))#第一个参数为多个ndarray数组,axis为0表示列级联
print(np.concatenate((ndarray_16,ndarray_16),axis=1))#第一个参数为多个ndarray数组,axis为1表示列级联 #4.ndarray数组的聚合操作
#(17)求和:ndarray.sum([axis=])
ndarray_17=np.random.randint(0,10,size=(3,4))
print(ndarray_17) print(ndarray_17.sum())#无参数默认全部求和
print(ndarray_17.sum(axis=1))#axis为1指定行求和
print(ndarray_17.sum(axis=0))#axis为0指定列求和 #(18)最大值、最小值:ndarray.max()/ndarray.min()
ndarray_18=np.random.randint(0,10,size=(3,4))
print(ndarray_18) print(ndarray_18.max())#默认无参数求数组最大值
print(ndarray_18.max(axis=1))#axis为1指定行求最大值
print(ndarray_18.max(axis=0))#axis为0指定列求最大值 print(ndarray_18.min())#默认无参数求数组最小值
print(ndarray_18.min(axis=1))#axis为1指定行求最小值
print(ndarray_18.min(axis=0))#axis为0指定列求最小值 #(19)平均值:ndarray.mean()
ndarray_19=np.random.randint(0,10,size=(3,4))
print(ndarray_19) print(ndarray_19.mean())#默认无参数求数组平均值
print(ndarray_19.mean(axis=1))#axis为1指定行求平均值
print(ndarray_19.mean(axis=0))#axis为1指定列求平均值 #(19)ndarray数组的其它聚合操作
"""
ndarray.prod()
ndarray.std() 标准差
ndarray.var()
ndarray.argmin()
ndarray.argmax()
ndarray.median()
ndarray.percentile()
ndarray.any() 一些
ndarray.all() 所有
ndarray.power() 幂运算
""" #5.ndarray数组排序:np.sort()与ndarray.sort()都可以,但有区别
ndarray_20=np.random.randint(0,10,size=(3,4))
print(ndarray_20)
print(np.sort(ndarray_20))#默认对行、列顺序排序
print(np.sort(ndarray_20,axis=1))#对行排序
print(np.sort(ndarray_20,axis=0))#对列排序
Numpy简单使用
Matplotlib模块的简单使用
"""
菜鸟教程:https://www.runoob.com/numpy/numpy-matplotlib.html
Matplotlib 是 Python 的绘图库。 它可与 NumPy 一起使用,提供了一种有效的 MatLab 开源替代方案。
它也可以和图形工具包一起使用,如 PyQt 和 wxPython。
"""
import numpy as np
import matplotlib.pyplot as plt
#读取显示图片
img_arr=plt.imread('1.png')#读取图片数组
plt.imshow(img_arr)#显示图片
plt.show()
print(img_arr.shape)
#可以直接使用numpy中的数组方法
plt.imshow(img_arr[::-1])#对行进行翻转,图片上下反转
plt.show() plt.imshow(img_arr[:,::-1])#对列进行翻转,图片左右反转
plt.show() plt.imshow(img_arr[::-1,::-1])#对行/列进行翻转,图片完全反转
plt.show() print(img_arr.shape)#三维数组
plt.imshow(img_arr[::-1,::-1,::-1])#对行/列及颜色进行翻转,图片及色彩完全反转
plt.show() #拼图np.concatenate((arr,arr),axis=0) #axis=0 列 1行
img_arr3=np.concatenate((img_arr,img_arr,img_arr),axis=1)#行拼3
img_arr9=np.concatenate((img_arr3,img_arr3,img_arr3),axis=0)#列拼3
plt.imshow(img_arr9)
plt.show() # 绘制图形
import numpy as np
x = np.arange(1, 11)
y = 2 * x + 5
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.plot(x, y)
plt.show()
Matplotlib简单使用
效果图:
数据分析之Numpy、Matplotlib库的更多相关文章
- 数据分析与展示——Matplotlib库入门
Matplotlib库入门 Matplotlib库介绍 Matliotlib库是Python优秀的数据可视化第三方库. Matliotlib库的效果见:http://matplotlib.org/ga ...
- numpy, matplotlib库学习笔记
Numpy库学习笔记: 1.array() 创建数组或者转化数组 例如,把列表转化为数组 >>>Np.array([1,2,3,4,5]) Array([1,2,3,4,5]) ...
- NumPy Matplotlib库
NumPy - Matplotlib Matplotlib 是 Python 的绘图库. 它可与 NumPy 一起使用,提供了一种有效的 MatLab 开源替代方案. 它也可以和图形工具包一起使用,如 ...
- 第二周 数据分析之展示 Matplotlib库入门
Matplotlib库介绍:优秀的数据可视化第三方库 使用:Matplotlib库由各种可视化类构成,内部结构复杂,受Matlab启发,matplotlib.pyplot是绘制各类可视化图形的命令子库 ...
- $python数据分析基础——初识matplotlib库
基本用法 import numpy as np import matplotlib.pyplot as plt # 年份 year = [1950,1970,1990,2010] # 全球总人口(单位 ...
- Python_科学计算平台__pypi体系的numpy、scipy、pandas、matplotlib库简介
1.numpy--基础,以矩阵为基础的数学计算模块,纯数学 存储和处理大型矩阵. 这个是很基础的扩展,其余的扩展都是以此为基础. 快速学习入口 https://docs.scipy.org/doc/n ...
- 在Ubuntu 14.04 64bit上安装numpy和matplotlib库
原文:http://blog.csdn.net/tao_627/article/details/44004541 按照这个成功安装! 机器学习是数据挖掘的一种实现形式,在学习<机器学习实战> ...
- 利用matplotlib库和numpy库画数学图形
首先,电脑要安装到matplotlib库和numpy库,这可以通过到命令符那里输入“pip install matplotlib ”,两个操作一样 其次,参照下列代码: import numpy as ...
- python数据分析基础——numpy和matplotlib
numpy库是python的一个著名的科学计算库,本文是一个quickstart. 引入:计算BMI BMI = 体重(kg)/身高(m)^2假如有如下几组体重和身高数据,让求每组数据的BMI值: w ...
- 利用Python进行数据分析——重要的Python库介绍
利用Python进行数据分析--重要的Python库介绍 一.NumPy 用于数组执行元素级计算及直接对数组执行数学运算 线性代数运算.傅里叶运算.随机数的生成 用于C/C++等代码的集成 二.pan ...
随机推荐
- cocos2d 导演,场景
导演(Director) Cocos2d-x 使用导演的概念,这个导演和电影制作过程中的导演一样!导演控制电影制作流程,指导团队完成各项任务.在使用 Cocos2d-x 开发游戏的过程中,你可以认为自 ...
- Spring5参考指南:依赖注入
文章目录 依赖注入 依赖注入的配置详解 depends-on lazy-init 自动装载 方法注入 依赖注入 依赖注入就是在Spring创建Bean的时候,去实例化该Bean构造函数所需的参数,或者 ...
- Gym 101194D Ice Cream Tower
被一道数位DP折磨得欲仙欲死之后,再做这道题真是如同吃了ice cream一样舒畅啊 #include<bits/stdc++.h> using namespace std; #defin ...
- Docker虚拟化管理:30分钟教你学会用Docker
关于Docker的官方介绍网上太多了我就不贴了,就实际体验来说Docker可以极大的简化环境搭建及服务部署的操作流程,大大降低部署的时间成本,解放你的双手. 本文不会深入讲解Docker底层架构及运行 ...
- Python封装应用程序的最佳项目结构是什么?
Python封装应用程序的最佳项目结构是什么? 转载来源于stackoverflow:https://stackoverflow.com/questions/193161/what-is-the-be ...
- windows服务程序的编写
服务编写https://blog.csdn.net/lanuage/article/details/77937407 #include <windows.h> #include <s ...
- CF思维联系--CodeForces - 218C E - Ice Skating (并查集)
题目地址:24道CF的DIv2 CD题有兴趣可以做一下. ACM思维题训练集合 Bajtek is learning to skate on ice. He's a beginner, so his ...
- Proteus传感器+气体浓度检测的报警方式控制仿真
Proteus传感器+气体浓度检测的报警方式控制仿真 目录 Proteus传感器+气体浓度检测的报警方式控制仿真 1 实验意义理解 2 主要实验器件 3 实验参考电路 4 实验中的问题思考 4.1 实 ...
- 【杂谈】Disruptor——RingBuffer问题整理(一)
纯CAS为啥比加锁要快? 同样是修改数据,一个采用加锁的方式保证原子性,一个采用CAS的方式保证原子性. 都是能够达到目的的,但是常用的锁(例如显式的Lock和隐式的synchonized),都会把获 ...
- Ubuntu系统make menuconfig的依赖包ncurses安装
Linux内核或者u-boot进行make menuconfig的时候,如果系统上没有安装ncurses,就会出现以下报错 *** Unable to find the ncurses librari ...