Numpy & Pandas (莫烦 Python 数据处理教程)笔记

Numpy教程笔记:

import numpy as np

#numpy属性
print('#numpy属性')
array = np.array([[1,2,3],
                 [2,3,4]])
print(array)
print('number of dim',array.ndim)#number of dim 2
print('shape:',array.shape)#shape: (2, 3)
print('size',array.size)#size 6
print()

#numpy的创建array
print('numpy的创建array')
a=np.array([2,23,4],dtype=np.float32)
print(a)#[ 2. 23.  4.]

a=np.array([[2,23,4],
          [2,32,4]])
print(a)

a=np.zeros((3,4))
print(a)

a=np.ones((3,4),dtype=np.int16)
print(a)

a=np.empty((3,4))
print(a)

a=np.arange(10,20,2)
print(a)

a=np.arange(12).reshape((3,4))
print(a)

a=np.linspace(1,10,5)
print(a)

a=np.linspace(1,10,6).reshape((2,3))
print(a)
print()

#numpy的基础运算
print('#numpy的基础运算')
a=np.array([10,20,30,40])
b=np.arange(4)
print(a,b)
c=a-b
print(c)
c=a+b
print(c)
c=a*b
print(c)
c=b**2
print(c)
c=10*np.sin(a)
print(c)
print(b)
print(b<3)
print(b==3)

a=np.array([[1,1],
          [0,1]])
b=np.arange(4).reshape(2,2)
print(a)
print(b)
c=a*b
c_dot=np.dot(a,b)
c_dot_2=a.dot(b)
print(c)
print(c_dot)
print(c_dot_2)

a=np.random.random((2,4))
print(a)

print(np.sum(a))
print(np.min(a))
print(np.max(a))

print(np.sum(a,axis=1))#axis=1是行,axis=0是列
print(np.min(a,axis=0))
print(np.max(a,axis=1))
print()

#numpy的基础运算2
print('#numpy的基础运算2')
A=np.arange(14,2,-1).reshape((3,4))
print(A)
print(np.argmin(A))
print(np.argmax(A))
print(np.mean(A))

print(A.mean())
print(np.average(A))

print(np.median(A))
print(np.cumsum(A))#累和
print(np.diff(A))#累差
print(np.nonzero(A))#返回非零的行和列索引
print(np.sort(A))#逐行排序
print(np.transpose(A))
print((A.T).dot(A))
print(np.clip(A,5,9))#所有比5小的数变为5,比9大的数变为9,中间的数不变
print(np.mean(A,axis=0))#axis=1是行,axis=0是列
print()

#numpy的索引
print('#numpy的索引')
A=np.arange(3,15).reshape((3,4))
print(A)
print(A[2])
print(A[2][1])
print(A[2,1])
print(A[2,:])
print(A[:,1])
print(A[1,1:3])

for row in A:
    print(row)
    
for column in A.T:
    print(column)

print(A.flatten())
for item in A.flat:
    print(item)
print()

#numpy的array合并
print('#numpy的array合并')
A=np.array([1,1,1])#注意:这里是3行一列!!!(3,)
B=np.array([2,2,2])#注意:这里是3行一列!!!(3,)
print(A.shape)#注意:这里是3行一列!!!(3,)

print(np.vstack((A,B)))#vertical stack,即竖方向拼接
    
C=np.vstack((A,B)) 
D=np.hstack((A,B))#horizontal stack,即横方向拼接
print(D)
print(A.shape,C.shape,D.shape)

print(A.T.shape)
print(A[np.newaxis,:].shape)
print(A[:,np.newaxis].shape)
print(A[:,np.newaxis])

A=np.array([1,1,1])[:,np.newaxis]#A=np.array([1,1,1]).reshape((3,1))
B=np.array([2,2,2])[:,np.newaxis]
print(np.hstack((A,B,A)))
C = np.concatenate((A,B,B,A),axis=0)#纵向合并
print(C)
print()

#numpy的array分割
print('#numpy的array分割')
A=np.arange(12).reshape((3,4))
print(A)

print(np.split(A,2,axis=1))#分割时axis=1分成3x2的两大块

print(np.array_split(A,3,axis=1))

print(np.vsplit(A,3))
print(np.hsplit(A,2))
print()

