数据分析1

news/2024/10/11 2:20:07

 

数据分析:是把隐藏在一些看似杂乱无章的数据背后的信息提炼出来,总结出所研究对象的内在规律

数据分析三剑客:Numpy,Pandas,Matplotlib

NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。

一、创建ndarray

1. 使用np.array()创建

  • 一维数据创建
import numpy as np
np.array([1,2,3])

  • 二维数组创建 
np.array([[1,2,3],[4,5,6]]) #就是列表中放列表

np.array([[1,'two',3],[4,5,6]])

注意:

  • numpy默认ndarray的所有元素的类型是相同的
  • 如果传进来的列表中包含不同的类型,则统一为同一类型,优先级:str>float>int
  • 使用matplotlib.pyplot获取一个numpy数组,数据来源于一张图片
In [17]:
 
 
 
 
 
import matplotlib.pylab as plt
img_arr = plt.imread('./cat.jpg')
img_arr
 
 
Out[17]:
array([[[231, 186, 131],[232, 187, 132],[233, 188, 133],...,[100,  55,  52],[ 92,  48,  49],[ 85,  43,  44]],[[232, 187, 132],[232, 187, 132],[233, 188, 133],...,[100,  55,  52],[ 92,  48,  49],[ 84,  42,  43]],[[232, 187, 132],[233, 188, 133],[233, 188, 133],...,[ 99,  54,  51],[ 91,  47,  48],[ 83,  41,  42]],...,[[199, 119,  82],[199, 119,  82],[200, 120,  83],...,[189,  99,  64],[187,  97,  62],[187,  97,  62]],[[199, 119,  82],[199, 119,  82],[199, 119,  82],...,[188,  98,  64],[188,  95,  62],[188,  95,  62]],[[199, 119,  82],[199, 119,  82],[199, 119,  82],...,[188,  98,  64],[188,  95,  62],[188,  95,  62]]], dtype=uint8)
In [7]:
 
 
 
 
 
plt.imshow(img_arr)
 
 
Out[7]:
<matplotlib.image.AxesImage at 0x1ee32cd34e0>
 
In [8]:
 
 
 
 
 
plt.imshow(img_arr-100)
 
 
Out[8]:
<matplotlib.image.AxesImage at 0x1ee331c3518>
 
 
  • 操作该numpy数据,该操作会同步到图片中
 

2. 使用np的routines函数创建

 

5) np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None) 等差数列

In [13]:
 
 
 
 
 
np.linspace(0,100,num=6)
 
 
Out[13]:
array([  0.,  20.,  40.,  60.,  80., 100.])
 

6) np.arange([start, ]stop, [step, ]dtype=None)

In [10]:
 
 
 
 
 
np.arange(0,100,2)
 
 
Out[10]:
array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32,34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66,68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98])
 

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

In [15]:
 
 
 
 
 
#固定随机性
    #随机因子:系统的时间
np.random.seed(100)
arr = np.random.randint(0,100,size=(4,5))
arr
 
 
Out[15]:
array([[ 8, 24, 67, 87, 79],[48, 10, 94, 52, 98],[53, 66, 98, 14, 34],[24, 15, 60, 58, 16]])
 

9) np.random.random(size=None)

生成0到1的随机数,左闭右开 np.random.seed(3)

In [19]:
 
 
 
 
 
np.random.random(size=(4,5))
 
 
Out[19]:
array([[0.56229626, 0.00581719, 0.30742321, 0.95018431, 0.12665424],[0.07898787, 0.31135313, 0.63238359, 0.69935892, 0.64196495],[0.92002378, 0.29887635, 0.56874553, 0.17862432, 0.5325737 ],[0.64669147, 0.14206538, 0.58138896, 0.47918994, 0.38641911]])
 

二、ndarray的属性

 

4个必记参数: ndim:维度 shape:形状(各维度的长度) size:总长度

dtype:元素类型

In [24]:
 
 
 
 
 
arr.shape
arr.size
img_arr.size
arr.dtype
 
 
Out[24]:
(4, 5)
 

