The use of Numpy in Python data analysis study notes in 2020 (4)

table of Contents

1. Basic usage of Numpy

(1) Creation and attributes of the array

(2) Index and slice

2. Advanced usage of Numpy

(1) Change of array shape

(2) The ufunc broadcast mechanism of the array

(3) Sorting and searching

3. Numpy file reading and writing

(1) Numpy file reading

(2) Numpy file storage

(3) Numpy string operation

4. Numpy statistical calculation

(1) Numpy random number generation

(2) Numpy statistical related functions

(3) Numpy linear algebra


1. Basic usage of Numpy

(1) Creation and attributes of the array

# 数组的创建

import numpy as np       # 导入库

arr1 = np.array([1,2,3,4,5])      # 创建数组
# print(arr1)

arr2 = np.array([2,3,5,7], dtype='str')        # 设置数组的数据类型dtype()
# print(arr2)

# 二维数组的创建
arr3 = np.arange(0,20,2)
# print(arr3)

# 产生一个位数为10的等差数列
arr4 = np.linspace(1,10,10, endpoint=False)
# arr4 = np.linspace(1,10,11)
# print(arr4)

# 产生一个位数为10的等比数列
arr5 = np.logspace(1,5,base=2,num=10)
# print(arr5)

# 创建一个全为0的数组
arr6 = np.zeros([5,6])
# print(arr6)

# 创建一个全为1的数组
arr7 = np.ones([5,6])
# print(arr7)


# 创建一个对角线全为1的数组,其余数据全为0
arr8 = np .eye(6)
# print(arr8)

# 创建一个对角线为1,2,3,4,5,6的数组,其余数据全为0
arr9 = np.diag([1,2,3,4,5,6])
# arr9 = np.diag([1,2,3,4,5,6])+1
# print(arr9)
# print(arr9.ndim)                  # .ndim查看数组维度
# print(arr9.shape)                   # .shape查看数组行列数
# print(arr9.size)                   # .siae查看数组有多少元素
# print(arr9.dtype)                    # .stype查看数组的数据类型

(2) Index and slice

One-dimensional array indexing and slicing:

import numpy as np

arr = np.arange(1,10)     # 创建一个一维数组
print(arr)
# >>> [1 2 3 4 5 6 7 8 9]

print(arr[2:3])     # 索引数组中第三位数字
# >>> [3]

print(arr[::-1])    # 倒序输出数组
# >>> [9 8 7 6 5 4 3 2 1]

print(arr[-1])      # 访问数组最后一个元素
# >>> 9

print(arr[-4:-2])     # 访问数组第三位和第四位数字
# >>> [6 7]

arr[5] = 12        # 修改数组第六位元素为12
arr[1:3] = [14,15]    # 修改数组第二位和第三位数字
print(arr)
# >>> [ 1 14 15  4  5 12  7  8  9]


# .copy方法将一个数组赋值给另一个数组,当修改赋值的数组时,原数组中的数据不发生任何改变
# arr1 = np.arange(11,21)
# arr2 = arr1.copy()
# arr2[1:3] = [5,6]
# print(arr1)
# print(arr2)

# >>> [11 12 13 14 15 16 17 18 19 20]
# >>> [11  5  6 14 15 16 17 18 19 20]

Two-dimensional array indexing and slicing:

Simple index

import numpy as np

# 创建一个二维数组
data = ((1,2,3,4,5,6),(2,3,4,5,6,7),(3,4,5,6,7,8),(4,5,6,7,8,9))
arr = np.array(data)
print(arr)
'''
打印结果:
[[1 2 3 4 5 6]
 [2 3 4 5 6 7]
 [3 4 5 6 7 8]
 [4 5 6 7 8 9]]
'''

# print(arr[2])      # 打印数组第三行数据
# >>> [3 4 5 6 7 8]

print(arr[2,1])    # 打印数组第三行第二位数据
# print(arr[2][1])   # 打印数组第三行第二位数据
# >>> 4

print(arr[:,2:4])    # 访问数组第三列到第四列数据
'''
打印结果:
[[3 4]
 [4 5]
 [5 6]
 [6 7]]
'''

# print(arr[1:,2:])      # 访问数组第二行到最后一行并且是第三列的数据
'''
打印结果:
[[4 5 6 7]
 [5 6 7 8]
 [6 7 8 9]]
'''

