python实验六到十二作业(待完善)

在这里插入图片描述

###############实验六   面向对象的程序设计###############
#####第一题
# class circle:
#     def __init__(self,yuanxing, r, color):
#         self.location=yuanxing
#         self.r=r
#         self.color=color
#     def GetC(self):
#         return 2 * 3.14 * self.r
#     def GetS(self):
#         return 3.14 * self.r * self.r
#
# Circles= circle((0,0),5, "blue")
# print(f'圆的周长='"{:.2f}".format(Circles.GetC()))
# print(f'圆的面积='"{:.2f}".format(Circles.GetS()))
# # print(str.format('圆的周长={:.2f}\n圆的面积={:.2f}',Circles.GetC(),Circles.GetS()))
#####第二题
# class falldwon:
#     g=9.8
#     def __init__(self, t=0):
#         self.t = t
#         self.v=0
#         self.s=0
#
#     def get_v(self):
#         self.v=self.t * falldwon.g
#         return self.v
#     def get_s(self):
#         self.s=(self.t**2*falldwon.g)/2
#         return self.s
#
# f1=falldwon(float(input("请输入任意时间 t:")))
# print(str.format('在t时刻的速度为:{:.2f}\n在t时刻的位移为:{:.2f}',f1.get_v(),f1.get_s()))
######第三题
# import math
# class Equation:
#     def __init__(self):
#         self.a=eval(input('请输入a的值:'))
#         self.b=eval(input('请输入b的值:'))
#         self.c=eval(input('请输入c的值:'))
#         self.d=self.b*self.b-4*self.a*self.c
#     def getDiscriminantl(self):
#         return self.d
#     def getRoot1(self):
#         if(self.d<0):
#             return 0
#         else:
#             return (-self.b+math.sqrt(self.d))/2
#     def getRoot2(self):
#         if (self.d < 0):
#             return 0
#         else:
#             return (-self.b - math.sqrt(self.d)) / 2*self.a
# equation=Equation()
# print(str.format('判别式的值为:{:.2f}\n一个根的值为:{:.2f}\n另一个根的值为:{:.2f}',equation.getDiscriminantl(),equation.getRoot1(),equation.getRoot2()))
#########第四题
# class vector:
#     def __init__(self,a,b):
#         self.a=a
#         self.b=b
#     def add(self):
#         c1=self.a[0]+self.b[0]
#         c2=self.a[1]+self.b[1]
#         print("向量相加:",(c1,c2))
#     def jian(self):
#         c1=self.a[0]-self.b[0]
#         c2=self.a[1]-self.b[1]
#         print("向量相减:", (c1, c2))
#     def cheng(self):
#         c1=self.a[0]*self.b[0]
#         c2=self.a[1]*self.b[1]
#         c=c1+c2
#         print("向量相乘:", c)
#     def chu(self):
#         c1=self.a[0]/self.b[0]
#         c2=self.a[1]/self.b[1]
#         print(str.format("向量相除:{:.2f}", c1+c2))
#
# x1=eval(input("请输入第一个向量x1:"))
# x2=eval(input("请输入第一个向量x2:"))
# x1x2=vector(x1,x2)
# x1x2.add()
# x1x2.jian()
# x1x2.cheng()
# x1x2.chu()
#########第五题
# class course:
#     def __init__(self,a1,a2,a3,a4):
#         self.cno=a1
#         self.cname=a2
#         self.teacher=a3
#         self.__place=a4
#
#     def shown(self):
#         print("课程编号:", self.cno)
#         print("课程名称:", self.cname)
#         print("任课教师:", self.teacher)
#         print("上课地点:", self.__place)
#
# course1=course('10086','Python牛','宋帅哥','N666')
# course1.shown()
######3第六题
###方法一
# class int_vecter:
#     def __init__(self,list1):
#         self.__list2=list1
#         self.lens=len(list1)
#     def part(self,id):
#         return self.__list2[id:]
#     def all(self):
#         return self.__list2
#
# list1=eval(input('请输入一个整型数组:'))
# a=eval(input('请输入一个a:'))
# x=int_vecter(list1)
# print('显示全部数组数据:',x.all())
# print('显示从a开始的一段连续数组数据:',x.part(a))