三、ndarray的基本操作

 

1. 索引

一维与列表完全一致 多维时同理

In [26]:
 
 
 
 
 
arr
 
 
Out[26]:
array([[ 8, 24, 67, 87, 79],[48, 10, 94, 52, 98],[53, 66, 98, 14, 34],[24, 15, 60, 58, 16]])
In [29]:
 
 
 
 
 
arr[1]
 
 
Out[29]:
array([48, 10, 94, 52, 98])
 

2. 切片

一维与列表完全一致 多维时同理

In [30]:
 
 
 
 
 
#获取二维数组前两行
arr[0:2]
 
 
Out[30]:
array([[ 8, 24, 67, 87, 79],[48, 10, 94, 52, 98]])
In [ ]:
 
 
 
 
 
#获取二维数组前两列
 
 
In [32]:
 
 
 
 
 
arr[:,0:2]  #arr[hang,lie]
 
 
Out[32]:
array([[ 8, 24],[48, 10],[53, 66],[24, 15]])
In [10]:
 
 
 
 
 
#获取二维数组前两行和前两列数据
 
 
In [33]:
 
 
 
 
 
arr[0:2,0:2]
 
 
Out[33]:
array([[ 8, 24],[48, 10]])
In [34]:
 
 
 
 
 
#将数组的行倒序
arr
 
 
Out[34]:
array([[ 8, 24, 67, 87, 79],[48, 10, 94, 52, 98],[53, 66, 98, 14, 34],[24, 15, 60, 58, 16]])
In [35]:
 
 
 
 
 
arr[::-1]
 
 
Out[35]:
array([[24, 15, 60, 58, 16],[53, 66, 98, 14, 34],[48, 10, 94, 52, 98],[ 8, 24, 67, 87, 79]])
In [36]:
 
 
 
 
 
#列倒序
arr[:,::-1]
 
 
Out[36]:
array([[79, 87, 67, 24,  8],[98, 52, 94, 10, 48],[34, 14, 98, 66, 53],[16, 58, 60, 15, 24]])
In [37]:
 
 
 
 
 
#全部倒序
arr[::-1,::-1]
 
 
Out[37]:
array([[16, 58, 60, 15, 24],[34, 14, 98, 66, 53],[98, 52, 94, 10, 48],[79, 87, 67, 24,  8]])
In [38]:
 
 
 
 
 
#将图片进行全倒置操作
plt.imshow(img_arr)
 
 
Out[38]:
<matplotlib.image.AxesImage at 0x1ee32fbb160>
 
In [40]:
 
 
 
 
 
plt.imshow(img_arr[:,::-1,:])
 
 
Out[40]:
<matplotlib.image.AxesImage at 0x1ee3304b588>
 
In [42]:
 
 
 
 
 
plt.imshow(img_arr[::-1,::-1,:])
 
 
Out[42]:
<matplotlib.image.AxesImage at 0x1ee32fa8898>
 
In [43]:
 
 
 
 
 
#裁剪
plt.imshow(img_arr)
 
 
Out[43]:
<matplotlib.image.AxesImage at 0x1ee34adbbe0>
 
In [44]:
 
 
 
 
 
plt.imshow(img_arr[100:340,170:565,:])
 
 
Out[44]:
<matplotlib.image.AxesImage at 0x1ee345a22b0>
 
 

3. 变形

使用arr.reshape()函数,注意参数是一个tuple!

 
  • 基本使用

    1.将一维数组变形成多维数组

In [35]:
 
 
 
 
 
arr_1.reshape((2,10))
arr_1.reshape((5,-1))
 
 
Out[35]:
array([[ 8, 24, 67, 87],[79, 48, 10, 94],[52, 98, 53, 66],[98, 14, 34, 24],[15, 60, 58, 16]])
 

2.将多维数组变形成一维数组

In [34]:
 
 
 
 
 