print(arr[arr>5])      # 打印数组中大于5的元素
# >>> [6 6 7 6 7 8 6 7 8 9]

print((arr>5) & (arr>7))
'''
打印结果:
[[False False False False False False]
 [False False False False False False]
 [False False False False False  True]
 [False False False False  True  True]]
'''
print(arr[(arr>5) & (arr>7)])
# >>> [8 8 9]

Fancy Index

import numpy as np

# 创建一个二维数组
data = ((1,2,3,4,5,6),(2,3,4,5,6,7),(3,4,5,6,7,8),(4,5,6,7,8,9))
arr = np.array(data)
print(arr)
'''
打印结果:
[[1 2 3 4 5 6]
 [2 3 4 5 6 7]
 [3 4 5 6 7 8]
 [4 5 6 7 8 9]]
'''

# print(arr[[2,1]])     # 指定输出第三行和第二行数据
'''
打印结果:
[[3 4 5 6 7 8]
 [2 3 4 5 6 7]]
'''

print(arr[[3,2],[0,1]])    # arr[3,0]  arr[2,1]  指定输出第四行第一个元素和第三行第二个元素
# >>> [4 4]

# 对行数不做限制,对列数做限制
print(arr[:,[2,0]])      # 访问第三行和第一行,二维结构
'''
打印结果:
[[3 1]
 [4 2]
 [5 3]
 [6 4]]
'''

print(arr[:, 1])     # 访问第二列数据,一维结构
# >>> [2 3 4 5]

print(arr[np.ix_([0, -1], [0,1,3])])    # 访问第一行和最后一行的第一列、第二列以及第四列数据
# print(arr[[0,-1]][:, [0, 1, 3]])
'''
打印结果:
[[1 2 4]
 [4 5 7]]
'''

2. Advanced usage of Numpy

(1) Change of array shape

.reshape()

import numpy as np

data = ((1,2,3,4,5),(3,4,5,3,6),(3,6,9,4,6),(4,6,8,2,5))
arr = np.array((data))
print(arr)
'''
[[1 2 3 4 5]
 [3 4 5 3 6]
 [3 6 9 4 6]
 [4 6 8 2 5]]
'''

# .reshape() 改变数组的行列数,但不会改变原数组
print(arr.reshape(5,4))
'''
[[1 2 3 4]
 [5 3 4 5]
 [3 6 3 6]
 [9 4 6 4]
 [6 8 2 5]]
'''

.resize()

import numpy as np

data = ((1,2,3,4,5),(3,4,5,3,6),(3,6,9,4,6),(4,6,8,2,5))
arr = np.array((data))
print(arr)
'''
[[1 2 3 4 5]
 [3 4 5 3 6]
 [3 6 9 4 6]
 [4 6 8 2 5]]
'''

# .resize()  改变数组的行列数,也直接改变原数组
arr.resize(5,4)
print(arr)
'''
[[1 2 3 4]
 [5 3 4 5]
 [3 6 3 6]
 [9 4 6 4]
 [6 8 2 5]]
'''

.shape

import numpy as np

data = ((1,2,3,4,5),(3,4,5,3,6),(3,6,9,4,6),(4,6,8,2,5))
arr = np.array((data))
print(arr)
'''
[[1 2 3 4 5]
 [3 4 5 3 6]
 [3 6 9 4 6]
 [4 6 8 2 5]]
'''

arr.shape = (5,4)
print(arr)
'''
[[1 2 3 4]
 [5 3 4 5]
 [3 6 3 6]
 [9 4 6 4]
 [6 8 2 5]]
'''

 Reduce a two-dimensional array to a one-dimensional array:

import numpy as np

data = ((1,2,3,4,5),(3,4,5,3,6),(3,6,9,4,6),(4,6,8,2,5))
arr = np.array((data))
print(arr)
'''
[[1 2 3 4 5]
 [3 4 5 3 6]
 [3 6 9 4 6]
 [4 6 8 2 5]]
'''

# 横向降维
# print(arr.ravel())     # 方法一
print(arr.flatten())     # 方法二
# >>> [1 2 3 4 5 3 4 5 3 6 3 6 9 4 6 4 6 8 2 5]

# 纵向降维
# print(arr.ravel(order='F'))     # 方法一
print(arr.flatten(order='F'))     # 方法二
# >>> [1 3 3 4 2 4 6 6 3 5 9 8 4 3 4 2 5 6 6 5]


