内容学习自:
Python for Data Analysis, 2nd Edition
就是这本
纯英文学的很累,对不对取决于百度翻译了
前情提要:
各种方法贴:
https://www.cnblogs.com/baili-luoyun/p/10250177.html
本内容主要讲的是
数组和矢量的计算
一: 创建数组
传入内容(序列化对象),转化成数组
np.array()
1:单维数组 (和列表没什么两样)
1 单维数组 2 # data1 = [6, 7, 8, 9, 10, 1, 2] 3 # arr1 =np.array(data1) 4 # print(arr1)
>>>>
[ 6 7 8 9 10 1 2]
2:多维数组
data2 =[[1,2,3,4],[5,6,7,8]] arr2 =np.array(data2) l =arr2.shape #返回维度 print(arr2) print(l)
>>>>>
[[1 2 3 4]
[5 6 7 8]]
(2, 4)
3:系统内置函数np.arange(横,纵)
data3 =np.arange(1,15)
print(data3)
>>>>>
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
4:生成对角线数组
data4 =np.eye(3,3) print(data4)
>>>
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
二:数组之间的运算(矢量化运算可以直接参与赋值)
1:矢量可以直接参运算
* 乘法 lis =[[1,2,3],[4,5,6]] data1 =np.array(lis) print(data1) #矢量化运算可以直接参与运算 l2 =data1*data1 print(l2)
-减法
l3 =data1+data1
print(l3)
>>>>>
[[1 2 3]
[4 5 6]]
[[ 1 4 9]
[16 25 36]]
[[ 2 4 6]
[ 8 10 12]]
2:数组与标量之间可以直接传递到数组的每个内容
l4 =1/data1 print(l4)
>>>>
[[1. 0.5 0.33333333]
[0.25 0.2 0.16666667]]
3:数组相同维度的比较(维度不同则报错)
data2 =np.array([[3,2,1],[6,5,4]]) l4 =data1>data2 print(l4) >>>> [[False False True] [False False True]]
三:数组的切片和索引
1:一维数组
print(arr1[1]) print(arr1[4]) print(arr1[:4]) arr1[1]=888 #给对应索引位置换值 print(arr1) arr1[:] =55 # 给所有索引位置换值 print(arr1) >>>>> 2 5 [1 2 3 4] [ 1 888 3 4 5] [55 55 55 55 55]
2:多维数组:
arr2d =np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]]) print(arr2d) print(arr2d[3]) #多维数组索引是一维数组 print(arr2d[1][2]) #一维数组以后切牌获得单个值 arr2d[0]=42 #多维数组单独索引赋值,则整个一维数组都换值 arr2d[0][0] =55 print(arr2d) # 把多为数组都拆开单独赋值获得内容 l =arr2d[0:2] #多维数组切片,可以 l =arr2d[:2,2] #切完之后再,进行小切 l =arr2d[:2,1:] #获取前二行后两列 l =arr2d[2,:2] l1 =arr2d[1,:2] #获取第二个前两列 l =arr2d[:3,:2] #获取前三行前两列 l =arr2d[:,:1] #多维数组,索引从1开始 print(l)
>>>
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
[10 11 12]
6
[[55 42 42]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
[[55]
[ 4]
[ 7]
[10]]
五:布尔型索引
names =np.array(['bob','bob','bob','bob','bob','apple','alan','sele']) data =np.random.randn(8,4) # names =='bob' print(names=='bob') #字符串也可以进行比较返回一个bool值
print(names)
>>>>
[ True True True True True False False False]
['bob' 'bob' 'bob' 'bob' 'bob' 'apple' 'alan' 'sele']
print(data)
print(data[names =='bob']) #有两个bob 所以返回行 #布尔型索数组长度,需要等于布尔型索引长度,
>>>>>
[[-0.3316016 1.78918492 -1.59222587 0.21469427]
[ 1.55247352 1.14508726 -0.68673629 -0.42648069]
[-0.95385141 -0.1938747 0.22302977 -1.25419395]
[ 0.09290589 0.26875941 -0.34120567 1.67205517]
[ 1.17781667 -0.83402007 -2.64528669 -0.70822941]
[ 0.34199013 1.81982055 0.60103061 0.39070584]
[-1.20352138 0.7618197 -1.29754963 1.19821404]
[ 0.84278983 -0.60723742 -0.73442051 -0.87391669]]
[[-0.3316016 1.78918492 -1.59222587 0.21469427]
[ 1.55247352 1.14508726 -0.68673629 -0.42648069]
[-0.95385141 -0.1938747 0.22302977 -1.25419395]
[ 0.09290589 0.26875941 -0.34120567 1.67205517]
[ 1.17781667 -0.83402007 -2.64528669 -0.70822941]]
print(data[names =='bob',2:]) #获取之后也可以切片
print(names!='bob')
print(data[~(names =='bob')])
>>>>>>>>>>
[[-1.05528931 -0.19176188]
[-1.55590721 0.43000376]
[ 1.3635074 0.75722279]
[ 2.80719722 0.3907232 ]
[ 0.3805182 -1.30951587]]
[False False False False False True True True]
[[ 0.64999255 -1.40220795 -0.11081563 0.51079912]
[ 1.24400895 -1.81700713 -0.24652383 1.20494275]
[-1.53308854 1.09828319 1.32899806 -0.86707369]]