###方法二
# class int_vecter:
#     def __init__(self,list1):
#         self.__list2=list1
#         self.lens=len(list1)
#     def part(self):
#         a = eval(input('请输入一个a:'))
#         print('显示从a开始的一段连续数组数据:',self.__list2[a:])
#     def all(self):
#         print('显示全部数组数据:',self.__list2)
#
# list1=eval(input('请输入一个整型数组:'))
# x=int_vecter(list1)
# x.all()
# x.part()
################实验七###########
###第一题
# try:
#     n=int(input("请输入一个整数:"))
#     x=100/n
#     print(x)
# except ValueError:
#     print("异常,请重新输入:")
# else:
#     print("程序正常运行,没有捕捉到异常")
###第二题
# try:
#     chars=["a","b",100,-37.2]
#     chars[5]="k"
# except IndexError:
#     print("产生该类型异常!")
# else:
#     print("产生该类型正常!")
###第三题
# try:
#     score=int(input("请输入该学生的成绩:"))
#     assert 0<=score<=100
#     if score>=90:
#         print("A优秀")
#     if 80<=score<90:
#         print("B良好")
#     if 60<=score<=80:
#         print("C合格")
#     if score<60:
#         print("D不及格")
# except ValueError:
#     print("ValueError:输入异常,输入必须为整数")
# except AssertionError:
#     print("AssertionError:输入异常,输入的成绩{}不在规定范围内".format(score))
# else:
#     print("程序正常运行,没有捕捉到异常")
###第四题
###方法一:
# import math
# class error(Exception):#自定义异常类,判断半径范围是否异常
#     def __init__(self, error_reason='异常,圆的半径不能为负数!'):
#         Exception.__init__(self, error_reason)
#
#
# class Circle(Exception):
#     def __init__(self,r):
#         self.r=r
#         if self.r < 0:
#             raise error
#     def getS(self):
#         area = math.pi *self.r *self.r
#         return area
# try:
#     r=float(input("请输入半径r:"))
#     c=Circle(r)
#     print("面积为:{:.2f}".format(c.getS()))
# except error as reason:
#     print(reason)
# else:
#     print("程序正常运行,没有捕捉到异常")
###方法二:
# class circle:
#         def __init__(self,x):
#             try:
#                 assert x>=0
#                 self.s=3.14*x*x
#             except AssertionError:
#                 print("用户自定义异常")
#             else:
#                 print("圆的面积为:{:.2f}".format(self.s))
# R=eval(input("请输入圆的半径r:"))
# yuan=circle(R)
###第五题
###方法一
# class highterror(Exception):
#     def __init__(self,h):
#         Exception.__init__(self)
#         self.H = h
#
#
# class jisuan:
#     def __init__(self,hight,weight):
#         self.hight = hight
#         self.weight = weight
#     def judge(self):
#         cha = self.weight - (self.hight-100)
#         biaozun = self.weight*0.05
#         if abs(cha) <= biaozun:
#             print('您的体重属于正常体重')
#         if cha > biaozun:
#             print('您的体重属于超重体重')
#         if cha < -biaozun:
#             print('您的体重属于不达标体重')
#
#
# try:
#     H = float(input('请输入您的身高:'))
#     W = float(input('请输入您的体重:'))
#     Judge = jisuan(H,W)
#     Judge.judge()
#     if H < 30 or H > 250:
#         raise highterror(H)
# except highterror as reason:
#     print(f'身高异常,您输入的身高为{reason.H}cm,身高范围在30cm到250cm之间')
# else:
#     print('输入正常')
# ###方法二
# class highterror(Exception):
#     def __init__(self,error_reason='身高异常,身高范围在30cm到250cm之间'):
#         Exception.__init__(self,error_reason)
#
#
# class jisuan:
#     def __init__(self,hight,weight):
#         self.hight = hight
#         self.weight = weight
#         if self.hight<30 or self.hight>250:
#             raise highterror
#     def judge(self):
#         cha = self.weight - (self.hight-100)
#         biaozun = self.weight*0.05
#         if abs(cha) <= biaozun:
#             print('您的体重属于正常体重')
#         if cha > biaozun:
#             print('您的体重属于超重体重')
#         if cha < -biaozun:
#             print('您的体重属于不达标体重')
#
#
# try:
#     H = float(input('请输入您的身高:'))
#     W = float(input('请输入您的体重:'))
#     Judge = jisuan(H,W)
#     Judge.judge()
# except highterror as reason:
#     print(reason)
# else:
#     print('输入正常')
###方法三
# class jisuan:
#     def __init__(self,hight,weight):
#         self.hight = hight
#         self.weight = weight
#         try:
#             assert 30<=self.hight<=250
#             cha = self.weight - (self.hight - 100)
#             biaozun = self.weight * 0.05
#             if abs(cha) <= biaozun:
#                 print('您的体重属于正常体重')
#             if cha > biaozun:
#                 print('您的体重属于超重体重')
#             if cha < -biaozun:
#                 print('您的体重属于不达标体重')
#         except AssertionError:
#             print(f"身高异常,您输入的身高为{self.hight}cm,身高范围在30cm到250cm之间")
#         else:
#             print('输入正常!')
#
#
# H = float(input('请输入您的身高:'))
# W = float(input('请输入您的体重:'))
# Judge = jisuan(H,W)
###第六题
###方法一
# import math
# class equationerror(Exception):
#     def __init__(self,error_reason='输入错误!判别式小于零,该方程无解!'):
#         Exception.__init__(self,error_reason)
#
#
# class equation:
#     def __init__(self,a,b,c):
#         self.a = a
#         self.b = b
#         self.c = c
#         self.d = (self.b)**2 -(4*self.a*self.c)
#         if self.d<0:
#             raise equationerror
#     def getx1(self):
#         return (-self.b + math.sqrt(self.d) )/ 2 * self.a
#     def getx2(self):
#         return (-self.b - math.sqrt(self.d) )/ 2 * self.a
#
# try:
#     A = float(input('请输入二次项系数a:'))
#     B = float(input('请输入二次项系数b:'))
#     C = float(input('请输入二次项系数c:'))
#     D = equation(A,B,C)
#     print("x1 = :{:.2f}".format(D.getx1()))
#     print("x2 = :{:.2f}".format(D.getx2()))
# except equationerror as reason:
#     print(reason)
# else:
#     print('输入正常')
###方法二
# import math
# class equationerror(Exception):
#     def __init__(self,a,b,c):
#         Exception.__init__(self)
#         self.a = a
#         self.b = b
#         self.c = c
#
#
# class equation:
#     def __init__(self,a,b,c):
#         self.a = a
#         self.b = b
#         self.c = c
#         self.d = (self.b)**2 -(4*self.a*self.c)
#         try:
#             assert self.d>=0
#             x1 = (-self.b + math.sqrt(self.d) )/ 2 * self.a
#             x2 = (-self.b - math.sqrt(self.d) )/ 2 * self.a
#             print("x1 = :{:.2f}".format(x1))
#             print("x2 = :{:.2f}".format(x2))
#         except AssertionError:
#             print('输入错误!判别式等于{:.2f},该方程无解!'.format(self.d))
#         else:
#             print('输入正常!')
#
# A = float(input('请输入二次项系数a:'))
# B = float(input('请输入二次项系数b:'))
# C = float(input('请输入二次项系数c:'))
# D = equation(A,B,C)

###方法三
# import math
# class equationerror(Exception):
#     def __init__(self,a,b,c):
#         Exception.__init__(self)
#         self.a = a
#         self.b = b
#         self.c = c
#         self.d = (self.b) ** 2 - (4 * self.a * self.c)
#
# class equation:
#     def __init__(self,a,b,c):
#         self.a = a
#         self.b = b
#         self.c = c
#         self.d = (self.b)**2 -(4*self.a*self.c)
#     def getd(self):
#         return self.d
#     def getx1(self):
#         return (-self.b + math.sqrt(self.d) )/ 2 * self.a
#     def getx2(self):
#         return (-self.b - math.sqrt(self.d) )/ 2 * self.a
#
# try:
#     A = float(input('请输入二次项系数a:'))
#     B = float(input('请输入二次项系数b:'))
#     C = float(input('请输入二次项系数c:'))
#     D = equation(A,B,C)
#     if D.getd()<=0:
#         raise equationerror(A,B,C)
#     print("x1 = :{:.2f}".format(D.getx1()))
#     print("x2 = :{:.2f}".format(D.getx2()))
# except equationerror as reason:
#     print('输入错误!判别式等于{:.2f},该方程无解!'.format(D.getd()))
# else:
#     print('输入正常')
print('nihap')

在这里插入图片描述

