科学计算程序设计题

2021/12/12 1:46:54

本文主要是介绍科学计算程序设计题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

# numpy

import numpy as np
vector = np.array([1,2,3,4,5])
matrix = np.array([[1,2,3],
                    [4,5,6],
                    [7,8,9]])
print(vector)   
print(matrix)

 

 

 以上代码分别输出一个1行5列的行向量、3行3列的矩阵。

import numpy as np
vector = np.array([1,2,3,4,5])
matrix = np.array([[1,2,3],
                    [4,5,6],
                    [7,8,9]])
print(vector.shape)   
print(matrix.shape)

 

 

 以上代码输出行向量和矩阵的秩。

import numpy as np
vector = np.array([1,2,3,4,'5'])
matrix = np.array([[1,2,3.0],
                    [4,5,6],
                    [7,8,9]])
print(vector)
print("行向量的类型为:",vector.dtype)   
print(matrix)
print("矩阵的类型为:",matrix.dtype)

 

 

 注意到行向量中有一个元素的类型为字符型,将导致整个向量的类型为字符型;

注意到矩阵中有一个元素的内层元素为浮点型,将导致该元素为浮点型,从而导致整个矩阵为浮点型。

 

import numpy as np
TextTest = np.genfromtxt('7_week_4.txt',delimiter=',',dtype=str)
print(TextTest)_week_4.txt:

 

 

 

 

 以上为代码的运行结果,让7_week_4.txt的文件内容以半角逗号分隔,每个元素的类型为字符型。

import numpy as np
vector = np.array([1,2,3,4,5])
matrix = np.array([[1,2,3],
                    [4,5,6],
                    [7,8,9]])
print("行向量分别输出第一个元素、第一个元素到第二个元素、最后一个元素、以步长为2得到的元素:\n",vector[0],'\n',vector[0:2],'\n',vector[-1],'\n',vector[::2])
  
print("矩阵分别输出第一第一列的元素、第一二行第一二列的元素、第二列的元素:\n",matrix[0][0],'\n',matrix[0:2,0:2],'\n',matrix[:,1])

 

 

 

 以上便是对numpy标准库的学习!

昵称:苒若

学号:3012



这篇关于科学计算程序设计题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程