数据科学包 第一课

import numpy as np
a = np.array([[1,2,3,],
[4,5,6]])
一、a.ndim 矩阵的维度为2维;a.shape 矩阵的形状为2行3列;a.size矩阵元素的个数为6;a.dtype()元素的数据类型,有int8-64(inti由平台决定数据大小)、float16-64、unit8-64、complex64-128(复数)
二、a = np.([1,2,4,],dtype = np.int)默认是64位,位数越高,占用空间越多。
三、创建不同的数组:a = np.zeros((3,4))3行4列元素全部为0的矩阵,同理a = np.ones((3,4))元素全部为1,a = np.eye(4)创建一个单位矩阵;a = np.arange(12).reshape((4,3))元素为0-11,4行3列的矩阵;a = np.linspace(1,5,5,endpoint = False)起始为1,结束为4,均分为5份,endpoint=True包含最后一个元素,否则不包含。同样的可以进行reshape操作
四、基础运算

a = np.array([10,20,30,40])
b = np.arange(4)

1.加减法 c = a+b a-b
2.幂 c = b**3求b的3次方
3.c = np.sin(a)求sin cos tan 前面要加np.
4.多维数组的运算

a = np.array([[1,1],
               [0,1]])
b = np.arange(4).reshape((2,2))

这里的4变成3或者5要报错:cannot reshape array of size 3 into shape (2,2),因为元素的个数不能reshape成2X2的矩阵。
c = a*b 各对于元素相乘 c_dot =np.dot(a,b)矩阵的乘法,另一种表达方式c_dot_2 = a.dot(b)
5.随机数及求和、最大、最小运算

a = np.random.random((2,4))
print(a)
print(np.sum(a,axis=0))#axis=1 在行中求和,axis=0 在列中求和
print(np.min(a,axis=1))#axis=1 在行中求最小,axis=0 在列中求最小
print(np.max(a,axis=0))#axis=1 在行中求最大,axis=0 在列中求最大

不加axis就是在所有的元素参与运算
7.其他运算

A = np.arange(2,14).reshape((3,4))
print(A)
print('a=',np.argmin(A))#A中最小值的位置
print('b=',np.argmax(A))#A中最大值的位置
print('c=',np.mean(A))#求均值或者是A.mean(),np.average()
print('d=',np.median(A))#求中位数
print('e=',np.cumsum(A))#从第一个元素开始累加计算,元素个数和A一致
print('f=',np.diff(A))#相邻两个元素累计相差
print('h=',A.T)#矩阵的转置
print('g=',np.clip(A,5,9))#矩阵的元素中小于5的变成5,大于9的变成9
A= [[ 2  3  4  5]
 [ 6  7  8  9]
 [10 11 12 13]]
a= 0
b= 11
c= 7.5
d= 7.5
e= [ 2  5  9 14 20 27 35 44 54 65 77 90]
f= [[1 1 1]
 [1 1 1]
 [1 1 1]]
h= [[ 2  6 10]
 [ 3  7 11]
 [ 4  8 12]
 [ 5  9 13]]
g= [[5 5 5 5]
 [6 7 8 9]
 [9 9 9 9]]

8.索引

A = np.arange(3,15).reshape((3,4))
print('A=',A)
print('a=',A[2])#默认是行,这里是第3行
print('b=',A[1,2])
print('c=',A[1,:])#第2行所有的元素
print('d=',A[:,0])#第1列的所有元素
print('e=',A[0,1:3])
A= [[ 3  4  5  6]
 [ 7  8  9 10]
 [11 12 13 14]]
a= [11 12 13 14]
b= 9
c= [ 7  8  9 10]
d= [ 3  7 11]
e= [4 5]
for row in A:#遍历每行
    print('row=',row)
for column in A.T:#遍历每列
    print('column=',column)
for item in A.flat:#遍历每个元素
    print('item=',item)
row= [3 4 5 6]
row= [ 7  8  9 10]
row= [11 12 13 14]
column= [ 3  7 11]
column= [ 4  8 12]
column= [ 5  9 13]
column= [ 6 10 14]
item= 3
item= 4
item= 5
item= 6
item= 7
item= 8
item= 9
item= 10
item= 11
item= 12
item= 13
item= 14

9.合并

A = np.array([1,1,1])
B = np.array([2,2,2])
C = np.vstack((A,B))#vertical stack 竖着合并
D = np.hstack((A,B))#左右合并
print(C,'C的形状=',C.shape)
print(D)
[[1 1 1]
 [2 2 2]] C的形状= (2, 3)
[1 1 1 2 2 2]
A = np.array([1,1,1])[:,np.newaxis]#新增1列
B = np.array([2,2,2])[:,np.newaxis]
E = np.hstack((A,B))#左右合并
F = np.concatenate((A,B,A),axis=1)#1表示在行的方向上合并
print(A)
print(E)
print(F)
[[1]
 [1]
 [1]]
[[1 2]
 [1 2]
 [1 2]]
[[1 2 1]
 [1 2 1]
 [1 2 1]]

10.分割

a = np.arange(12).reshape(3,4)
print(a)
print(np.split(a,2,axis=1))#axis=1对列进行操作
print(np.split(a,3,axis=0))#axis=0对列进行操作
print(np.array_split(a,3,axis=1))#按列进行不等的分割
#更为简便的分割方法
print(np.vsplit(a,3))#按行进行分割
print(np.hsplit(a,2))#按列进行分割

11.复制
a = np.arange(4)
b = a.copy

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值