学习Numpy基础操作

Posted Jasonhaven.D

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了学习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

以上是关于学习Numpy基础操作的主要内容,如果未能解决你的问题,请参考以下文章

学习Numpy基础操作

numpy 学习 第2篇:ndarray 基础操作

Numpy学习:《Python数据分析基础教程NumPy学习指南第2版》中文PDF+英文PDF+代码

Numpy基础学习

机器学习笔记_1_Numpy

深度学习——基础(基于Pytorch代码)