NumPy学的还不错?来试试这20题!
Python爬虫与数据挖掘
共 2962字,需浏览 6分钟
·
2020-09-01 13:33
回复“书籍”即可获赠Python从入门到进阶共10本电子书
数据查找
import numpy as np
import pandas as pd
import warnings
warnings.filterwarnings("ignore")
arr1 = np.random.randint(10,6,6)
arr2 = np.random.randint(10,6,6)
arr1 = np.random.randint(10,6,6)
arr2 = np.random.randint(10,6,6)
print("arr1: %s"%arr1)
print("arr2: %s"%arr2)
np.intersect1d(arr1,arr2)
数据修改
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.setdiff1d(arr1,arr2)
数据修改
arr1 = np.random.randint(1,10,10)
arr1 = np.random.randint(1,10,10)
arr1.flags.writeable = False
数据转换
a = [1,2,3,4,5]
a = [1,2,3,4,5]
np.array(a)
数据转换
df = pd.DataFrame({'A':[1,2,3],'B':[4,5,6],'C':[7,8,9]})
df.values
数据分析
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))
数据抽样
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])
数据创建
arr = np.array([1,2,3,4,5])
#对副本数据进行修改,不会影响到原始数据
arr = np.array([1,2,3,4,5])
arr1 = arr.copy()
数据切片
arr = np.arange(10)
arr = np.arange(10)
a = slice(2,8,2)
arr[a] #等价于arr[2:8:2]
字符串操作
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))
数据修改
arr = np.random.uniform(0,10,10)
arr = np.random.uniform(0,10,10)
print(arr)
###向上取整
print(np.ceil(arr))
###向下取整
print(np.floor(arr) )
格式修改
np.set_printoptions(suppress=True)
数据修改
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, :])
数据查找
arr1 = np.random.randint(1,10,5)
arr2 = np.random.randint(1,20,10)
arr1 = np.random.randint(1,10,5)
arr2 = np.random.randint(1,20,10)
print(arr1)
print(arr2)
print(np.take(arr2,arr1))
数据计算
a = 10
b = 3
np.mod(a,b)
数据计算
A = np.random.randint(1,10,[3,3])
np.linalg.svd(A)
数据筛选
arr = np.random.randint(1,20,10)
arr = np.random.randint(1,20,10)
print(arr[(arr>1)&(arr<7)&(arr%2==0)])
数据修改
arr = np.random.randint(1,20,10)
arr = np.random.randint(1,20,10)
print(arr)
print(np.piecewise(arr, [arr < 3, arr >= 7], [-1, 1]))
数据修改
arr = np.random.randint(1,10,[3,1])
arr = np.random.randint(1,10,[3,1])
print(arr)
print(np.squeeze(arr))
数据计算
A = np.array([[1, 2, 3], [2, -1, 1], [3, 0, -1]])
b = np.array([9, 8, 3])
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)
以上就是我总结的NumPy经典20题,你都会吗?并且每题我都只给出了一种解法,而事实上每题都有多种解法,所以你应该思考是否有更好的思路!
------------------- End -------------------
往期精彩文章推荐:
欢迎大家点赞,留言,转发,转载,感谢大家的相伴与支持
想加入Python学习群请在后台回复【入群】
万水千山总是情,点个【在看】行不行
/今日留言主题/
随便说一两句吧~~
评论