美文网首页
Numpy 科学计算库

Numpy 科学计算库

作者: 比特跃动 | 来源:发表于2019-04-22 21:35 被阅读0次

备注:output代码的--------------是我自己加的,在input中没有相关代码。

  1. 相关概念
    • Numpy用于矩阵运算,ndarray表示,[1,2,3]表示1*3的一维矩阵,[[1,2],[1,2],[1,2]]表示3*2的二维矩阵。
    • 数据和代码放在一个文件夹下,方便写代码。
    • numpy下的数据结构需要都一致
    • 取数的时候[1,3],表示取数的范围是[1,3)。0表示数据的第一行/列。
  1. 常用代码
    • 读数
      • 读取txt:numpy.genfromtxt("_.txt",delimiter=",",dtype)
    • 矩阵的行列数:矩阵.shape
    • 函数的详细说明:help(函数)
      • 代码介绍简练,读者对于不懂的可以使用help
  1. 矩阵基础知识
#input
#读数
import numpy
world_alcohol = numpy.genfromtxt("world_alcohol.txt",delimiter=',',dtype=str)
world_alcohol

world_alcohol.shape  #了解矩阵行列数:995行,5列
help(help) #了解help函数用法
zhaoyue= numpy.array([[11,12,13,14],
                      [21,22,23,24],
                      [31,32,33,34],
                      [41,42,43,44]])

zhaoyue[1:4,1:4] #提取zhaoyue的第2,3,4行与第2,3,4列的交际的数据。




#output
array([[22, 23, 24],
       [32, 33, 34],
       [42, 43, 44]])
#input
#The matrix product can be performed using the dot function or method
A = np.array( [[1,1],
               [0,1]] )
B = np.array( [[2,0],
               [3,4]] )
print(A*B)
print ( A.dot(B))
print (np.dot(A, B)) 




#output
#注意这里的矩阵运算,*表示对应位置相乘,.dot表示矩阵乘法。
[[2 0]
 [0 4]]
[[5 4]
 [3 4]]
[[5 4]
 [3 4]]
  1. 矩阵变形
#input
a = np.floor(10*np.random.random((3,4)))
print (a)




#output
[[2. 3. 0. 8.]
 [2. 4. 3. 2.]
 [7. 0. 3. 3.]]
#input
a = np.floor(10*np.random.random((3,4)))
print (a)




#output
[[2. 3. 0. 8.]
 [2. 4. 3. 2.]
 [7. 0. 3. 3.]]
#input
a = np.floor(10*np.random.random((2,2)))
b = np.floor(10*np.random.random((2,2)))
print(a)
print(b)
print(a.T) #矩阵转置
print(np.hstack((a,b))) #左右拼接
print(np.vstack((a,b))) #上下拼接



#output
[[1. 3.]
 [1. 7.]]
--------------
[[2. 4.]
 [1. 1.]]
--------------
[[1. 1.]
 [3. 7.]]
--------------
[[1. 3. 2. 4.]
 [1. 7. 1. 1.]]
--------------
[[1. 3.]
 [1. 7.]
 [2. 4.]
 [1. 1.]]
  1. 复制
a = 5
b = a

ab的id一致。

相关文章

  • NumPy学习-初见

    NumPy 是 Python 中科学计算的基础包,很多其他的科学计算库都是构建在这个库之上,在 Numpy 官网上...

  • Python:一篇文章掌握Numpy的基本用法

    前言 Numpy是一个开源的Python科学计算库,它是python科学计算库的基础库,许多其他著名的科学计算库如...

  • 一、numpy

    1、科学计算库numpy 输出: ['3.542485' '1.9...

  • Numpy的一些小知识 - 01

    Numpy是科学计算当中最常用的python工具库之一,是很多工具库的基础,掌握Numpy的一些基本概念对科学计算...

  • Python——ndarray多维数组对象介绍

    1.Numpy库介绍: Numpy是Numercial Python的简称,是一个开源的Python科学计算基础库...

  • 科学计算库numpy

    交换矩阵的其中两行 输出 :[[ 0 1 2 3 4][ 5 6 7 8 9][10 11 12 ...

  • NumPy科学计算库

    NumPy(Numerical Python)是Python的⼀种开源的数值计算扩展。提供多维数组对象,各种派⽣对...

  • Numpy 科学计算库

    备注:output代码的--------------是我自己加的,在input中没有相关代码。 相关概念Numpy...

  • Python Scipy库

    Scipy库的简介 Scipy高级科学计算库:和Numpy联系很密切,Scipy一般都是操控Numpy数组来...

  • Numpy | 基础操作(矩阵)

    NumPy 基础操作 什么是 NumPy NumPy是Python中科学计算的基础包。它是一个Python库,提供...

网友评论

      本文标题:Numpy 科学计算库

      本文链接:https://www.haomeiwen.com/subject/oxzjgqtx.html