#numpy的copy & deep copy
print('#numpy的copy & deep copy')
a=np.arange(4)
print(a)
b=a
c=a
d=b
a[0]=0.3
print(a)
a[0]=11
print(a)

print(b)
print(b is a)

d[1:3]=[22,33]
print(a)
print(d)
print(c)

b=a.copy()#deep copy
print(b)
a[3]=44
print(b)

运行结果:

#numpy属性
[[1 2 3]
 [2 3 4]]
number of dim 2
shape: (2, 3)
size 6

numpy的创建array
[ 2. 23.  4.]
[[ 2 23  4]
 [ 2 32  4]]
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
[[1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]]
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
[10 12 14 16 18]
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[ 1.    3.25  5.5   7.75 10.  ]
[[ 1.   2.8  4.6]
 [ 6.4  8.2 10. ]]

#numpy的基础运算
[10 20 30 40] [0 1 2 3]
[10 19 28 37]
[10 21 32 43]
[  0  20  60 120]
[0 1 4 9]
[-5.44021111  9.12945251 -9.88031624  7.4511316 ]
[0 1 2 3]
[ True  True  True False]
[False False False  True]
[[1 1]
 [0 1]]
[[0 1]
 [2 3]]
[[0 1]
 [0 3]]
[[2 4]
 [2 3]]
[[2 4]
 [2 3]]
[[0.5905678  0.65830868 0.79721866 0.21143963]
 [0.03309821 0.26522157 0.67987985 0.81047326]]
4.046207653158971
0.033098207684490455
0.8104732557528412
[2.25753477 1.78867288]
[0.03309821 0.26522157 0.67987985 0.21143963]
[0.79721866 0.81047326]

#numpy的基础运算2
[[14 13 12 11]
 [10  9  8  7]
 [ 6  5  4  3]]
11
0
8.5
8.5
8.5
8.5
[ 14  27  39  50  60  69  77  84  90  95  99 102]
[[-1 -1 -1]
 [-1 -1 -1]
 [-1 -1 -1]]
(array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2]), array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]))
[[11 12 13 14]
 [ 7  8  9 10]
 [ 3  4  5  6]]
[[14 10  6]
 [13  9  5]
 [12  8  4]
 [11  7  3]]
[[332 302 272 242]
 [302 275 248 221]
 [272 248 224 200]
 [242 221 200 179]]
[[9 9 9 9]
 [9 9 8 7]
 [6 5 5 5]]
[10.  9.  8.  7.]

#numpy的索引
[[ 3  4  5  6]
 [ 7  8  9 10]
 [11 12 13 14]]
[11 12 13 14]
12
12
[11 12 13 14]
[ 4  8 12]
[8 9]
[3 4 5 6]
[ 7  8  9 10]
[11 12 13 14]
[ 3  7 11]
[ 4  8 12]
[ 5  9 13]
[ 6 10 14]
[ 3  4  5  6  7  8  9 10 11 12 13 14]
3
4
5
6
7
8
9
10
11
12
13
14

#numpy的array合并
(3,)
[[1 1 1]
 [2 2 2]]
[1 1 1 2 2 2]
(3,) (2, 3) (6,)
(3,)
(1, 3)
(3, 1)
[[1]
 [1]
 [1]]
[[1 2 1]
 [1 2 1]
 [1 2 1]]
[[1]
 [1]
 [1]
 [2]
 [2]
 [2]
 [2]
 [2]
 [2]
 [1]
 [1]
 [1]]

#numpy的array分割
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[array([[0, 1],
       [4, 5],
       [8, 9]]), array([[ 2,  3],
       [ 6,  7],
       [10, 11]])]
[array([[0, 1],
       [4, 5],
       [8, 9]]), array([[ 2],
       [ 6],
       [10]]), array([[ 3],
       [ 7],
       [11]])]
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8,  9, 10, 11]])]
[array([[0, 1],
       [4, 5],
       [8, 9]]), array([[ 2,  3],
       [ 6,  7],
       [10, 11]])]

#numpy的copy & deep copy
[0 1 2 3]
[0 1 2 3]
[11  1  2  3]
[11  1  2  3]
True
[11 22 33  3]
[11 22 33  3]
[11 22 33  3]
[11 22 33  3]
[11 22 33  3]


Pandas教程(待续):



你可能感兴趣的:(机器学习)