【Python数据分析】数据分析之numpy基础

实验环境:建立在Python3的基础之上

numpy提供了一种数据类型,提供了数据分析的运算基础,安装方式

pip install numpy

导入numpy到python项目

import numpy as np

本文以案例的方式展示numpy的基本语法,没有介绍语法的细枝末节,笔者认为通过查阅案例就能掌握基本用法。

numpy数组的基本概念

numpy默认所有元素具有相同的数据类型,如果类型不一致,会对其进行优化。如果元素类型不同,将统一成一种类型,优先级:str>float>int

import numpy as np``   ``t_list = [1, 1.2, "hello"]``print(t_list)``   ``t_list = np.array([1, 1.2, "hello"])``print(t_list)``   ``t_list = np.array([1, 1.2])``print(t_list)

定义数组的时候,可以声明数据类型

t_list = np.array([1,2,3])``print(t_list)``   ``t_list = np.array([1,2,3], dtype=np.float32)``print(t_list)

numpy构造数组

1、np.ones(shape, dtype)

shape=(m,n)  m行n列``shape=(m)    m个元素的一维数组``shape=(m,)   m个元素的一维数组``shape=(m,1)  m行1列的二维数组  [[1],[2],[3]]``shape=(1,m)  1列m行的二维数组  [[1,2,3]]
t_list = np.ones(shape=(5,4), dtype=np.int32)``print(t_list)

2、np.zeros(shape, dtype)

t_list = np.zeros(shape=(5,3), dtype=np.int32)``print(t_list)

3、np.full(shape, fill_value, dtype)

t_list = np.full(shape=(2,3,4), fill_value=10, dtype=np.int32)``print(t_list)

4、np.eye(N,M,k,dtype)

# 单位矩阵``t_list = np.eye(N=5, dtype=np.float32)``print(t_list)``   ``# 控制行列的矩阵``t_list = np.eye(N=5, M=4, dtype=np.int32)``print(t_list)``   ``# 1向左偏移``t_list = np.eye(N=5, k=-1)``print(t_list)

5、np.linspace(start, stop, num, endpoint=True, retstep=False, dtype)

# 共11个数``t_list = np.linspace(0, 10, 10)``print(t_list)``# 共10个数``t_list = np.linspace(0, 10, 10, endpoint=False)``print(t_list)

6、np.arange(start, stop, step, dtype)

t_list = np.arange(1,10,2)``print(t_list)

7、np.random.randint(low, high=None, size=None, dtype)

# 随机数``t_list = np.random.randint(1, 100, size=(5,4))``print(t_list)

8、np.random.random(size)

# 0到1之间的随机数``t_list = np.random.random(size=(5,4))``print(t_list)

9、np.random.permutation()

# 随机索引``t_list = np.random.permutation(10)``print(t_list)

10、属性

t_list = np.full(shape=(2,3,4), fill_value=10, dtype=np.int32)``print(t_list)``# 维度``print(t_list.ndim)``# 形状``print(t_list.shape)``# 大小``print(t_list.size)``# 元素类型``print(t_list.dtype)

数组的索引和切片

1、索引

t_list = np.array([1,2,3,4,5])``# 以下标的方式访问``print(t_list[0])``# 以列表索引的方式访问``print(t_list[[0,1,2,0,1,3]])``# 以布尔类型访问,得到数组中True的值,但布尔列表的长度需要与数组长度相同``print(t_list[[True,False,True,False,False]])``# 数组可以做运算``print(t_list > 3)``print(t_list[t_list > 3])``t_list = np.array([[1,20,3],[2,30,4],[3,40,5]])``print(t_list[0][1])``# 下标可以放在一起``print(t_list[0,1])``# 高维数组``t_list = np.random.randint(1, 10, size=(3,4,5), dtype=np.int32)``print(t_list)``print(t_list[1])``print(t_list[1,1])``print(t_list[1,1,1])

2、切片

t_list = np.random.randint(1,100,size=(10), dtype=np.int32)``print(t_list)``# 切片``print(t_list[2:5])``t_list = np.random.randint(1,100,size=(5,6), dtype=np.int32)``print(t_list)``# 行切片``print(t_list[1:3])``# 列切片``print(t_list[:,1:3])``t_list = np.random.randint(1,100,size=(3,6,5), dtype=np.int32)``print(t_list)``print(t_list[:,:,1:3])

