Numpy and pandas强大的库,数据分析时十分重要,让数据分析快,用c语言写的
numpy的基本属性(numpy是基于矩阵的运算)
import numpy as np
array=np.array([[1,2,3],[4,5,6]])
print(array)
print('number of dim:',array.ndim)#几维数组,是一维还是二维
print('shape:',array.shape)#行数,列数是多少
print('size:',array.size)#总共有多少元素
import numpy as np
a=np.array([2,23,4])
print(a)#输出没有逗号分割列表
输出:
[ 2 23 4]
import numpy as np
a=np.array([2,23,4],dtype=np.int)
print(a.dtype)#输出没有逗号分割列表
输出:
int64
import numpy as np
a=np.array([2,23,4],dtype=np.int32)
print(a.dtype)#输出没有逗号分割列表
输出:
int32
import numpy as np
a=np.zeros((3,4))
print(a)
输出:
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
import numpy as np
a=np.ones((3,4),dtype=np.int16)
print(a)
输出:
[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
import numpy as np
a=np.empty((3,4))
print(a)
输出:
[[6.01347002e-154 1.72965980e+156 7.49744286e+247 1.11787108e+253]
[5.82456862e+257 2.88980624e+204 5.98129759e-154 1.42731838e+228]
[6.01346954e-154 4.47593804e-091 6.01347002e-154 2.21176254e+068]]
import numpy as np
a=np.arange(10,20,2)
print(a)
输出:
[10 12 14 16 18]
import numpy as np
a=np.arange(12).reshape((3,4))
print(a)
输出:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
#在指定的间隔内返回均匀间隔的数字
import numpy as np
a=np.linspace(1,10,5)
print(a)
输出:
[ 1. 3.25 5.5 7.75 10. ]
import numpy as np
a=np.linspace(1,10,6).reshape((2,3))
print(a)
输出:
[[ 1. 2.8 4.6]
[ 6.4 8.2 10. ]]
import numpy as np
a=np.array([10,20,30,40])
b=np.arange(4)
c=a+b
print(c)
输出:
[10 21 32 43]
import numpy as np
a=np.array([10,20,30,40])
b=np.arange(4)
c=b**2#平方
print(c)
输出:
[0 1 4 9]
import numpy as np
a=np.array([10,20,30,40])
b=np.arange(4)
c=10*np.sin(a)
print(c)
输出:
[-5.44021111 9.12945251 -9.88031624 7.4511316 ]
import numpy as np
a=np.array([10,20,30,40])
b=np.arange(4)
print(b)
print(b<3)
输出:
[0 1 2 3]
[ True True True False]
import numpy as np
a=np.array([[1,1],[0,1]])
b=np.arange(4).reshape(2,2)
print(a)
print(b)
输出:
[[1 1]
[0 1]]
[[0 1]
[2 3]]
import numpy as np
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)
输出:
[[1 1]
[0 1]]
[[0 1]
[2 3]]
[[0 1]
[0 3]]
[[2 4]
[2 3]]
[[2 4]
[2 3]]
import numpy as np
a=np.random.random((2,4))#随机生成的二行四列
print(np.sum(a))#所有元素的和
print(np.min(a))#整个矩阵的最小值
print(np.max(a))#整个矩阵的最大值
print(a)
输出:
3.971545736566909
0.18508071505186152
0.8316892881441365
[[0.53973162 0.36452964 0.44466585 0.18508072]
[0.56738272 0.48522782 0.83168929 0.55323808]]
import numpy as np
a=np.random.random((2,4))#随机生成的二行四列
print(a)
print(np.sum(a,axis=1))#每一行中求和
print(np.min(a,axis=0))#每一列中最小值
print(np.max(a,axis=1))#每一行中最大值
输出:
[[0.22203562 0.30471696 0.27934005 0.68606662]
[0.53754092 0.11174921 0.84337411 0.20123639]]
[1.49215925 1.69390064]
[0.22203562 0.11174921 0.27934005 0.20123639]
[0.68606662 0.84337411]
import numpy as np
A=np.arange(2,14).reshape(3,4)
print(A)
print(np.argmin(A))#输出最小值的索引
print(np.argmax(A))#输出最大值的索引
#求平均值
print(np.mean(A))
print(np.average(A))
print(A.mean())
#中位数
print(np.median(A))
#前面几项相加,累加
print(np.cumsum(A))
#累差
print(np.diff(A))
#找出非0的数
print(np.nonzero(A))
#逐行排序
A=np.arange(14,2,-1).reshape(3,4)
print(A)
print(np.sort(A))
#矩阵的转置
print(np.transpose(A))
print(A.T)
#矩阵的乘法
print((A.T).dot(A))
#截取矩阵,小于5的数变为5,大于9的数等于9
print(np.clip(A,5,9))
#按行求平均值
print(np.mean(A,axis=1))
输出:
[[ 2 3 4 5]
[ 6 7 8 9]
[10 11 12 13]]
0
11
7.5
7.5
7.5
7.5
[ 2 5 9 14 20 27 35 44 54 65 77 90]
[[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]))
[[14 13 12 11]
[10 9 8 7]
[ 6 5 4 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]]
[[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]]
[12.5 8.5 4.5]
import numpy as np
A=np.arange(3,15)
print(A)
#一维输出相应位置
print(A[3])
A=np.arange(3,15).reshape((3,4))
#二维的
print(A)
#索引行数
print(A[2])
print(A[1][1])
print(A[1,1])
#第2行的所有数
print(A[2,:])
#第一列的所有数
print(A[:,1])
#第一行的,第1到3列,注意左闭右开
print(A[1,1:3])
#默认迭代行
for row in A:
print(row)
#迭代列
for column in A.T:
print(column)
#迭代项,先转化为一维数组,用flatten转化为一维列表,,而A.flat返回的是迭代器
print(A.flatten())
for item in A.flat:
print(item)
输出:
[ 3 4 5 6 7 8 9 10 11 12 13 14]
6
[[ 3 4 5 6]
[ 7 8 9 10]
[11 12 13 14]]
[11 12 13 14]
8
8
[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
import numpy as np
A=np.array([1,1,1])
B=np.array([2,2,2])
C=np.vstack((A,B))
print(C)#列合并,上下合并
print(A.shape,C.shape)
D=np.hstack((A,B))#左右合并
print(D)
print(A.shape,D.shape)
#横向数列变为列向数列
#在行上面加了个维度
print(A[np.newaxis,:].shape)
#在列上面加了维度
print(A[:,np.newaxis].shape)
print(A[:,np.newaxis])
输出:
[[1 1 1]
[2 2 2]]
(3,) (2, 3)
[1 1 1 2 2 2]
(3,) (6,)
(1, 3)
(3, 1)
[[1]
[1]
[1]]
import numpy as np
#先变为纵向,然后进行合并
A=np.array([1,1,1])[:,np.newaxis]
B=np.array([2,2,2])[:,np.newaxis]
C=np.vstack((A,B))
print(C)#列合并,上下合并
print(A.shape,C.shape)
D=np.hstack((A,B))#左右合并
print(D)
print(A.shape,D.shape)
输出:
[[1]
[1]
[1]
[2]
[2]
[2]]
(3, 1) (6, 1)
[[1 2]
[1 2]
[1 2]]
(3, 1) (3, 2)
import numpy as np
#先变为纵向,然后进行合并
A=np.array([1,1,1])[:,np.newaxis]
B=np.array([2,2,2])[:,np.newaxis]
#进行多个array合并,对比hstack以及cstack,可以指定横向还是纵向合并
C=np.concatenate((A,B,B,A),axis=0)
print(C)
输出:
[[1]
[1]
[1]
[2]
[2]
[2]
[2]
[2]
[2]
[1]
[1]
[1]]
#如何进行分割
import numpy as np
A=np.arange(12).reshape(3,4)
print(A)
#从横向分割,只能进行等量分割,按列
print(np.split(A,2,axis=1))
#进行不等量分割
print(np.array_split(A,3,axis=1))
#进行纵向分割
print(np.vsplit(A,3))
print(np.hsplit(A,2))
输出:
[[ 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]])]
import numpy as np
#都是关联起来的
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 is a)
print(b)
print(d is a)
d[1:3]=[22,33]
print(a)
#想要a的值但是不能关联a
b=a.copy()#深度的copy
print(b)
a[3]=44
print(a)
print(b)
输出:
[0 1 2 3]
[0 1 2 3]
[11 1 2 3]
True
[11 1 2 3]
True
[11 22 33 3]
[11 22 33 3]
[11 22 33 44]
[11 22 33 3]