数据分析-numpy模块

1、numpy模块

1.简介

- NumPy(Numerical Python) 是 Python 语言中做科学计算的基础库。重在于数值计算,也是大部分Python科学计算库的基础,多用于在大型、多维数组上执行的数值运算。

- numpy当成是一个一维或者多维的数组

2. numpy的创建

- 使用 np.array()创建

- 使用plt创建
- 使用np的routines函数创建

- 使用array()创建一个一维数组
# 创建一维数组
import numpy as np
arr = np.array([1,2,3,4,5])#创建一维数组
arr
# 输出结果
#arr([1,2,3,4,5])
# 使用array()创建一个多维数组
import numpy as np
np.array([[1,2,3],[4,5,6]])#创建二维数组
#输出结果:array([[1,2,3],[4,5,6]])

3.数组和列表的区别

- 列表中可以存储不同类型的数据
- 数组中存储的数据元素类型必须一致
	- str>float>int数据类型的优先级

示例

import numpy as np 
np.array([[1,2,3],[4,'five',6]])
# 输出结果
# array([['1','2','3'],['4','five','6']],dtype='<U11')

4. 将外部的一张图片读取加载到numpy数组中

1.加载到numpy数组中

# 将外部的一张图片读取加载到numpy数组中,然后尝试改变数组元素的数值查看对原始图片的影响
import matplotlib.pyplot as plt
#图片数据的读取
img_arr = plt.imread('./1.jpg')
img_arr

# 输出结果
array([[[129,  86,  31],#前两个参数代表像素 后一个参数代表 颜色
        [129,  86,  31],
        [130,  87,  32],
        ...,
        [111,  77,  39],
        [102,  75,  45],
        [255, 239, 212]]],dtype=uint8)
#将一个三维的numpy数组显示成一张图片
pit.imshow(img_arr-100)

2.图片反转

#将一张图片反转
plt.imshow(img_arr[::-1,::-1,::-1])

5. zeros、ones、linespace、arange、random

zeros、ones

import numpy as np 
np.zeros(shape=(3,4))

# 输出结果
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])


np.ones(shape=(3,4))

# 输出结果
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])

linspace、arange random 一维等差数列

import numpy as np 
np.linspace(0,100,num=20)

# 输出结果
array([  0.        ,   5.26315789,  10.52631579,  15.78947368,
        21.05263158,  26.31578947,  31.57894737,  36.84210526,
        42.10526316,  47.36842105,  52.63157895,  57.89473684,
        63.15789474,  68.42105263,  73.68421053,  78.94736842,
        84.21052632,  89.47368421,  94.73684211, 100.        ])
import numpy as np
np.arange(0,100,2)

#输出结果
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])
import numpy as np
np.random.randint(0,80,size=(5,8))

# 输出结果
array([[29,  8, 73,  0, 40, 36, 16, 11],
       [54, 62, 33, 72, 78, 49, 51, 54],
       [77, 69, 13, 25, 13, 30, 30, 12],
       [65, 31, 57, 36, 27, 18, 77, 22],
       [23, 11, 28, 74,  9, 15, 18, 71]])


np.randodm.random(size=(3,4))

# 输出结果
array([[0.0768555 , 0.85304299, 0.43998746, 0.12195415],
       [0.73173462, 0.13878247, 0.76688005, 0.83198977],
       [0.30977806, 0.59758229, 0.87239246, 0.98302087]])

6.随机因子 seed

#随机因子(系统时间):无时无刻都在变化的值
#随机因子固定下来,随机性就固定
import numpy as np
np.random.seed(10)
np.random.randint(0,100,size=(2,3))
# 输出结果
array([[ 9, 15, 64],
       [28, 89, 93]])

7. numpy的常用属性

# shape、ndim、size、dtype
import numpy as np
arr = np.random.randint(0,100,size=(5,6))
arr

# 输出结果
array([[88, 11, 17, 46,  7, 75],
       [28, 33, 84, 96, 88, 44],
       [ 5,  4, 71, 88, 88, 50],
       [54, 34, 15, 77, 88, 15],
       [ 6, 85, 22, 11, 12, 92]])


arr.shape#形状(重点)
# 输出结果(5,6)

arr.ndim#维度数
# 输出结果 2

arr.size#数组的长度
#输出结果 30

arr.dtype#数组元素的类型(重点)
# 输出结果:dtype('int32')

type(arr)
# 输出结果 numpy.ndarray

8. numpy的数据类型(数组元素的类型)

  • array(dtype=?):可以设定数据类型
  • arr,dtype='?':可以修改数据类型

# 通过dtype修改数据的数据类型
arr.dtype = 'int16'
arr.dtype
#输出结果:dtype('int16')

9. numpy的索引和切片操作

简介

意义:可以让我们取出numpy数组中任意指定的局部数据
索引操作和列表同理

索引操作

import numpy as np
arr = np.random.randint(0,100,size=(5,6))

# 取一行
arr[1]

# 取多行
arr[[1,2,3]]

切片操作

# 切出前两列数据
# 切出前两行数据
# 切出前两行的前两列的数据
# 数组数据翻转
# 练习:将一张图片上下左右进行翻转操作
# 练习:将图片进行指定区域的裁剪

import numpy as np
arr = np.random.randint(0,100,size=(5,6))

# 切出前两行数据
arr[0:2]

# 切除前两列数据
arr[:,0:2]

# 切出前两行的前两列的数据
arr[0:2,0:2]

# 列反转
arr[:,::-1]

#行反转
arr[::-1]

# 元素反转
arr[::-1,::-1]

10. 变形reshape,级联操作

1. reshape

import numpy as np
arr = np.array([[1,2,3],[4,5,6]])
arr

# 输出结果
array([[1, 2, 3],
       [4, 5, 6]])

# 多维变一维
arr_1 = arr.reshape((6,))
arr_1
#输出结果:array([1, 2, 3, 4, 5, 6])


# 一维变多维
arr_1.reshape((6,1))
arr_1.reshape((3,-1)) #-1表示自动计算行或者列数
# 输出结果 array([[1,2],[3,4],[5,6]])

2. 级联操作

就是将多个numpy数组进行横向或者纵向的拼接

axis轴向的理解

import numpy as np
arr = np.array([[1,2,3],[4,5,6]])
n_arr = arr
display(arr,n_arr)

# 输出结果
array([[ 9, 53, 94, 60, 44, 86],
       [29, 69, 14, 17, 11, 24],
       [ 8, 30, 39,  5, 19, 67],
       [55, 33,  6, 16, 57, 48],
       [19, 69, 12, 52, 27,  0]])
array([[ 9, 53, 94, 60, 44, 86],
       [29, 69, 14, 17, 11, 24],
       [ 8, 30, 39,  5, 19, 67],
       [55, 33,  6, 16, 57, 48],
       [19, 69, 12, 52, 27,  0]])

3. 匹配级联

进行级联的多个数组的形状是完全一样

import numpy as np
arr = np.array([[1,2,3],[4,5,6]])
n_arr = arr

np.concatenate((arr,n_arr),axis=0)
#输出结果
array([[1, 2, 3],
       [4, 5, 6],
       [1, 2, 3],
       [4, 5, 6]])

#不匹配级联
	#维度一致,但是行列个数不一直
		#横向级联:保证行数一致
		#纵向级联:保证列数一致
posted @ 2019-12-11 21:47  __张达达  Views(272)  Comments(0Edit  收藏  举报