####实验八####
#第一题
# import random
# with open("suiji.txt", 'w+', encoding="utf-8") as pytxt:
#     for i in range(100000):
#         pytxt.write(str(random.randint(1,100)))
#         pytxt.write('\n')
# pytxt.close()

####第二题
# import csv
# headers = ['SNO', 'NAME', 'SCORE']
# rows = [('004', 'zhangqi', '98'),
#         ('005', 'lijiu', '95'),
#         ('006', 'wangyi', '92')]
#
# with open('tongji.csv', 'w', encoding='utf8', newline='') as f:
#     writer = csv.writer(f)
#     writer.writerow(headers)
#     writer.writerows(rows)
#
#
# print('tongji的csv文件内容为:')
# with open('tongji.csv', "r", encoding='utf8', newline='') as f:
# 	reader = csv.reader(f)
# 	for row in reader:
# 		print(row)
#
# with open("tongji.csv", 'r') as pycsv:
#     count1=0
#     count2=0
#     count3=0
#     for i in pycsv.read():
#         if i.isdigit():
#             count1+=1
#         if i.islower():
#             count2+=1
#         if i.isupper():
#             count3+=1
# print("数字出现次数:",count1)
# print("小写字母出现次数:",count2)
# print("大写字母出现次数:",count3)
#
# pycsv.close()

###第三题
# import csv
# import random
# headers = ['SNO', 'NAME', 'SCORE']
# rows = [('004', 'zhangqi', 98),
#         ('005', 'lijiu', 95),
#         ('006', 'wangyi', 92)]
# with open(r'./pingjun.csv','w',encoding='utf-8', newline='') as f:
#         w=csv.writer(f)
#         w.writerow(headers)
#         w.writerows(rows)
#
# sum=0
# n=0
# with open("pingjun.csv", 'r',encoding='utf-8') as f:
#     r=csv.reader(f,dialect='excel',delimiter=',')
#     not_head=next(r)
#     for i in r:
#         sum=sum+int(i[2])
#         n+=1
#         print(i)
#
# print(f'数值的总分为:{sum},平均分为{sum/n}')
# f.close()

###第四题
# with open(r'./test-row.txt','w',encoding='utf8') as f:
#     for i in range(10):
#         if i%2==0:
#             f.write('#nihao')
#         else:
#             f.write('没有#')
#         f.write('\n')
#
# with open(r'./test-row.txt','r',encoding='utf8') as f:
#     s = f.readlines()
#     for row in s:
#         if not row.startswith('#'):
#             print(row,end='')
###第五题
# import os
# oldname=os.listdir('./')
# os.chdir('./')
# for name in oldname:
#     os.rename(name,"文件操做-"+name)
###第六题
#
# with open(r'./yingwen.txt','w',encoding='utf-8') as f:
#     for i in range(5):
#         f.write('ABabYZ')
#         f.write('\n')
#
#
# with open("yingwen.txt","r") as f:
#     content=f.read()
#     print("原文件的内容:",'\n'+content)
#
# newstr = ''
# for i in content:
#     if i.islower():
#         if i == 'a' or i == 'b':
#             i = chr(ord(i) + 1)
#     if i.isupper():
#         if i == 'A' or i == 'B' or i == 'Y':
#             i = chr(ord(i) + 1)
#         elif i == 'Z':
#             i = chr(65)
#     newstr=''.join([newstr,i])
# print("修改后文件中的内容:", '\n' + newstr)
#
# with open("yingwen_new.txt", "w+") as fp:
#     fp.write(newstr)


####第七题

# import requests
# # url ='https://img12.360buyimg.com/n1/s450x450_jfs/t1/142383/17/13825/73504/5fa8b1c8E6a34854c/91c7d2bb5ea7f9aa.jpg'
# url = 'http://img10.360buyimg.com/n1/s450x450_jfs/t1/70270/22/10661/109832/5d834285E50d7c841/ad61e639c4fb32d5.jpg'
# response = requests.get(url).content
#
# with open(r'./文件操作-ad61e639c4fb32d5.jpg', 'wb') as f:
#         f.write(response)
# f.close()

在这里插入图片描述

在这里插入图片描述

########################实验十#####
#####第一题
# import numpy as np
# a = np.arange(15)
# print(a)
# b = a[5:11]
# print(b)

######第二题
####方法一
# import numpy as np
# a = np.arange(20)
# b = a.reshape(4,5)
# print('转换前:\n',b)
# b[[0,1]] = b[[1,0]]
# print('转换后:\n',b)

###方法二
# import  numpy as np
# a = np.arange(20).reshape(4,5)
# print('转换前:\n',a)
# x1 = np.split(a,4,axis=0)
# b = x1[0]
# x1[0] = x1[1]
# x1[1] = b
# print('转化后:\n',x1)


#####第三题
# ###方法一使用where
# import numpy as np
# import random
# a = np.random.randint(1,10,size=(5,5))
# print('原来的数组:\n',a)
# a[np.where(a%2==1)]=0
# print('变换后的数组:\n',a)
# ###方法二使用布尔索引
# import numpy as np
# import random
# a = np.random.randint(1,10,size=(5,5))
# print('原来的数组:\n',a)
# idx = np.where(a%2==1)
# a[idx] = 0
# print('变换后的数组:\n',idx)


####第四题
# import numpy as np
# np.random.seed(100)
# a = np.random.uniform(1,50,20)
# idx = (a > 30)
# a[idx] = 0
# print(a)
# c = np.argsort(-a)
# print('5的最大值的位置:',c[:5])


#####第五题
# import numpy as np
# np.random.seed(100)
# a = np.random.uniform(1,30,size=[6,10])
# print('原来的数组:\n',a)
# a1 = np.modf(a)
# a2 = a1[1]
# print('修改后的数组:\n',a2)
# b = np.unique(a2)
# print('去重后的数组:')
# for i in range(len(a2)):
#     for j in a2[[i],:]:
#         print(np.unique(a2[[i],:]))

######第六题
# import numpy as np
#
# np.random.seed(100)
# url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
# iris_2d = np.genfromtxt(url, delimiter=',', dtype='float')
# # print(iris_2d)
#
# # 1),随机使得20个数值为np.nan
# iris_2d[np.random.randint(0,150,size=20),np.random.randint(0,4,size=20)]=np.nan
###方法二
# a, b = np.where(iris_2d)  # 取出数据坐标,a对应行,b对应列
# iris_2d[np.random.choice((a), 20), np.random.choice((b), 20)] = np.nan
# print(iris_2d)

# # 2),输出第一列中缺失值个数和位置
# print("缺失值个数:", np.isnan(iris_2d[:, 0]).sum())
# print("缺失值位置:", np.where(np.isnan(iris_2d[:, 0])))

# # 3),过滤具有(第 3 列)>1.5和h(第 1 列)<5.0的iris_2d的行
# condition = (iris_2d[:, 2] > 1.5) & (iris_2d[:, 0] < 5.0)
# print(iris_2d[condition])

# # 4),选择没有 nan 值的 iris_2d 数组的行
# r = iris_2d[np.sum(np.isnan(iris_2d), axis = 1) == 0][:]
# print(r)
# ##方法二
# r = np.array([np.all(np.isnan(row)) for row in iris_2d])
# print(iris_2d[r][:])

# # 5)  找出数组 iris_2d 是否有缺失的值
# if np.isnan(iris_2d).any():
#     print('数组 iris_2d有缺失的值')

# 6)  在 numpy 数组中用 0 替换 nan
# iris_2d = np.where(np.isnan(iris_2d), 0, iris_2d)
# print('替换后的iris_2d:\n',iris_2d)
####方法二
# iris_2d[np.isnan(iris_2d)] = 0
# print(iris_2d)


