数据分析必备知识(1),Numpy基础篇

python科学计算包 numpy是pandas、SciPy、sklearn等模块的必需模块,pandas、SciPy和sklearn模块都是在numpy模块的基础上封装的,由此可见numpy的作用是很大的。

numpy在后期数据科学方面的学习上会经常性用到的,特别是算法模型这一大块,要将数据转换成不同维度的数组以便符合算法模型的需求。

本文是总结性文章,适合学过、还没学过的读者朋友阅读。下面从numpy基础操作讲起,后续将持续更新.....

安装:

pip install numpy

导入模块

import numpy as np

1、创建数组

# 一维数组 np.array([1, 2, 3, 4])

array([1, 2, 3, 4])

# 二维数组,指定数据类型为浮点型 np.array([(1.5, 2, 3), (4, 5, 6)], dtype=float)

array([[ 1.5, 2. , 3. ],

[ 4. , 5. , 6. ]])

# 三维数组 np.array([[(1.5, 2, 3), (4, 5, 6), (3, 2, 1), (4, 5, 6)]], dtype = float)

array([[[ 1.5, 2. , 3. ],

[ 4. , 5. , 6. ],

[ 3. , 2. , 1. ],

[ 4. , 5. , 6. ]]])

2、初始化占位符

# 1、创建值为 0 的数组, np.zeros((rows, columns)) np.zeros((3, 4))

array([[ 0., 0., 0., 0.],

[ 0., 0., 0., 0.],

[ 0., 0., 0., 0.]])

# 2、创建值为 1 数组 # np.ones((多少个数组,rows,columns)) np.ones((2, 3, 4), dtype=np.int16)

array([[[1, 1, 1, 1],

[1, 1, 1, 1],

[1, 1, 1, 1]],

[[1, 1, 1, 1],

[1, 1, 1, 1],

[1, 1, 1, 1]]], dtype=int16)

# 3、创建均匀间隔的数组(步进值,类似于等差数列) # np.arange(start, end, 等差值) np.arange(10, 25, 2)

array([10, 12, 14, 16, 18, 20, 22, 24])

# 4、创建均匀间隔的数组(样本值) # np.linspace(允许的最小值,允许的最大值,生成样本值的个数) np.linspace(0, 2, 9)

array([ 0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. ])

# 5、创建常数数组 # np.full((rows, columns), 指定的常数值) np.full((3, 6), 5)

array([[5, 5, 5, 5, 5, 5],

[5, 5, 5, 5, 5, 5],

[5, 5, 5, 5, 5, 5]])

# 6、创建 3 x 3 单位矩阵 np.eye(3)

array([[ 1., 0., 0.],

[ 0., 1., 0.],

[ 0., 0., 1.]])

# 7、创建随机值的数组 # np.random.random((rows, columns)) np.random.random((2, 2))

array([[ 0.23610365, 0.55253555],

[ 0.6943377 , 0.05858759]])

3、输入输出

1、输出

# 语法:np.save(fileName, 需要保存的数组) np.save('../data/my_array', [b, a]) # 保存多个要用 () 或者 [] 括起来 np.savez('../data/my_array.npz', a, b) np.savetxt('../data/my_array.txt', b, delimiter=" ") np.savetxt('../data/my_array.csv', b, delimiter=",")

2、输入

np.load('../data/my_array.npy') np.loadtxt('../data/my_array.txt') np.genfromtxt('../data/my_array.csv', delimiter=' ')

4、数据类型

1、带符号的64位整数

np.int64

# 指定创建的"步进值"数组为整数 np.arange(1, 20, 4, dtype=np.int64)

array([ 1, 5, 9, 13, 17], dtype=int64)

2、标准双精度浮点数

np.float32

# 创建均匀的样本值 np.linspace(1, 6, 5, dtype=np.float32)

array([ 1. , 2.25, 3.5 , 4.75, 6. ], dtype=float32)

3、显示为128位浮点数的复数

np.complex

4、布尔值:True值和False值

np.bool

5、Python 对象

# 创建 4 x 4 矩阵,指定类型为 python 对象 np.object np.eye(4, dtype=np.object)

array([[1, 0, 0, 0],

[0, 1, 0, 0],

[0, 0, 1, 0],

[0, 0, 0, 1]], dtype=object)

6、固定长度字符串

# 创建常数数组,指定类型为字符串 np.string_ np.full((3, 4), 4, dtype=np.string_)

array([[b'4', b'4', b'4', b'4'],

[b'4', b'4', b'4', b'4'],

[b'4', b'4', b'4', b'4']],

dtype='|S1')

5、数组信息

test = np.array([[1, 15, 10, 2], [2, 5, 9, 1], [6, 2, 5, 4]]) test

array([[ 1, 15, 10, 2],

[ 2, 5, 9, 1],

[ 6, 2, 5, 4]])

1、数组形状,几行几列

# 查看整体形状,查看行的形状,查看列的形状 a, b, c= test.shape, test.shape[0], test.shape[1] a, b, c

((3, 4), 3, 4)

2、数组长度

len(test) # 也相当求行的数量

3

3、几维数组

test.ndim

2

4、数组有多少个元素

test.size

12

5、数据类型

test.dtype

dtype('int32')

6、数据类型的名字

test.dtype.name

'int32'

7、数据类型转换

test.astype(float)

array([[ 1., 15., 10., 2.],

[ 2., 5., 9., 1.],

[ 6., 2., 5., 4.]])

6、查看某个函数怎么使用,有哪些参数

np.info(np.linspace)

End-----------

推荐往期文章如下: