Numpy快速入门学习

numpy学习

import numpy as np
a = np.arange(15) #范围
b = a.reshape(3,5) #转化为3行5列
c = b.shape #(3,5)
d = b.ndim #2 维数
e = b.itemsize #元素占用字节
f = b.size #元素个数
print(type(b)) #<class 'numpy.ndarray'>
import numpy as np
a = np.array([1,'2','3',4,5,6],ndmin = 2,dtype=int) #实例化ndarray对象 ndmin维度为2 dtype指定数据类型
#zeros、ones方法类似
a = np.zeros((2,3),dtype=int) #零填充的数组

a = np.arange(15).reshape(3,5)
a = np.zeros_like(a) #把3行5列所有元素变为0

#等差数列  retstep表示为True显示步长,endpoint为true包含100临界值
a = np.linspace(1,100,20,retstep=True,dtype=int,endpoint=True)
import numpy as np
a = np.random.random() #0-1随机数
b = np.random.randint(10) #0-10随机整数
c = np.random.sample() #生成随机的浮点数
d = np.random.normal() #生成正态分布
e = np.random.uniform() #均勻分布的随机数
f = np.random.seed(11) #随机数种子
print('a:'+str(a),'\nb:'+str(b),'\nc:'+str(c),'\nd:'+str(d),'\ne:'+str(e),'\nf:'+str(f))
import numpy as np
a = np.arange(15).reshape(3,5)
b = a[1:3,1] #[ 6 11]
c = a[a>6] #元素大于6  [ 7  8  9 10 11 12 13 14]
d = a[[1,2]] #花式索引
import numpy as np
a = np.arange(15).reshape(3,5)
f = a.T #转置
b = a.view()
c = a.copy()
d = np.array(a)
print(type(a),type(b),type(c),type(d))
import numpy as np
a = np.arange(15).reshape(3,5)
for x in np.nditer(a): #迭代器
    print(x)
import numpy as np
a = np.arange(0,15,3)
b = np.arange(4,18,3)
c = []
for x ,y in zip(a,b):
    c.append(x+y) #[4, 10, 16, 22, 28]
d = np.add(a,b)#[ 4 10 16 22 28]
import numpy as np
a = np.arange(16).reshape(2,4,2)
b = a.reshape(-1) #展平数组 [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15]
import numpy as np
a = np.array([[0, 0, 0],
            [10, 10, 10],
            [20, 20, 20],
            [30, 30, 30]])
b = np.array([1, 2, 3]) # 重复 b 的各个维度  使用广播机制
# [[ 0  0  0]
#  [10 20 30]
#  [20 40 60]
#  [30 60 90]]
import numpy as np
a = np.arange(12).reshape(3,4)
b = np.arange(12,24).reshape(3,4)
c = np.concatenate((a,b),axis=1)#连接
[[ 0  1  2  3 12 13 14 15]
 [ 4  5  6  7 16 17 18 19]
 [ 8  9 10 11 20 21 22 23]]
 
a = np.arange(12).reshape(3,4)
b = np.arange(12,24).reshape(3,4)
c = np.stack((a,b),1) #堆栈
import numpy as np
a = np.arange(15)
print(a)
b = np.split(a,5)
print(b) #[array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8]), array([ 9, 10, 11]), array([12, 13, 14])]
c = np.split(a,[4,9])
print(c) #[array([0, 1, 2, 3]), array([4, 5, 6, 7, 8]), array([ 9, 10, 11, 12, 13, 14])]
import numpy as np
a = np.arange(15).reshape(3,5)
print(a)
b = np.hsplit(a,5)#垂直切割     vsplit--》水平切割
print(b)
# [array([[ 0],
#        [ 5],
#        [10]]), array([[ 1],
#        [ 6],
#        [11]]), array([[ 2],
#        [ 7],
#        [12]]), array([[ 3],
#        [ 8],
#        [13]]), array([[ 4],
#        [ 9],
#        [14]])]
import numpy as np
a = np.arange(15).reshape(3,5)
print(a)
b = np.resize(a,(5,3)) #返回指定大小的新数组
print(b)
# [[ 0  1  2]
#  [ 3  4  5]
#  [ 6  7  8]
#  [ 9 10 11]
#  [12 13 14]]
import numpy as np
a = np.arange(15).reshape(3,5)
print(a)
e = np.arange(5)
e = np.array(e,ndmin = 2)

b = np.append(a,e)#[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14  0  1  2  3  4]
c = np.append(a,e,axis = 0)
# [[ 0  1  2  3  4]
#  [ 5  6  7  8  9]
#  [10 11 12 13 14]
#  [ 0  1  2  3  4]]
import numpy as np
a = np.arange(15).reshape(3,5)
print(a)
b = np.arange(5)
c = np.insert(a,1,b,axis=0)
# [[ 0  1  2  3  4]
#  [ 0  1  2  3  4]
#  [ 5  6  7  8  9]
#  [10 11 12 13 14]]
d = np.insert(a,2,2,axis=1)
# [[ 0  1  2  2  3  4]
#  [ 5  6  2  7  8  9]
#  [10 11  2 12 13 14]]
import numpy as np
a = np.arange(15).reshape(3,5)
print(a)
b = np.delete(a,1,axis=0)
# [[ 0  1  2  3  4]
#  [10 11 12 13 14]]
c = np.delete(a,1,axis=1)
# [[ 0  2  3  4]
#  [ 5  7  8  9]
#  [10 12 13 14]]
import numpy as np
a = np.array([3,4,4,2,5,7,5,4.2,1],dtype=int)
print(a)
b,idx = np.unique(a,return_index=True)
# [1 2 3 4 5 7]
# [8 3 0 1 4 5]
c,c_idx = np.unique(a,return_inverse=True) #返回旧数组在新的数组位置的下标
# [3 4 4 2 5 7 5 4 1]
# [1 2 3 4 5 7]
# [2 3 3 1 4 5 4 3 0]
d,d_idx = np.unique(a,return_counts=True) #返回新数组元素在旧数组中出现的次数
# [3 4 4 2 5 7 5 4 1]
# [1 2 3 4 5 7]
# [1 1 1 3 2 1]
import numpy as np
a = np.array([[3,4,4,2],[5,7,5,4]],dtype=int)
b = np.sort(a) #排序
# [[2 3 4 4]
#  [4 5 5 7]]
import numpy as np
a = np.arange(1,16)
filter_arr = []
b = a % 2 == 0  #过滤器
c = a[b]
# ------------------------------[ 2  4  6  8 10 12 14]
# for x in a:
#     if x % 2 == 0 :
#         filter_arr.append(True)
#     else:
#         filter_arr.append(False)
# b = a[filter_arr]
# ----------------------
# for x in a:
#     if x % 2 ==0:
#         filter_arr.append(x)
#     else:
#         pass
import numpy as np
a = np.arange(0,16).reshape(4,4)
b = np.arange(0,4)
c =np.add(a,b) #subtract()-->减法  multiply()-->乘法  divide()-->除法
d = a + b
# [[ 0  2  4  6]
#  [ 4  6  8 10]
#  [ 8 10 12 14]
#  [12 14 16 18]]
e = a * b
# [[ 0  1  4  9]
#  [ 0  5 12 21]
#  [ 0  9 20 33]
#  [ 0 13 28 45]]

a = np.array([[0.5, 2, 1, 100],[4,0.25,0.3,4]])
b = np.reciprocal(a) #倒数
# [[2.         0.5        1.         0.01      ]
#  [0.25       4.         3.33333333 0.25      ]]

a = np.array([[3,4],[5,7]])
b = np.power(a,2) #幂函数
# [[ 9 16]
#  [25 49]]

a = np.array([[3,4],[5,7]])
b = np.mod(a,2) #取模
# [[1 0]
#  [1 1]]

a = np.array([[2,3,4], [8, 7, 6], [7,8,9]])
b = np.amin(a) #2
c = np.amin(a,axis=0) #[2 3 4]
d = np.amin(a,axis=1) #[2 6 7]
e = np.amax(a) #9
f = np.amax(a,axis=0) #[8 8 9]
g = np.amax(a,axis=1) #[4 8 9]

h = np.ptp(a,axis=0) #最大值与最小值差 [6 5 5]
j = np.average(a,axis=0) #[5.66666667 6.         6.33333333] 平均值
import numpy as np
a = np.random.random((3,4)) #随机数
b = np.random.rand(3,4)
# [[0.5101048  0.5764455  0.26568822 0.89471467]
#  [0.17843437 0.29631956 0.24929332 0.572631  ]
#  [0.94709559 0.96145517 0.76423133 0.00113706]]
c = np.random.randint(10,size=(3,4))
# [[9 9 1 1]
#  [0 3 0 4]
#  [7 7 3 3]]
d = np.random.choice([2,3,56,6,7],size=(3,4))
# [[ 2  2 56  3]
#  [ 6  6 56  2]
#  [ 7  2  6  6]]
import numpy as np
e = ['hello'] #字符串函数
f = ['world']
h = ['How are you']
a = np.char.add(e,f) #['helloworld']
b = np.concatenate((e,f)) #['hello' 'world']
c = np.char.multiply(e,5) #['hellohellohellohellohello']
d = np.char.center(e,20,fillchar='*') #['*******hello********']
g = np.char.capitalize(e) #['Hello']
t = np.char.title(h) #['How Are You']
s = np.char.lower(h) #['how are you']
u = np.char.upper(h) #['HOW ARE YOU']
k = np.char.split(h,' ')#[list(['How', 'are', 'you'])]
m = np.char.splitlines('i\nlike nhooo') #['i', 'like nhooo']   \n, \r, \r\n 都可用作换行符。
n = np.char.strip(['aaaa','abbb','cccca'],'a') #['' 'bbb' 'cccc']  移除开头或结尾的特定字符
l = np.char.join('-',h) #['H-o-w- -a-r-e- -y-o-u'] 连接数组中的元素或字符串
v = np.char.replace(h,'you','你') #['How are 你']
o = np.char.encode(e,'cp500') #[b'\x88\x85\x93\x93\x96']
p = np.char.decode(o,'cp500') #['hello']
import numpy as np
a = np.random.random(10)*10  #[2.45262042 4.52970368 5.50931557 6.36045153 6.16185986 0.299253862.24821068 5.97371766 6.63056209 6.31047073]
b = np.around(a) #[2. 5. 6. 6. 6. 0. 2. 6. 7. 6.] #四舍五入
c = np.around(a,decimals=1) #[2.5 4.5 5.5 6.4 6.2 0.3 2.2 6.  6.6 6.3]
d = np.around(a,decimals=-1) #[ 0.  0. 10. 10. 10.  0.  0. 10. 10. 10.]
f = np.floor(a) #[2. 4. 5. 6. 6. 0. 2. 5. 6. 6.] 向下取整
g = np.ceil(a) #[3. 5. 6. 7. 7. 1. 3. 6. 7. 7.] 向上取整
import numpy as np
#numpy IO
a = np.array([[2,3,4], [8, 7, 6], [7,8,9]])
#np.save('test',a)
b = np.load('test.npy')
# [[2 3 4]
#  [8 7 6]
#  [7 8 9]]
np.savetxt('test.txt',a,delimiter=',') #以逗号分隔
c = np.loadtxt('test.txt',delimiter=',')
import numpy as np
from matplotlib import pyplot as plt #绘图
a = np.arange(1,15)
b = 2 * a + 6
print(a)
print(b)
plt.plot(a,b)
plt.show()

