From 7c8fc751809a8652b753de19632bc1d9bc15eed2 Mon Sep 17 00:00:00 2001 From: pq5abxrt4 <2316909265@qq.com> Date: Tue, 30 May 2023 21:28:54 +0800 Subject: [PATCH] ADD file via upload --- 20407148-张利红-计科2001班.html | 15543 ++++++++++++++++++++++++ 1 file changed, 15543 insertions(+) create mode 100644 20407148-张利红-计科2001班.html diff --git a/20407148-张利红-计科2001班.html b/20407148-张利红-计科2001班.html new file mode 100644 index 0000000..d358b97 --- /dev/null +++ b/20407148-张利红-计科2001班.html @@ -0,0 +1,15543 @@ + + +
+ + +# 计算n的阶乘
+def factorial(n):
+ if n == 1:
+ return 1
+ else:
+ return n * factorial(n-1)
+
+# 计算1! + 2! + 3! + ... + 20!
+total = 0
+for i in range(1, 21):
+ total += factorial(i)
+
+print(total)
+
2561327494111820313 ++
s = [9,7,8,3,2,1,55,6]
+
+# 元素个数
+print("元素个数:", len(s))
+
+# 最大值
+print("最大值:", max(s))
+
+# 最小值
+print("最小值:", min(s))
+
+# 添加元素10
+s.append(10)
+print("添加元素10后的列表:", s)
+
+# 删除元素55
+s.remove(55)
+print("删除元素55后的列表:", s)
+
元素个数: 8 +最大值: 55 +最小值: 1 +添加元素10后的列表: [9, 7, 8, 3, 2, 1, 55, 6, 10] +删除元素55后的列表: [9, 7, 8, 3, 2, 1, 6, 10] ++
TTTTTx
+TTTTxx
+TTTxxx
+TTxxxx
+Txxxxx
+
+for i in range(5):
+ for j in range(5-i):
+ print("T", end="")
+ for k in range(i):
+ print("x", end="")
+ print()
+
TTTTT +TTTTx +TTTxx +TTxxx +Txxxx ++
# 定义加法函数
+def add(x, y):
+ return x + y
+
+# 定义减法函数
+def subtract(x, y):
+ return x - y
+
+# 定义乘法函数
+def multiply(x, y):
+ return x * y
+
+# 定义除法函数
+def divide(x, y):
+ return x / y
+
+# 打印菜单提示信息
+print("请选择要进行的计算:")
+print("1. 加法")
+print("2. 减法")
+print("3. 乘法")
+print("4. 除法")
+
+# 获取用户选择
+choice = input("请输入计算的序号(1/2/3/4):")
+
+# 获取用户输入的数字
+num1 = float(input("请输入第一个数字:"))
+num2 = float(input("请输入第二个数字:"))
+
+# 根据用户选择调用相应的函数进行计算
+if choice == '1':
+ result = add(num1, num2)
+elif choice == '2':
+ result = subtract(num1, num2)
+elif choice == '3':
+ result = multiply(num1, num2)
+elif choice == '4':
+ result = divide(num1, num2)
+else:
+ print("输入的计算序号不正确!")
+
+# 输出计算结果
+print("计算结果为:", result)
+
请选择要进行的计算: +1. 加法 +2. 减法 +3. 乘法 +4. 除法 +请输入计算的序号(1/2/3/4):2 +请输入第一个数字:98 +请输入第二个数字:45 +计算结果为: 53.0 ++
class Student:
+ def __init__(self, name, age, course):
+ self.name = name
+ self.age = age
+ self.course = course
+
+ def get_name(self):
+ return self.name
+
+ def get_age(self):
+ return self.age
+
+ def get_course(self):
+ return max(self.course)
+
+st = Student('zhangming', 20, [69, 88, 100])
+print("姓名:", st.get_name())
+print("年龄:", st.get_age())
+print("最高分数:", st.get_course())
+
姓名: zhangming +年龄: 20 +最高分数: 100 ++
X | +Y | +X | +Y | +
---|---|---|---|
-3.00 | +4 | +0.15 | +255 | +
-2.50 | +12 | +0.75 | +170 | +
-1.75 | +50 | +1.25 | +100 | +
-1.15 | +120 | +1.85 | +20 | +
-0.50 | +205 | +2.45 | +14 | +
import matplotlib.pyplot as plt
+
+# 数据
+data = [(-3.00, 4), (-2.50, 12), (-1.75, 50), (-1.15, 120),
+ (-0.50, 205), (0.15, 255), (0.75, 170), (1.25, 100),
+ (1.85, 20), (2.45, 14)]
+
+# 将数据拆分为 x 和 y 坐标
+x = [i[0] for i in data]
+y = [i[1] for i in data]
+
+# 绘制条形图
+plt.bar(x, y)
+
+# 设置标题和坐标轴标签
+plt.title("Bar Chart")
+plt.xlabel("X Axis")
+plt.ylabel("Y Axis")
+
+# 显示图形
+plt.show()
+
注:训练集:测试集=8:2,随机种子采用你学号后两位,例如你学号后两位=01,则random_state=1,如果最后两位=34,则random_state=34。最终结果打印出各个回归的w和b系数即可。
+序号 | +X1 | +X2 | +X3 | +X4 | +Y | +
---|---|---|---|---|---|
1 | +7 | +26 | +6 | +60 | +78.5 | +
2 | +1 | +29 | +15 | +52 | +74.3 | +
3 | +11 | +56 | +8 | +20 | +104.3 | +
4 | +11 | +31 | +8 | +47 | +87.6 | +
5 | +7 | +52 | +6 | +33 | +95.9 | +
6 | +11 | +55 | +9 | +22 | +109.2 | +
7 | +3 | +71 | +17 | +6 | +102.7 | +
8 | +1 | +31 | +22 | +44 | +72.5 | +
9 | +2 | +54 | +18 | +22 | +93.1 | +
10 | +21 | +47 | +4 | +26 | +115.9 | +
11 | +1 | +40 | +23 | +34 | +83.8 | +
12 | +11 | +66 | +9 | +12 | +113.3 | +
13 | +10 | +68 | +8 | +12 | +109.4 | +
import numpy as np
+import pandas as pd
+from sklearn.linear_model import LinearRegression, Ridge, Lasso
+from sklearn.model_selection import train_test_split
+
+# 数据
+data = {'X1': [7, 1, 11, 11, 7, 11, 3, 1, 2, 21, 1, 11, 10],
+ 'X2': [26, 29, 56, 31, 52, 55, 71, 31, 54, 47, 40, 66, 68],
+ 'X3': [6, 15, 8, 8, 6, 9, 17, 22, 18, 4, 23, 9, 8],
+ 'X4': [60, 52, 20, 47, 33, 22, 6, 44, 22, 26, 34, 12, 12],
+ 'Y': [78.5, 74.3, 104.3, 87.6, 95.9, 109.2, 102.7, 72.5, 93.1, 115.9, 83.8, 113.3, 109.4]}
+
+# 转为 DataFrame
+df = pd.DataFrame(data)
+
+# 分隔特征和目标
+X = df.drop('Y', axis=1)
+Y = df['Y']
+
+# 拆分数据集
+train_X, test_X, train_Y, test_Y = train_test_split(X, Y, test_size=0.2, random_state=48)
+
+# 线性回归
+lr = LinearRegression()
+lr.fit(train_X, train_Y)
+print('===线性回归===')
+print('w:', lr.coef_)
+print('b:', lr.intercept_)
+
+# 岭回归
+ridge = Ridge(alpha=1.0)
+ridge.fit(train_X, train_Y)
+print('===岭回归===')
+print('w:', ridge.coef_)
+print('b:', ridge.intercept_)
+
+# Lasso 回归
+lasso = Lasso(alpha=0.1)
+lasso.fit(train_X, train_Y)
+print('===Lasso 回归===')
+print('w:', lasso.coef_)
+print('b:', lasso.intercept_)
+
===线性回归=== +w: [ 1.14735687 0.21239976 -0.32800606 -0.42396249] +b: 92.37823019043876 +===岭回归=== +w: [ 1.09637836 0.16865872 -0.37832815 -0.46696953] +b: 96.73786398238221 +===Lasso 回归=== +w: [ 1.1833755 0.24890043 -0.28746408 -0.38801264] +b: 88.80275594629103 ++
注:训练集:测试集=1:1,随机种子采用你学号后两位,例如你学号后两位=01,则random_state=1,如果最后两位=34,则random_state=34。最终结果输出你预测结果、实际结果以及模型得分三项。
+序号 | +年龄 | +收入 | +是否为学生 | +信誉 | +购买计算机 | +
---|---|---|---|---|---|
1 | +<=30 | +高 | +否 | +中 | +否 | +
2 | +<=30 | +高 | +否 | +优 | +否 | +
3 | +31-40 | +高 | +否 | +中 | +是 | +
4 | +>40 | +中 | +否 | +中 | +是 | +
5 | +>40 | +低 | +是 | +中 | +是 | +
6 | +>40 | +低 | +是 | +优 | +否 | +
7 | +31-40 | +低 | +是 | +优 | +是 | +
8 | +<=30 | +中 | +否 | +中 | +否 | +
9 | +<=30 | +低 | +是 | +中 | +是 | +
10 | +>40 | +中 | +是 | +中 | +是 | +
11 | +<=30 | +中 | +是 | +优 | +是 | +
12 | +31-40 | +中 | +否 | +优 | +是 | +
13 | +31-40 | +高 | +是 | +中 | +是 | +
14 | +>40 | +中 | +否 | +优 | +否 | +
import pandas as pd
+from sklearn.naive_bayes import CategoricalNB
+from sklearn.model_selection import train_test_split
+
+# 读取数据
+data = {'年龄': ['<=30', '<=30', '31-40', '>40', '>40', '>40', '31-40', '<=30', '<=30', '>40', '<=30', '31-40', '31-40', '>40'],
+ '收入': ['高', '高', '高', '中', '低', '低', '低', '中', '低', '中', '中', '中', '高', '中'],
+ '是否为学生': ['否', '否', '否', '否', '是', '是', '是', '否', '是', '是', '是', '否', '是', '否'],
+ '信誉': ['中', '优', '中', '中', '中', '优', '优', '中', '中', '中', '优', '优', '中', '优'],
+ '购买计算机': ['否', '否', '是', '是', '是', '否', '是', '否', '是', '是', '是', '是', '是', '否']}
+
+df = pd.DataFrame(data)
+
+# 将字符串转换为数字
+df['年龄'] = pd.factorize(df['年龄'])[0]
+df['收入'] = pd.factorize(df['收入'])[0]
+df['是否为学生'] = pd.factorize(df['是否为学生'])[0]
+df['信誉'] = pd.factorize(df['信誉'])[0]
+df['购买计算机'] = pd.factorize(df['购买计算机'])[0]
+
+# 分隔特征和目标变量
+X = df.drop('购买计算机', axis=1)
+Y = df['购买计算机']
+
+# 拆分数据集
+train_X, test_X, train_Y, test_Y = train_test_split(X, Y, test_size=0.5, random_state=48)
+
+# 模型训练和预测
+nb = CategoricalNB()
+nb.fit(train_X, train_Y)
+predicted_Y = nb.predict(test_X)
+
+# 预测结果、实际结果和得分
+print('预测结果:', predicted_Y)
+print('实际结果:', test_Y.values)
+print('模型得分:', nb.score(test_X, test_Y))
+
预测结果: [1 0 1 1 0 0 0] +实际结果: [0 1 1 1 1 1 0] +模型得分: 0.42857142857142855 ++
+