arr_1 = arr.reshape((20,))
arr_1
 
 
Out[34]:
array([ 8, 24, 67, 87, 79, 48, 10, 94, 52, 98, 53, 66, 98, 14, 34, 24, 15,60, 58, 16])
 

4. 级联

  • np.concatenate()
 

1.一维,二维,多维数组的级联,实际操作中级联多为二维数组

In [58]:
 
 
 
 
 
arr
 
 
Out[58]:
array([[ 8, 24, 67, 87, 79],[48, 10, 94, 52, 98],[53, 66, 98, 14, 34],[24, 15, 60, 58, 16]])
In [61]:
 
 
 
 
 
np.concatenate((arr,arr,arr),axis=1)
 
 
Out[61]:
array([[ 8, 24, 67, 87, 79,  8, 24, 67, 87, 79,  8, 24, 67, 87, 79],[48, 10, 94, 52, 98, 48, 10, 94, 52, 98, 48, 10, 94, 52, 98],[53, 66, 98, 14, 34, 53, 66, 98, 14, 34, 53, 66, 98, 14, 34],[24, 15, 60, 58, 16, 24, 15, 60, 58, 16, 24, 15, 60, 58, 16]])
 

2.合并两张照片

In [63]:
 
 
 
 
 
img_3 = np.concatenate((img_arr,img_arr,img_arr),axis=1)
img_9 = np.concatenate((img_3,img_3,img_3),axis=0)
plt.imshow(img_9)
 
 
Out[63]:
<matplotlib.image.AxesImage at 0x1ee3456aa90>
 
 

级联需要注意的点:

  • 级联的参数是列表:一定要加中括号或小括号
  • 维度必须相同
  • 形状相符:在维度保持一致的前提下,如果进行横向(axis=1)级联,必须保证进行级联的数组行数保持一致。如果进行纵向(axis=0)级联,必须保证进行级联的数组列数保持一致。
  • 可通过axis参数改变级联的方向
In [64]:
 
 
 
 
 
arr_1 = arr.reshape((5,4))
arr_1
 
 
Out[64]:
array([[ 8, 24, 67, 87],[79, 48, 10, 94],[52, 98, 53, 66],[98, 14, 34, 24],[15, 60, 58, 16]])
In [65]:
 
 
 
 
 
arr
 
 
Out[65]:
array([[ 8, 24, 67, 87, 79],[48, 10, 94, 52, 98],[53, 66, 98, 14, 34],[24, 15, 60, 58, 16]]) 
 
np.concatenate((arr,arr_1),axis=1)
 
 
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-38-78b5b92d6f99> in <module>
----> 1 np.concatenate((arr,arr_1),axis=1)<__array_function__ internals> in concatenate(*args, **kwargs)ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)
 

四、ndarray的聚合操作

 

1. 求和np.sum 

 
arr.sum(axis=1)


array([265, 302, 265, 173])
 
 
 
 
 
 
### 2. 最大最小值:np.max/ np.min
同理
 
 

3.平均值:np.mean()

In [ ]:
 
 

3. 其他聚合操作

Function Name    NaN-safe Version    Description
np.sum    np.nansum    Compute sum of elements
np.prod    np.nanprod    Compute product of elements
np.mean    np.nanmean    Compute mean of elements
np.std    np.nanstd    Compute standard deviation
np.var    np.nanvar    Compute variance
np.min    np.nanmin    Find minimum value
np.max    np.nanmax    Find maximum value
np.argmin    np.nanargmin    Find index of minimum value
np.argmax    np.nanargmax    Find index of maximum value
np.median    np.nanmedian    Compute median of elements
np.percentile    np.nanpercentile    Compute rank-based statistics of elements
np.any    N/A    Evaluate whether any elements are true
np.all    N/A    Evaluate whether all elements are true
np.power 幂运算
 

六、ndarray的排序

 

1. 快速排序

np.sort()与ndarray.sort()都可以,但有区别:

  • np.sort()不改变输入
  • ndarray.sort()本地处理,不占用空间,但改变输入
In [77]:
 
 
 
 
 
