zoukankan      html  css  js  c++  java
  • 学习Numpy基础操作

     

    # coding:utf-8
    
    import numpy as np
    from numpy.linalg import *
    
    
    def day1():
    	'''
    	ndarray
    	:return:
    	'''
    	lst = [[1, 2, 3], [4, 5, 6]]
    	print(type(lst))
    	np_lst = np.array(lst)
    	print(type(np_lst))
    
    	np_lst = np.array(lst, dtype=np.float)
    	# bool
    	# int,int8,int16,int32,int64,int128
    	# uint8,uint16,uint32,uint64,uint128
    	# float,float8,float16,float32,float64
    	# complex64/128
    
    
    	print(np_lst.shape)
    	print(np_lst.ndim)  # dimnation(维度)
    	print(np_lst.dtype)
    	print(np_lst.itemsize)  # Byte
    	print(np_lst.size)  # 元素个数
    
    
    def day2():
    	'''
    	Array
    	:return:
    	'''
    
    	print(np.zeros([2, 4]))
    
    	print(np.ones([3, 5]))
    
    	print("Rand:")
    	print(np.random.rand(2, 4))
    	print(np.random.rand())
    	print("RandInt:")
    	print(np.random.randint(1, 10))
    	print(np.random.randint(1, 10, 3))
    	print("Randn:")
    	print(np.random.randn(2, 4))
    	print('Choice:')
    	print(np.random.choice([10, 20, 30, 2, 5, 7]))
    	print('Distribute:')
    	print(np.random.beta(1, 10, 100))
    
    
    def day3():
    	'''
    	Array Opes
    	:return:
    	'''
    
    	print(np.arange(1, 11).reshape(2, 5))
    	lst = np.arange(1, 11).reshape(2, 5)
    	print(lst)
    	print(np.exp(lst))
    	print(np.exp2(lst))
    	print(np.sqrt(lst))
    	print(np.log(lst))
    
    	print('...')
    	lst = np.array([
    		[[1, 2, 3, 4], [5, 6, 7, 8]],
    		[[9, 10, 11, 12], [13, 14, 15, 16]],
    		[[17, 18, 19, 20], [21, 22, 23, 24]]
    	])
    	print('axis=0')
    	print(lst.sum(axis=0))
    	print(lst.max(axis=0), lst.min(axis=0))
    	print('axis=1')
    	print(lst.sum(axis=1))
    	print(lst.max(axis=0), lst.min(axis=0))
    	print('...')
    	lst1 = np.array([1, 2, 3, 4])
    	lst2 = np.array([1, 2, 3, 4])
    	print(lst1 + lst2)
    	print(lst1 * lst2)
    	print(lst1 / lst2)
    	print(lst1 - lst2)
    	print(lst1 ** 2)
    
    	print('Dot')
    	print(np.dot(lst1.reshape([2, 2]), lst2.reshape([2, 2])))
    	print('Concatenate')
    	print(np.concatenate((lst1, lst2), axis=0))
    	print(np.vstack((lst1, lst2)))
    	print(np.hstack((lst1, lst2)))
    	print(np.split(lst1, 2))
    	print(np.split(lst1, 4))
    	print(np.copy(lst1))
    
    
    def day4():
    	'''
    	linear
    	:return:
    	'''
    
    	lst = np.array([[1, 2], [3, 4]])
    	print(lst)
    	print(inv(lst))  # 矩阵的逆矩阵
    	print(lst.transpose())  # 转置矩阵
    	print(det(lst))  # 行列式
    	print(eig(lst))  # 特征值和特征向量
    	y = np.array([[5, ], [7, ]])
    	print(solve(lst, y))  # 解方程组
    
    
    if __name__ == '__main__':
    	pass
  • 相关阅读:
    使用OpenSSL自建一个HTTPS服务
    工程实践项目中的需求分析建模—问答系统后端
    代码中的软件工程—分析一个命令行菜单小程序
    Git多人项目开发流程演练
    Docker笔记
    Nginx+Gunicorn+Supervisor部署Flask应用
    Python协程之asyncio
    Python类元编程
    搬家到博客园啦
    Spring boot Security 登陆安全配置
  • 原文地址:https://www.cnblogs.com/jasonhaven/p/7609026.html
Copyright © 2011-2022 走看看