# 行列变换
print(arr.reshape(2,10))      # 方法一
# print(arr.reshape(2,-1))      # 方法二
'''
[[1 2 3 4 5 3 4 5 3 6]
 [3 6 9 4 6 4 6 8 2 5]]
'''

 

(2) The ufunc broadcast mechanism of the array

(3) Sorting and searching

Sort function: sort function and argsort function, note that argsort returns the index value from small to large;

import numpy as np


# np.sort  升序排序
a = np.array([2,7,3,9,5,14,8,4,1,6,8,4,8,9,5,3,45])
# 方法一
# a = np.sort(a)
# 方法二
sorted(a)
print(a)
# >>> [ 1  2  3  3  4  4  5  5  6  7  8  8  8  9  9 14 45]

# 降序排列
# b = sorted(a, reverse=True)
b = np.array(sorted(a, reverse=True))   # 转换成数组
b = np.argsort(b)     # np.argsort 排完序后原数据在数据中的位置
print(b)
# >>> [45, 14, 9, 9, 8, 8, 8, 7, 6, 5, 5, 4, 4, 3, 3, 2, 1]

Search functions: agrmax and agrmin functions;

import numpy as np

# np.sort  升序排序
a = np.array([2,7,3,9,5,14,8,4,1,6,8,4,8,9,5,3,45])

print(a.argmax())    # 返回数据中最大值所在的位置
# >>> 16
print(a.argmin())    # 返回数据中最小值所在的位置
# >>> 8



arr1 = np.array([[0,1,3],[4,2,9],[4,5,9],[1,-3,4]])
'''
[[ 0  1  3]
 [ 4  2  9]
 [ 4  5  9]
 [ 1 -3  4]]
'''
print(arr1.argmax(axis=0))     # 按列进行操作,返回每列最大值的位置
# >>> [1 2 1]
print(arr1.argmin(axis=0))     # 按列进行操作,返回每列最小值的位置
# >>> [0 3 0]

Conditional assignment: np.where can be customized to return the conditions that meet the conditions, and np.extract returns the value of the elements that meet the conditions.

import numpy as np

arr = np.arange(12).reshape(4,3)
'''
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
'''
arr = np.where(arr > 4, '1','0')
print(arr)
'''
[['0' '0' '0']
 ['0' '0' '1']
 ['1' '1' '1']
 ['1' '1' '1']]
'''

 

import numpy as np

arr = np.arange(12).reshape(4,3)
'''
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
'''

arr = np.extract(arr > 4, arr)
print(arr)
'''
[ 5  6  7  8  9 10 11]
'''

 

3. Numpy file reading and writing

(1) Numpy file reading

A. You can use genfromtxt to read txt or csv files;

B. You can use loadtxt to read txt or csv files;

C. The functions of the two functions are similar, and genfromtxt is more aimed at structured data.

import numpy as np

# 读取文件
data = np.genfromtxt('arr.txt', delimiter=',')

data = np.genfromtxt('sales.csv', delimiter=',', skip_header=1)      # skip_header=1表示跳过表头读取数据

# 使用loadtxt读取
data = np.loadtxt('arr.txt', delimiter=',')

(2) Numpy file storage

A. Generally stored as txt or csv files;

B、savetxt(fname,data,delimiter,fmt);

c. Generally, the above four parameters are commonly used, which are the saved path, data, separator and format.

import numpy as np

# 读取文件
data = np.genfromtxt('arr.txt', delimiter=',')

# 存储txt数据
np.savetxt('arr1.txt',data, delimiter=',', fmt='%.3f')

# 存储csv数据
np.savetxt('arr1.csv',data, delimiter=',', fmt='%.3f')

(3) Numpy string operation

char module

import numpy as np

#  np.char.upper  字母变成大写的语法
str_list = ['Hello', 'world']
str_arr = np.char.upper(str_list)
print(str_arr)
# >>> ['HELLO' 'WORLD']


# >>> np.char.add  字符串的拼接
a = np.char.add(['中国', '爱好'], ['人民', '和平'])
print(a)
# >>> ['中国人民' '爱好和平']


# >>> np.char.multiply 多次复制
b = np.char.multiply(['中国','人民'], 3)
print(b)

# np.char.join 字符串的分割与拼接
c = np.char.join([':','-'],['China','peace'])
print(c)
# >>> ['C:h:i:n:a' 'p-e-a-c-e']

