码迷,mamicode.com
首页 > 其他好文 > 详细

学习Numpy基础操作

时间:2017-09-29 00:18:38      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:tac   return   ima   解方程   distrib   nes   http   src   逆矩阵   

技术分享

 

# 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

学习Numpy基础操作

标签:tac   return   ima   解方程   distrib   nes   http   src   逆矩阵   

原文地址:http://www.cnblogs.com/jasonhaven/p/7609026.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!