3、变形

t_list = np.random.randint(1,100,size=(20), dtype=np.int32)``# 一维数组变形为二维数组,变形需要注意,前后两个数组的元素个数相同``print(t_list.reshape(4,5))

4、连接

t_list = np.random.randint(1,100,size=(4,4))``t_list2 = np.random.randint(1,100,size=(4,4))``# 横向连接,要求两个数组的横列大小相同``t_list = np.concatenate((t_list,t_list2), axis=1)``# 纵向连接,要求两个数组的横列大小相同``t_list = np.concatenate((t_list,t_list2), axis=0)

t_list = np.random.randint(1,100,size=(4,4))``t_list2 = np.random.randint(1,100,size=(4,4))``np.hstack((t_list,t_list2))``np.vstack((t_list,t_list2))

5、切分

t_list = np.random.randint(1,100,size=(4,8))``# 横向切分,等份切分``part1, part2 = np.split(t_list, indices_or_sections=2)``print(part1)``print(part2)``# 纵向切分``part1, part2 = np.split(t_list, indices_or_sections=2, axis=1)``print(part1)``print(part2)``t_list = np.random.randint(1,100,size=(5,7))``part1, part2, part3 = np.split(t_list, indices_or_sections=[2,3])``print(part1)``print(part2)``print(part3)``part1, part2, part3 = np.split(t_list, indices_or_sections=[2,3],axis=1)``print(part1)``print(part2)``print(part3)

part1, part2, part3 = np.vsplit(t_list, indices_or_sections=[2,3])``print(part1)``print(part2)``print(part3)``part1, part2, part3 = np.hsplit(t_list, indices_or_sections=[2,3])``print(part1)``print(part2)``print(part3)

6、复制

ct_list = t_list.copy()``ct_list[1,2] = 1000``print(t_list)``print(ct_list)

聚合操作

1、求和

t_list = np.random.randint(1,100,size=(4,8))``# 求和``print(t_list.sum())``# 求均值``print(t_list.mean())``# 求最值``print(t_list.max())``print(t_list.min())``# 最值索引``print(t_list.argmax())``print(t_list.argmin())``# 标准方差``print(t_list.std())``# 方差``print(t_list.var())``# 中位数``print(np.median(t_list))

2、布尔运算

t_list = np.array([True, False, True, True])``# 只要存在一个True,返回True``print(t_list.any())``# 全部为Ture,返回True``print(t_list.all())

3、矩阵

t_list = np.array([[1,2,3],[2,3,4]])``t_list2 = np.array([[1,2],[2,3],[3,4]])``print(np.dot(t_list, t_list2))

以上是numpy的基本操作,numpy提供了操作数组的运算基础,复杂业务处理,还需要Pandas的加入。

---------------------------END---------------------------

题外话

在这里插入图片描述

感兴趣的小伙伴,赠送全套Python学习资料,包含面试题、简历资料等具体看下方。

👉CSDN大礼包🎁:全网最全《Python学习资料》免费赠送🆓!(安全链接,放心点击)

一、Python所有方向的学习路线

Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照下面的知识点去找对应的学习资源,保证自己学得较为全面。

img
img

二、Python必备开发工具

工具都帮大家整理好了,安装就可直接上手!img

三、最新Python学习笔记

当我学到一定基础,有自己的理解能力的时候,会去阅读一些前辈整理的书籍或者手写的笔记资料,这些笔记详细记载了他们对一些技术点的理解,这些理解是比较独到,可以学到不一样的思路。

img

四、Python视频合集

观看全面零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。

img

五、实战案例

纸上得来终觉浅,要学会跟着视频一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

img

六、面试宝典

在这里插入图片描述

在这里插入图片描述

简历模板在这里插入图片描述

👉CSDN大礼包🎁:全网最全《Python学习资料》免费赠送🆓!(安全链接,放心点击)

若有侵权,请联系删除

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

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

相关文章

汽车制造行业,配电柜如何实施监控?

工业领域的生产过程依赖于高效、稳定的电力供应,而配电柜作为电力分配和控制的关键组件,其监控显得尤为重要。 配电柜监控通过实时监测、数据收集和远程控制,为工业企业提供了一种有效管理电能的手段,从而确保生产的连续性、安全性…

微信小程序echart导出图片