# np.char.replace 修改字符串
A = ['人生苦短', '我不学python']
d = np.char.replace(A, '不学', '学')
print(d)
# >>> ['人生苦短' '我学python']


# np.char.strip  去除多余部分语法
B = [',中国', ',人民', '爱好', ',和平']
e = np.char.strip(B, ',')
print(e)
# >>> ['中国' '人民' '爱好' '和平']

# np.char.find(列表名,'查找内容') 此语法用于查找内容

 

4. Numpy statistical calculation

(1) Numpy random number generation

Numpy random number generation is generated through the random function, and all generated are pseudo-random numbers.

random function:

import numpy as np

# np.random.random() 随机生成0到1的浮点数
print(np.random.random())
# >>> 0.923892421471367

# >>> # np.random.random(100) 随机生成100个0到1的浮点数
print(np.random.random(100))


# 随机生成一个四行三列的随机数
print(np.random.random([4,3]))
'''
[[0.77571289 0.17747931 0.19031084]
 [0.78796167 0.55427268 0.0562924 ]
 [0.28150098 0.87504422 0.01081581]
 [0.7561027  0.91834593 0.07259151]]
'''

randint function:

import numpy as np

# np.random.randint 随机生成整数
print(np.random.randint(0,100, size=10))
# >>> [31 19 60 15 63 48 52 88 13 26]


# 随机生成给定范围的随机整数
print(np.random.randint(0,100, size=[5, 5]))
'''
[[32 76 18 56 58]
 [ 2 16 65 69 34]
 [53 28 87 41 29]
 [11 53 61 39  7]
 [30 92 87 79 52]]
'''

uniform function:

import numpy as np

# np.set_printoptions(precision=2)控制产生的随机数的小数位数
np.set_printoptions(precision=2)

# 产生给定范围且均匀分布的数
a = np.random.uniform(low=0, high=50, size=[5,5])
print(a)
'''
 [42.87 37.03 25.68 44.84 47.42]
 [ 0.42 25.04 23.32 39.87 17.56]
 [47.16 37.28  4.26 38.17 31.47]
 [ 8.77 40.19 12.89 36.89 46.85]]
'''

normal function: (normal distribution)

import numpy as np

# 产生10个均值为1,标准差为3的数字
a = np.random.normal(1, 3, size=10)
# print(a)
# >>> [-3.90475112  3.10656401 -0.65542231 -1.18732134  3.69875681  0.9381126 1.25147991  1.04354389 -0.64479801  0.30947606]

# 查看产生的均值
print(np.mean(np.random.normal(1, 3, size=10000)))

# 查看产生的标准差
print(np.std(np.random.normal(1, 3, size=10000)))

randn function:

import numpy as np

# randn()产生标准正态分布随机数
print(np.random.randn(10, 2))
'''
[[-0.52965208 -1.05684052]
 [ 0.488091   -1.30885924]
 [-0.57514636 -0.43549027]
 [-1.07452458  0.3166585 ]
 [-0.09775564  1.06311644]
 [-0.6850656  -0.88613022]
 [ 0.02777025 -0.89569731]
 [-1.1285278   1.21297933]
 [ 0.16932239 -1.05238369]
 [-0.66562821 -0.49166847]]
'''

shuffle function:

import numpy as np

# shuffle() 对数组中的数据随机排序输出,但是原数组也会发生改变
a = np.array([1,2,3,4,5,6])
np.random.shuffle(a)
print(a)
# >>> [3 4 6 5 1 2]

# permutation() 对数组中的数据随机排序输出,但是原数组不会发生改变
b = np.array([1,2,3,4,5,6])
s = np.random.permutation(b)
print(s)
# >>> [4 1 3 6 2 5]

print(b)
# >>> [1 2 3 4 5 6]

(2) Numpy statistical related functions

arr.txt file data (the following code will be used)

1,2,3,4,5,6
2,3,4,5,6,7
3,4,5,6,7,8
4,5,6,7,8,9
5,6,7,8,9,10
6,7,8,9,10,11
7,8,9,10,11,12
8,9,10,11,12,13
9,10,11,12,13,14
10,11,12,13,14,15
11,12,13,14,15,16
import numpy as np