numpy学习干货

#列表与数据区别
import numpy as np
import matplotlib.pyplot as plt
# arr = np.array([1,2.3,'hello'])
# lists = [1,2.3,'hello']
# print(arr,'\n',lists)

# #读取一张图片数据
# im_arr = plt.imread('./data/picture/a.jpg')
# # print(im_arr) #三维数组
# #显示图像
# plt.imshow(im_arr)
# plt.show()

# ones = np.ones(shape=(3,4),dtype=str) #shape形式
# ones.shape #返回数组的形状
# ones.ndim #返回数组维度
#ones.sizee #返回数组元素的个数
#ones.dtype #返回数组元素的类型
# print(ones)
# zeros = np.zeros(shape=(3,4),dtype=float)
# print(zeros)
# randoms = np.random.randint(10,100,size=(9,10))
# print(randoms)
# linespaces = np.linspace(10,100,num = 20)
# print(linespaces)
# aranges = np.arange(10,100,5)
# print(aranges)

# randoms  = np.random.randint(10,100,size=(5,6))
# print(randoms)
# print(randoms[1]) #下标索引
# print(randoms[1:4]) #行切片
# print(randoms[0:2,0:2],'*'*20) #行列切片
# print(randoms[:,::-1]) #上下倒置
# print(randoms[::-1,::-1]) #元素上下左右倒置
# print(randoms[1,2]) #索引到某点元素

# im_arr = plt.imread('./data/picture/d.jpg')
# re_arr = im_arr[::-1,::-1,::-1] #图片倒置  切片
# plt.imshow(re_arr)
# plt.show()

# randoms = np.random.randint(10,100,size=(5,6))
# print(randoms.reshape((2,3,5))) #重置数组维度
# print(np.concatenate((randoms,randoms),axis = 0)) #数组级联操作  0 表示列,纵向连接  1表示行,横向连接 级联只能在同一维度下进行,且对应的行列个数一样  图片数据拼接
# print(randoms.sum(axis = 0)) # axis = 0每一列的和
# print(randoms.max(axis = 0)) # axis = 0每一列最大值
# print(randoms.min(axis = 0)) # axis = 0每一列最小值
# print(randoms.mean(axis = 0)) # axis = 0每一列平均值

#标准三角函数sin(),cos(),tan()
# print(np.sin(randoms))
# print(np.around(3.5155,2)) #四舍五入 decimals四舍五入小数位  为负数则表示整数位的四舍五入

# print(np.ptp(randoms,axis=0)) #返回每一列的最大值跟最小值差

#标准差是一组数据的平均值的分散程度的度量 公式:std = sqrt(mean((x - x.mean())**2))  标准差是方差的平方根
# std = randoms[1].std() #平方差
# var = randoms[1].var() #方差
# mean = randoms[1].mean() #平均值
# print('标准差:{}'.format(std),'\n','方差:{}'.format(var),'\n','平均值:{}'.format(mean),'\n',randoms[1]) #求下标为1的行的标准差

#矩阵
# print(np.eye(6)) #返回标准的单位矩阵
# print(np.eye(6).T) #转置  行变为列

# arr1 = np.array([[1,3],[4,6]])
# arr2 = np.array([[3,4],[6,7]])
# chengfa1 = arr1 *  arr2
# chengfa2 = np.dot(arr1,arr2)
# chengfa3 = arr1 @ arr2
# print(chengfa1) #元素级乘法
# print(chengfa2) #两个矩阵相乘  np.dot(arr1,arr2) 等价于 arr1 @ arr2
# print(chengfa3)
# print(arr1 + arr2) #两个矩阵相加
# print(5 * arr1) #常量乘一个矩阵
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MtoSlc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值