echarts版本5.1.0 用到的echarts组件是uni插件市场的echart组件 <div style"overflow: hidden;"><dCanvas class"uni-ec-canvass" id"uni-ec-canvas" ref"canvas" canvas-id"mychart-gauge" :ec"ec"&g…

WebSocket--技术文档--架构体系--《WebSocket实现原理以及关键组件》

WebSocket产生背景 简单的说&#xff0c;WebSocket协议之前&#xff0c;双工通信是通过多个http链接来实现&#xff0c;这导致了效率低下。WebSocket解决了这个问题。下面是标准RFC6455中的产生背景概述。 长久以来, 创建实现客户端和用户端之间双工通讯的web app都会造成HTT…

leetcode - 360周赛

一&#xff0c;2833. 距离原点最远的点 这道题的意思是&#xff0c;遇到 "L" 向左走&#xff0c;遇到 "R" 向右走&#xff0c;遇到 "_" 左右都可以走&#xff0c;那么要想找到距离原点最远的点&#xff0c;就是在找 | "L" "R&qu…

Java代码审计15之Apache log4j2漏洞

文章目录 1、log4j简介2、复现2.1、高版本测试2.2、测试代码2.3、补充之dns探测2.3.1、rmi、ldap也可以dnslog探测 2.3.2、dnslog外带信息 3、漏洞原理3.1、漏洞的危害大的背景3.2、具体的代码调试 4、靶场测试4.1、dns探测4.2、工具下载与使用4.3、测试4.4、手工可以测出&…

LeetCode--HOT100题(44)

目录 题目描述&#xff1a;230. 二叉搜索树中第K小的元素&#xff08;中等&#xff09;题目接口解题思路代码 PS: 题目描述&#xff1a;230. 二叉搜索树中第K小的元素&#xff08;中等&#xff09; 给定一个二叉搜索树的根节点 root &#xff0c;和一个整数 k &#xff0c;请你…

git的常用命令

初始化git&#xff0c;以及如何提交代码 1、配置用户信息 git config --global user.name zhangsan # 设置用户签名 git config --global user.email zhangsanqq.com # 设置用户邮箱&#xff08;不会验证&#xff0c;可以不存在&#xff09;1.1、查看是否已经添加用户配置 在…

软件测试Day6|接口测试

学习流程 接口测试流程 需求分析和评审–接口文档分析–编写测试用例–测试用例设计及评审–测试脚本构建–执行测试用例–缺陷管理和回归–测试报告和总结计网基础&#xff08;URL、请求、响应&#xff09; 接口文档解析 拿到一个项目接口之后&#xff0c;先测试业务接口还是…

英码深元“三位一体”AI场景化解决方案,助力多地化工园区快速实现智慧化转型!

我国是世界公认的化工大国&#xff0c;同时也是崛起中的化工强国。近年来多起重大爆炸事故暴露出我国化工园区安全问题突出&#xff0c;特别是在安全风险管控数字化转型、智能化升级方面存在明显短板和不足&#xff0c;尤其突出的痛点&#xff1a;化工园区的日常管理方式较为粗…

快速上手GIT命令,现学也能登堂入室

系列文章目录 手把手教你安装Git&#xff0c;萌新迈向专业的必备一步 GIT命令只会抄却不理解&#xff1f;看完原理才能事半功倍&#xff01; 快速上手GIT命令&#xff0c;现学也能登堂入室 系列文章目录一、GIT HELP1. 命令文档2. 简要说明 二、配置1. 配置列表2. 增删改查3. …

机器学习——手写数字识别

0、&#xff1a;前言 这篇文章能够帮助你从数据到模型的整个过程实现不过至于安装第三方库等基础问题&#xff0c;本文不涉及&#xff0c;因为确实不难&#xff0c;搜一搜一大把本此实验运行环境为jupyter&#xff0c;当然通过pycharm也是可行的 1、数据&#xff1a; 手写数字…

自动化运维工具Ansible之playbooks剧本

自动化运维工具Ansible之playbooks剧本 一、playbooks1.playbooks简述2.playbooks剧本格式3.playbooks组成部分 二、实例1.编写脚本2.运行playbook3.定义、引用变量4.指定远程主机sudo切换用户5.when条件判断6.迭代7.Templates 模块8.tags 模块9.Roles 模块 三、编写应用模块1.…