data = np.genfromtxt('arr.txt', delimiter=',', skip_header=0, encoding='utf-8')
# 如果需要跳过首行,使用语法skip_header(1)
print(data)                 # 打印数据
# print(data.shape)           # data.shape求数据的行列数
# print(data.sum())           # data.sum() 求和计算

# print(data.sum(axis=0))     # axis=0 计算每一列加起来的值
# print(data.sum(axis=1))     # axis=1 计算每一行加起来的值
#
# print(data.mean(axis=0))     # axis=0 计算每一列的平均值
# print(data.mean(axis=1))     # axis=1 计算每一行的平均值

# print(data.cumsum())              # cumsum()对整个数据表进行累加
# print(data.cumsum(axis=0))      # cumsum(axis=0)对每一列进行累加
# print(data.cumsum(axis=1))      # cumsum(axis=1)对每一行进行累加

# print(data.cumprod())           # cumprod()累乘
# print(data.cumprod(axis=0))
# print(data.cumprod(axis=1))

# print(data.max())                  # 计算最大值
# print(data.min())                  # 计算最小值

# print(np.ptp(data))                   # np.ptp(data)求极差(数据中的最大值 — 最小值)
#
# print(np.percentile(data,5))         # 计算中位数

# print(data > 5)                 # 统计数据中大于5的值,如果是返回True,否则返回Flase

# print(np.sum(data > 5))           # 统计数据中大于5的值的个数

(3) Numpy linear algebra

A Understand linear algebra

Multiplying arrays:

import numpy as np

a = np.array([1,2,3])
b = np.array([4,5,6])
vector_dot = np.dot(a, b)
print(vector_dot)
# >>> 32

arr1 = np.array([5,15,25,40]).reshape(4,1)
arr2 = np.arange(12).reshape(3,4)

arr2d = np.dot(arr2, arr1)
print(arr2d)
'''
[[185]
 [525]
 [865]]
'''
import numpy as np

arr3 = np.array([[1,2],[3,4]])
print(arr3)
'''
[[1 2]
 [3 4]]
'''
arr4 = np.array([[4,5],[6,7]])
print(arr4)
'''
[[4 5]
 [6 7]]
'''

print(arr3 * arr4)
'''
[[ 4 10]
 [18 28]]

Matrix multiplication:

import numpy as np

arr3 = np.array([[1,2],[3,4]])
print(arr3)
'''
[[1 2]
 [3 4]]
'''
arr4 = np.array([[4,5],[6,7]])
print(arr4)
'''
[[4 5]
 [6 7]]
'''
# 矩阵乘法
c = np.dot(arr3, arr4)
print(c)
'''
[[16 19]
 [36 43]]
'''

B Understand the transposition and inversion of matrices

Matrix transpose:

import numpy as np

arr3 = np.array([[1,2,3,4],[5,6,7,8],[4,3,5,7],[6,8,3,6]])
print(arr3)
'''
[[1 2 3 4]
 [5 6 7 8]
 [4 3 5 7]
 [6 8 3 6]]
'''

# 矩阵转置(X轴变Y轴,Y轴变X轴)
print(np.transpose(arr3))
'''
[[1 5 4 6]
 [2 6 3 8]
 [3 7 5 3]
 [4 8 7 6]]
'''
print(arr3.shape)      # 查看数据结构

Matrix inversion:

import numpy as np

arr3 = np.array([[1,2,3,4],[5,6,7,8],[4,3,5,7],[6,8,3,6]])
print(arr3)
'''
[[1 2 3 4]
 [5 6 7 8]
 [4 3 5 7]
 [6 8 3 6]]
'''

# 矩阵求逆
arr3_inv = np.linalg.inv(arr3)
print(arr3_inv)
'''
[[-7.50000000e-01  8.33333333e-02  3.33333333e-01 -3.70074342e-17]
 [ 2.81250000e-01  9.37500000e-02 -3.75000000e-01  1.25000000e-01]
 [-3.12500000e-01  5.62500000e-01 -2.50000000e-01 -2.50000000e-01]
 [ 5.31250000e-01 -4.89583333e-01  2.91666667e-01  1.25000000e-01]]
'''

C multivariate linear equation solving

(Because the solving of multivariate linear equations is not commonly used in practice, so the notes are not made. If you have relevant needs, you can leave a comment and let me know. I will update and supplement when I see it.)

 

Guess you like

Origin blog.csdn.net/weixin_44940488/article/details/106445557