实验4 Python数据分析与GUI

【实验4 Python数据分析与GUI】实验4.1:NumPy数据分析
题目描述:利用NumPy库完成2项编程任务 。实验效果如图1-1所示 。
(1)创建一个一维数组arr1,存放10个[10, 99]随机整数,计算其最大值,最小值和平均值 。
(2)创建一个二维数组arr2,存放5行5列共25个[10, 99]随机整数,计算其最大值,最小值和平均值 。
提示:使用numpy.array( )函数创建,通过列表生成数组对象 。
import numpy as npimport random# (1)list = []sum = 0for i in range(10):n = int(random.random()*90 + 10)list.append(n)sum += narr1 = np.array(list)print(arr1)arr1.sort()print(arr1[9],arr1[0],sum/10)# (2)sum = 0list2 = []for i in range(5):temp = []for j in range(5):n = int(random.random()*90 + 10)sum += ntemp.append(n)list2.append(temp)arr2 = np.array(list2)max,min= arr2[0][0],arr2[0][0]for i in range(5):for j in range(5):if max < arr2[i][j] :max = arr2[i][j]if min > arr2[i][j] :min = arr2[i][j]print(arr2)print(max,min,sum/25)
实验4.2:SciPy数据分析
题目描述:利用SciPy库完成2项编程任务 。
(1)求解线性方程组 。如图2-1所示 。
(2)图像处理 。预处理灰度图片效果如图2-2;平移处理未自动填充图片效果如图2-3;平移处理自动填充图片效果如图2-4;旋转处理图片效果如图2-5所示 。
提示:使用scipy.mat( )和scipy..solve( )对线性方程求解;
使用scipy.和scipy.misc库对图像进行处理 。使用pylab内部图库
import numpy as npimport scipy as sp# (1)a = sp.mat('[5 2 3;2 3 -4;3 -4 -5]')b = sp.mat('[6;7;8]')solve = np.linalg.solve(a,b)print(solve)# (2)import pylab as plfrom scipy import miscfrom scipy import ndimage# 生成图片ascent = misc.ascent()# 平移shifted_ascent = ndimage.shift(ascent,(50,50))# 平移后填充shifted_ascent2 = ndimage.shift(ascent,(50,50),mode = "nearest")# 旋转rotated_ascent = ndimage.rotate(ascent,30)pl.imshow(ascent,cmap = pl.cm.gray)pl.figure()pl.imshow(shifted_ascent,cmap = pl.cm.gray)pl.figure()pl.imshow(shifted_ascent2,cmap = pl.cm.gray)pl.figure()pl.imshow(rotated_ascent,cmap = pl.cm.gray)pl.figure()
实验4.3:数据分析
题目描述:利用库完成2项编程任务 。
(1)利用列表、元组和字典分别创建3个一级索引的数据结构 。实验效果如图3-1所示 。
(2)利用字典和分别创建2个二级索引的数据结构 。实验效果如图3-2所示 。
提示:使用.( )和.( )分别创建一维和二维数据结构;
from pandas import Seriesfrom pandas import DataFrame# (1)obj_list = [98,88,78,68]obj_tuple = ('c++程序设计','Python程序设计','Java程序设计','物联网工程')obj_dict = {'202101':['张三',18,'男','计科2002'],'202102':['李四',18,'男','计科2002'],'202103':['萌萌',18,'男','计科2002'],'202104':['王五',18,'女','计科2002']}series_list = Series(obj_list,index=["No1","No2","No3","No4"])series_tuple = Series(obj_tuple,index=["CId0001","CId0002","CId0003","CId0004"])series_dict = Series(obj_dict)print("(1) 通过list建立Series: ")print(series_list)print("(2) 通过tuple建立Series: ")print(series_tuple)print("(3) 通过dict建立Series: ")print(series_dict)# (2)obj1 = {"学号":["202001","202002","202003","202004"],"姓名":["张三","李四","萌萌","王五"],"年龄":["18","18","18","18"],"性别":["男","男","男","男"],"班级":["计科2002","计科2002","计科2002","计科2002"]}series_dict1 = Series(["202002",'张三',18,'男','计科2002'],index=["学号","姓名","年龄","性别","班级"])series_dict2 = Series(["202002",'张三',18,'男','计科2002'],index=["学号","姓名","年龄","性别","班级"])series_dict3 = Series(["202002",'张三',18,'男','计科2002'],index=["学号","姓名","年龄","性别","班级"])series_dict4 = Series(["202002",'张三',18,'男','计科2002'],index=["学号","姓名","年龄","性别","班级"])df_obj1 = DataFrame(obj1)df_obj2 = DataFrame([series_dict1,series_dict2,series_dict3,series_dict4])print(df_obj1)print("-----------")print(df_obj2)