# 7)  将 iris_2d(第 3 列)组成一个文本数组,<3 则为'小'3-5 则为'中''> = 5 则为'大';

# a_length= np.digitize(iris_2d[:, 2].astype('float'), [0, 3, 5, 10])
# label_map = {1: '小', 2: '中', 3: '大'}
# a_list = [label_map[x] for x in a_length]
# print(a_list)

# # 8)  在iris_2d中volume创建一个新列,其中volume是(pi x petallength x sepal_length ^ 2/ 3
# petallength = iris_2d[:, 2]
# sepallength = iris_2d[:, 0]
# volume = (np.pi * petallength * (sepallength ** 2)) / 3
# volume = volume[:, np.newaxis]
# new = np.hstack([iris_2d, volume])
# print(new[:20])

# # 9)  查找在 iris 数据集的第 4 列中第一次出现值大于 1.0的位置
# ids = np.argmax(iris_2d[:, 3].astype('float') > 1.0)
# print(ids)


#####第七题
# import numpy as np
# x1=np.array([1,9,5,12,8,20,4,10,2,8])
# x2=np.array([5,12,8,20,4,10,2,8,1,9])
# c1=np.sqrt(np.sum(np.square(x1-x2)))
# ###方法二
# c2=np.linalg.norm(x1-x2)
# print("该图形的周长为:{:.2f}".format(c1))
# print("方法二:\n该图形的周长为:{:.2f}".format(c2))

在这里插入图片描述

###########################实验十一########################
import pandas as pd
import numpy as np
import csv

data = {'animal':['cat','cat','snake','dog','dog','cat','snake','cat','dog','dog'],\
        'age': [2.5, 3, 0.5, np.nan, 5, 2, 4.5, np.nan, 7, 3],\
        'visits':[1,3,2,3,2,3,1,1,2,1],\
        'priority':['yes',np.nan,'no','yes','no','no','no','yes','no','no']}
labels = ['a','b','c','d','e','f','g','h','i','j']

##1)创建date_frame类型df

df=pd.DataFrame(data,index=labels,columns=data)
# print(df)

##2)输出 df 的前三行,并选择所有 visits 属性值大于 2 的所有行

print(df.iloc[0:3],"\n")
print(df.loc[df['visits'] > 2])

##3)输出 df 缺失值所在的行,输出'age''animal'两列数据

print(df[df.index.isnull()],'\n')
print(df[df.isnull().values ==True])
print(df[['age','animal']])

##4)输出animal == cat且age< 3的所有行,并将行为”f“列为"age"的值改为1.5

df1= df.loc[(df['animal']=='cat') & (df['age']<3)]
df.iloc[5,1]=1.5
print(df1,'\n')

##5)列出animal所有取值的出现的次数
sum=df.iloc[:,0].value_counts()
print(sum)

##6)将 animal 列中所有 snake 替换为 tangyudi
df['animal'] = df['animal'].replace('snake','tangyudi')
print(df['animal'])

#7)对 df 按列 animal 进行排序
print(df.sort_values(by = 'animal'))
print(df.sort_index(axis=1))

#8)在 df 的在后一列后添加一列列名为 No.数据 0,1,2,3,4,5,6,7,8,9
df['No.']=[0,1,2,3,4,5,6,7,8,9]
print(df)
num = pd.Series([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], index=df.index)
df['No.'] = num
print(df)

#9)对 df 中的'visits'列求平均值以及乘积、和

avg = df.visits.mean()
chenji = df.visits.sum()
sum =df.visits.prod()#cumprod()cumsum()不一样,累计的话输出每一项运算的结果
print(avg)
print(chenji)
print(sum)

#10)将 animal 对应的列中所有字符串字母变为大写
df2= df.animal.str.capitalize()
df['animal']= df.animal.str.upper()
print(df)


#11)利用浅复制方式创建 df 的副本 df2 并将其所有缺失值填充为 3

df2=df.copy()  #复制
df2=df2.fillna(3)
print(df2)

#12)利用浅复制方式创建 df 的副本 df3 并将其删除缺失值所在的行
df3=df.copy()
df3=df3.dropna() #删除含缺失值的列:(axis=1)
print(df3)