np.sort(arr,axis=0)
 
 
Out[77]:
array([[ 8, 10, 60, 14, 16],[24, 15, 67, 52, 34],[48, 24, 94, 58, 79],[53, 66, 98, 87, 98]])
In [74]:
 
 
 
 
 
arr
 
 
Out[74]:
array([[ 8, 24, 67, 87, 79],[48, 10, 94, 52, 98],[53, 66, 98, 14, 34],[24, 15, 60, 58, 16]])
In [75]:
 
 
 
 
 
arr.sort(axis=0)
 
 
In [76]:
 
 
 
 
 
arr
 
 
Out[76]:
array([[ 8, 10, 60, 14, 16],[24, 15, 67, 52, 34],[48, 24, 94, 58, 79],[53, 66, 98, 87, 98]])
In [ ]:
 

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

CSS调试 (1)

https://www.bilibili.com/video/BV1KM4y1G7EF/内容转自【浏览器调试工具精讲】Chrome Dev Tools精讲,前端必看! 橙色:外边距 蓝色:本体。 绿色:内边距。CTRL+ F 可以搜索 输入某一个string 或者 css,section#someid xpath: //section/p 编辑样式

多线程四-Lock锁及其原理分析

JUC是什么 可能有些不太关注底层代码,会不太理解juc是啥,比如之前的我,只知道是跟并发相关。juc其实就是并发包路径的缩写,java.util.concurrent.而Lock是其中锁的接口,有比如重入锁,读锁,写锁等一些具体实现。 这部分源码理解起来还是有些难度,暂时先理解其大概思路,…

Dijkstras algorithm All In One

Dijkstras algorithm All In One 迪杰斯特拉算法Dijkstras algorithm All In One迪杰斯特拉算法Dijkstra Dijkstras algorithm (/ˈdaɪkstrəz/ DYKE-strəz) is an algorithm for finding the shortest paths between nodes in a weighted graph, which may represent, for e…

Datawhale X 李宏毅苹果书AI夏令营 Task1打卡

3.1 局部极小值与鞍点 3.1.1 临界点及其分类参数对于损失函数的微分为零时,就无法进一步优化了,训练即停止了。所以我们把这些梯度为零的点统称为临界点 。 临界点可以分为两类:极值点 (局部极小值)和 鞍点 。 鞍点就是指那些梯度为零但不是局部极小值或者局部极大值的点,…

纪念第一次在 Github 上提 ISSUE 得到了老哥的回复

背景 第一次在 GitHub 上提 ISSUE,提问的内容就是我的上一篇博文 rustlings v6.0 运行时出现 “ You are trying to run Rustlings using the old method before version 6”,当时搞了好长时间都没思绪,然后就抱着试一试的心态在上面提了一个 ISSUE。 提问之后,又慢慢理了一…

sky-take-out chapter 5

微信登录 商品浏览 HttpClient (1)介绍 就是一个客户端编程工具包,更直白点就是我们可以在java程序中通过HttpClient这个工具包来构造http请求,并且可以来发送http请求;要使用httpclient就需要再java程序中导入maven坐标。 核心API:HttpClient 实际上是一个接口,使用它可…

可拖拽表单设计器都有哪些突出特点?

一起来了解低代码技术平台、可拖拽表单设计器的多个优势特点为了提高效率、降低开发成本,利用低代码技术平台的优势特点可以实现这一目标。究竟什么是低代码技术平台?都有哪些值得夸耀的特点和优势?今天,我们就带着这些问题,一起来了解低代码技术平台、可拖拽表单设计器的…

找到喜欢的事情,坚持去做!

村上春樹:临近三十岁的时候,我依旧一无所成, 我意识到自己并没有经营的才能,又不善于应酬,不喜欢社交,也不太喜欢认识新的东西。我酷爱音乐,但是在这个方面,也谈不上有什么成就。我似乎唯有的优点,就是喜欢的事,总能坚持去做。--- 自己的智力水平一般般,情商不高,也…