Python科学计算与可视化

1、利用numpy将一个列表和元组,转换成数组ndarray对象并输出数据类型和值。
源代码:

import numpy as np

a = [1,2,3,4,5]

b = (6,7,8,9,10)

ndarray1 = np.array(a,dtype=int)

ndarray2 = np.array(b,dtype=int)

print('列表a-数组ndarray1:',ndarray1,'数据类型:',type(ndarray1))

print('元组b-数组ndarray2:',ndarray2,'数据类型:',type(ndarray2))

运行结果:
在这里插入图片描述
2、利用numpy生成多维数组。要求将列表转换成二维或多维数组,并输出数组的维数、大小、最大值、对角元素、数组平铺和数组纵向排序。
源代码:

import numpy as np

index = np.random.randint(0,100,9)

index.shape = 3,3

print("数组:\n",index)

print("维数:",index.shape)

print("大小:",len(index))

print("最大值:",np.max(index))

print("对角元素:",np.diagonal(index,offset=0))

print("数组平铺:",index.flatten())

print("数组纵向排序:\n",np.sort(index,axis=0))

运行结果:
在这里插入图片描述
3、生成5行6列的数组,数组元素是[0,1)区间里的随机浮点数,屏幕上打印输出该数组,并将该数组保存到文件array.txt中(要求:保存到文件中的数组元素保留5位小数,同一行中的元素之间以逗号分隔)。同时,完成如下操作并输出:1)把数组变成6行5列的数组;2)计算每列元素的标准差;3)对数组进行切片,隔两个取一个元素;4)对数组元素进行四舍五入处理。
源代码:

import numpy as np

index = np.random.random(30)

index.shape = 5,6

with open("array.txt","w") as f:

    for i in index:

        for j in i:

            f.write(str(round(j,5)))

            f.write(', ')

        f.write('\n')

print("原数组:\n",index)

print("转置后的数组:\n",index.T)

print("每列元素的标准差:\n",np.std(index,axis=0))

print("切片操作结果:\n",index[::3])

print("四舍五入处理结果:\n",np.round(index,1))

运行结果:
在这里插入图片描述
在这里插入图片描述
4、使用SciPy.ndimage对图像进行处理,并显示处理后图像。
对标准测试图像ascent或其他图片进行如下处理:1)显示预处理灰度图片;2)进行(50, 50))平移处理;3)进行(50, 50)平移后自动填充处理;4)进行逆时针旋转30º处理。

from scipy import ndimage

from scipy import misc

import pylab as pb

ascent = misc.ascent()

ascent1 = ndimage.shift(ascent, (50, 50))

ascent2 = ndimage.shift(ascent, (50, 50), mode="nearest")

ascent3 = ndimage.rotate(ascent, 30)

pb.imshow(ascent, cmap=pb.cm.gray)

pb.figure()

pb.imshow(ascent1, cmap=pb.cm.gray)

pb.figure()

pb.imshow(ascent2, cmap=pb.cm.gray)

pb.figure()

pb.imshow(ascent3, cmap=pb.cm.gray)

pb.show()

运行结果:
在这里插入图片描述
5、使用SciPy求解下面的线性方程组:
在这里插入图片描述

源代码:

import numpy as np

from scipy.linalg import solve

a = np.array([[2,1,-5,1],[1,-3,0,-6],[0,2,-1,2],[1,4,-7,6]])

b = np.array([8,9,-5,0])

x = solve(a,b)

print(x)

运行结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值