#13)将 df 写入 animal.csv 文件
df.to_csv("animal.csv")
# with open ("animal.csv","w+") as fp:
#     writer = csv.writer(fp)
#     writer.writerow(headers)
#     writer.writerows(rows)


##第二题
##1)列名为“Class”中取值分别将“negative”和“positive”替换为数字 01,并统计 01 各自出现的频数
dft = pd.read_csv('haberman-kmes.dat')
dft_class = dft[" Class"]
dft.loc[dft[' Class']==' negative',' Class'] = 0
dft.loc[dft[' Class']==' positive',' Class'] = 1
print(dft)
#2)创建df的副本df2,其中df2为除了df最后一列之外的所有列;
df2 = pd.DataFrame(dft,columns=['Age',' Year',' Positive'])
# df2.drop(labels=' Class', axis=1, inplace=True)
# print(df2)
#3)将 df2 的每一列数据进行归一化处理,即 \frac{x-x_{min}}{x_{max}-x_{min}} 其中 x 为列中的任一数据,x_{min},x_{max} 分别为列中所有数据的最大值和最小值;

df2 = df2.apply(lambda x: (x - np.min(x)) / (np.max(x) - np.min(x)))
# print(df2)

#4)计算 df2 行(样本或观测值)与行(样本或观测值)之间的欧式距离,并组成新的欧式距离数组 df3
from numpy.linalg import norm
x = norm(df2['Age'] - df2[" Year"])
# for i in range((df2['Age'].size+1)):
#      x.append(norm(df2.iloc[i] - df2.iloc[i+1]))
print(x)

#5)将 df3 中所有的行中的数据从小到大的顺序进行排序
df2 = df2.sort_values(by ='Age')#DataFrame.sort_values(by=‘##’,axis=0,ascending=True, inplace=False)
# print(df2)
######第三题
df = pd.read_csv('adult.dat')
# ##1) 删除该数据集中全部含有缺失值的行数据;
df.dropna(axis=0,how='any')
# ##2)删除该数据集中重复的行数据;
##df = df[~df.duplicated()]
df = df.drop_duplicates()
# # print(df)
# # print(df.isnull().sum())
# ##3)按照 class 字段将该数据进行分组,
# ##并计算各组中列分别 age, Education-num,Capital-gain, Capital-loss 和 Hours-per-week 的均值和方差,
# ##并计算其余各列中不重复元素的个数以及所占的比例
x = list(df.columns)
print(x)
y = ['Age',' Education-num',' Capital-gain',' Capital-loss',' Hours-p-week']
df1 = df.groupby(' Class')
for i in y:
        print("{}的平均值{}".format(i,df1[i].mean()))
for i in y:
        print("{}的方差{}".format(i,df1[i].var()))

for i in x:
        if i not in y:
                print("{0:}不同元素有{1:}个,所占比例:{2:.2f}%".format(i,df1[i].nunique().sum(),df1[i].nunique().sum()*100/df[i].size))


##4)将列 Age 字段取值划分为青年人(0-18)、中年人(19-45)、老年人 (45-100),
##并故根据该属性将该数据进行分组,然后计算各组中列分别 Education-num,Capital-gain, Capital-loss 和 Hours-per-week 的均值和方差,
##并计算其余各列中不重复元素的个数以及所占的比例。

print(df.columns)
a = [' Education-num',' Capital-gain',' Capital-loss',' Hours-p-week']
bins = [0,18,45,100]
labels = ['青年人', '中年人', '老年人']
cut_groups = pd.cut(df['Age'], bins=bins, right=False, labels=labels)
Age_groups =df.groupby(cut_groups)
Age_groups1 = df.groupby(cut_groups).count()
for i in a:
        print("{0:} 的平均值{1:2f}".format(i,Age_groups1[i].mean()))
for i in a:
        print("{0:} 的方差{1:2f}".format(i,Age_groups1[i].var()))

for i in x:
        if i not in a:
                print("{0:}不同元素有{1:}个,所占比例:{2:.2f}%".format(i,Age_groups[i].nunique().sum(),Age_groups[i].nunique().sum()*100/Age_groups1[i].sum()))


在这里插入图片描述

##############################实验十二##############


