NumPy进阶修炼第四期|NumPy最后二十问
import numpy as np import pandas as pd import warnings warnings.filterwarnings("ignore")
61.如何获得两个数组之间的相同元素
输入:
arr1 = np.random.randint(10,6,6)
arr2 = np.random.randint(10,6,6)
arr1 = np.random.randint(1,10,10)
arr2 = np.random.randint(1,10,10)
print("arr1: %s"%arr1) print("arr2: %s"%arr2) np.intersect1d(arr1,arr2)
62.如何从一个数组中删除另一个数组存在的元素
输入:
arr1 = np.random.randint(1,10,10)
arr2 = np.random.randint(1,10,10)
arr1 = np.random.randint(1,10,10) arr2 = np.random.randint(1,10,10) print("arr1: %s"%arr1) print("arr2: %s"%arr2) np.setdiff1d(arr1,arr2)
63.如何修改一个数组为只读模式
输入:
arr1 = np.random.randint(1,10,10)
arr1 = np.random.randint(1,10,10)
arr1.flags.writeable = False
#尝试修改会报错! arr1[0] = 6
64.如何将list转为numpy数组
输入:
a = [1,2,3,4,5]
a = [1,2,3,4,5]
np.array(a)
65.如何将pd.DataFrame转为numpy数组
输入:
df = pd.DataFrame({'A':[1,2,3],'B':[4,5,6],'C':[7,8,9]})
df = pd.DataFrame({'A':[1,2,3],'B':[4,5,6],'C':[7,8,9]}) print(df) print(df.values)
66.如何使用numpy进行描述性统计分析
输入:
arr1 = np.random.randint(1,10,10)
arr2 = np.random.randint(1,10,10)
arr1 = np.random.randint(1,10,10) arr2 = np.random.randint(1,10,10) print("arr1的平均数为:%s" %np.mean(arr1)) print("arr1的中位数为:%s" %np.median(arr1)) print("arr1的方差为:%s" %np.var(arr1)) print("arr1的标准差为:%s" %np.std(arr1)) print("arr1,arr的相关性矩阵为:%s" %np.cov(arr1,arr2)) print("arr1,arr的协方差矩阵为:%s" %np.corrcoef(arr1,arr2))
67.如何使用numpy进行概率抽样
输入:
arr = np.array([1,2,3,4,5])
arr = np.array([1,2,3,4,5])
np.random.choice(arr,10,p = [0.1,0.1,0.1,0.1,0.6])
68.如何创建副本
输入:
arr = np.array([1,2,3,4,5])
#对副本数据进行修改,不会影响到原始数据 arr = np.array([1,2,3,4,5]) arr1 = arr.copy()
69.如何对数组切片
输入: arr = np.arange(10)
备注:从索引2开始到索引8停止,间隔为2
arr = np.arange(10) a = slice(2,8,2) arr[a] #等价于arr[2:8:2]
70.如何使用NumPy操作字符串
输入:
str1 = ['I love']
str2 = [' Python']
#拼接字符串 str1 = ['I love'] str2 = [' Python'] print(np.char.add(str1,str2)) #大写首字母 str3 = np.char.add(str1,str2) print(np.char.title(str3))
71.如何对数据向上/下取整
输入:
arr = np.random.uniform(0,10,10)
arr = np.random.uniform(0,10,10) print(arr) ###向上取整 print(np.ceil(arr)) ###向下取整 print(np.floor(arr) )
72.如何取消默认科学计数显示数据
np.set_printoptions(suppress=True)
73.如何使用NumPy对二维数组逆序
输入:
arr = np.random.randint(1,10,[3,3])
arr = np.random.randint(1,10,[3,3]) print(arr) print('列逆序') print(arr[:, -1::-1]) print('行逆序') print(arr[-1::-1, :])
74.如何使用NumPy根据位置查找元素
输入:
arr1 = np.random.randint(1,10,5)
arr2 = np.random.randint(1,20,10)
备注:在arr2中根据arr1中元素以位置查找
arr1 = np.random.randint(1,10,5) arr2 = np.random.randint(1,20,10) print(arr1) print(arr2) print(np.take(arr2,arr1))
75.如何使用numpy求余数
输入:
a = 10
b = 3
np.mod(a,b)
76.如何使用NumPy进行矩阵SVD分解
输入:
A = np.random.randint(1,10,[3,3])
np.linalg.svd(A)
77.如何使用NumPy多条件筛选数据
输入:
arr = np.random.randint(1,20,10)
arr = np.random.randint(1,20,10) print(arr[(arr>1)&(arr<7)&(arr%2==0)])
78.如何使用NumPy对数组分类
输入:
arr = np.random.randint(1,20,10)
备注:将大于等于7,或小于3的元素标记为1,其余为0
arr = np.random.randint(1,20,10) print(arr) print(np.piecewise(arr, [arr < 3, arr >= 7], [-1, 1]))
79如何使用NumPy压缩矩阵
输入:
arr = np.random.randint(1,10,[3,1])
备注:从数组的形状中删除单维度条目,即把shape中为1的维度去掉
arr = np.random.randint(1,10,[3,1]) print(arr) print(np.squeeze(arr))
80.如何使用numpy求解线性方程组
输入:
A = np.array([[1, 2, 3], [2, -1, 1], [3, 0, -1]])
b = np.array([9, 8, 3])
求解Ax = b
A = np.array([[1, 2, 3], [2, -1, 1], [3, 0, -1]]) b = np.array([9, 8, 3]) x = np.linalg.solve(A, b) print(x)