# import matplotlib.pyplot as plt
# import numpy as np
#
# plt.rcParams['font.sans-serif'] = ['SimHei']#中文乱码问题解决
######第一题,用点加线的方式画出 x=(0,10) 间 sin 的图像
# x = np.linspace(0, 10)
# y = np.sin(x)
# plt.plot(x, y, 'o-')  # 用点加线的方式画出x=(0,10)间sin的图像
# plt.show()

#######第二题, 利用以下数据分别制作水平和垂直柱状图
# x = [1,2,3,4,5,6,7,8]
# y = [3,1,4,5,8,9,7,2]
# label=['A','B','C','D','E','F','G','H']
# plt.barh(x,y,tick_label = label) #水平
# plt.show()
# plt.bar(x,y,tick_label = label)  #垂直
# plt.show()

#######第二题,自定义图表元素
##1) 绘制 x=(0,10)间 sin 的图像,设置线性为虚线
# x = np.linspace(0,10)
# y = np.sin(x)
# plt.plot(x,y,'--')
# ##2) 设置 y 轴显示范围为(-1.5,1.5)
# plt.ylim(-1.5,1.5)
# ##3)设置 x,y 轴标签分别为“variable x”,“value y”
# plt.xlabel('variable x')
# plt.ylabel('value y')
# ##4)设置图表标题“三角函数”
# plt.title('三角函数')
# ##5)显示网络
# plt.grid()
# ##6) 绘制平行于 x轴 y=0.8的水平参考线(提示:使用.axhline)
# plt.axhline(y = 0.8,ls='--',c='red')
# ##7)添加注释文字 sin(x)
# plt.text(1, 1, 'y=sin(x)',weight='bold', color='black')
# plt.show()

########第四题, 多子图
##在一个 10×10 的画布中,制作 2 个子图,分别显示 sin(x)cos(x)的图像,设置相同行和列共享 x,y 轴

# x = np.linspace(0,20,2000)
# y1 = np.sin(x)
# y2 = np.cos(x)
#
# figure,(ax1,ax2) = plt.subplots(2,1,figsize=(10,10),dpi=100,sharex=True,sharey=True)
# ax1.plot(x,y1,c='blue',linestyle='-')
# ax2.plot(x,y2,c='orange',linestyle='-')

# figure.subplots_adjust(hspace=0.1)

##方法二
# fig = plt.figure(figsize=(10,10))
# axe1 = fig.add_axes([0.5, 0.5, 0.4, 0.2], ylim=(-1.1, 1.1))
# axe2 = fig.add_axes([0.05, 0.5, 0.4, 0.2], ylim=(-1.1, 1.1))
# x = np.linspace(0,20)
# axe1.plot(np.sin(x))
# axe2.plot(np.cos(x))

##########第五题#######

# text = '''Hooray! It's snowing! It's time to make a snowman. \
# James runs out. He makes a big pile of snow. He puts a big snowball on top. \
# He adds a scarf and a hat. He adds an orange for the nose. \
# He adds coal for the eyes and buttons. In the evening, James opens the door. \
# What does he see ? The snowman is moving! James invites him in. \
# The snowman has never been inside a house. He says hello to the cat. \
# He plays with paper towels. A moment later, the snowman takes James's hand and goes out. \
# They go up, up, up into the air! They are flying! What a wonderful night! \
# The next morning, James jumps out of bed. He runs to the door. \
# He wants to thank the snowman. But he's gone.'''
#
# text = text.replace(',', '').replace('.', '').replace('!', '').replace('?', '')
# text = text.split()  # 默认会以空格,回车符,空格符等作为分割条件。
# date = {}
# words = []
# counts = []
# setword = set(text)
# for i in setword:
#     count = text.count(i)
#     words.append(i)
#     counts.append(count)
#
# date = dict(zip(words, counts))
#
# date = sorted(date.items(), key = lambda x:x[1], reverse=1)
#
# words5 = []
# count5 = []
# for i in range(5):
#     words5.append(date[i][0])
#     count5.append(date[i][1])
#
# plt.pie(np.array(count5), labels=words5, autopct='%1.1f%%')
# plt.title('WordsCount')
# plt.show()
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值