From 28cf5813332c1eaebfe08d0901c7d688cfb86177 Mon Sep 17 00:00:00 2001 From: plivr5y4e <2510397137@qq.com> Date: Tue, 30 May 2023 21:39:31 +0800 Subject: [PATCH] ADD file via upload --- 20407115-呼芳利-计科2001班.html | 15514 ++++++++++++++++++++++++ 1 file changed, 15514 insertions(+) create mode 100644 20407115-呼芳利-计科2001班.html diff --git a/20407115-呼芳利-计科2001班.html b/20407115-呼芳利-计科2001班.html new file mode 100644 index 0000000..07ea52d --- /dev/null +++ b/20407115-呼芳利-计科2001班.html @@ -0,0 +1,15514 @@ + + +
+ + +def factorial(n):
+ if n == 0 or n == 1:
+ return 1
+ else:
+ return n * factorial(n-1)
+
+def sum_factorial(n):
+ if n == 1:
+ return 1
+ else:
+ return factorial(n) + sum_factorial(n-1)
+
+total = sum_factorial(20)
+print("1!+2!+3!+...+20! =", total)
+
1!+2!+3!+...+20! = 2561327494111820313 ++
s = [9, 7, 8, 3, 2, 1, 55, 6]
+
+print("元素个数:", len(s))
+print("最大数:", max(s))
+print("最小数:", min(s))
+
+s.append(10)
+s.remove(55)
+print("删除元素后的列表:", s)
+
元素个数: 8 +最大数: 55 +最小数: 1 +删除元素后的列表: [9, 7, 8, 3, 2, 1, 6, 10] ++
TTTTTx
+TTTTxx
+TTTxxx
+TTxxxx
+Txxxxx
+
+n = 5
+for i in range(n):
+ print('T'*(n-i-1) + 'x'*(i+1))
+
TTTTx +TTTxx +TTxxx +Txxxx +xxxxx ++
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):
+ if y == 0:
+ return "除数不能为0"
+ else:
+ return x / y
+
+print("选择要进行的运算:")
+print("1. 加法")
+print("2. 减法")
+print("3. 乘法")
+print("4. 除法")
+
+choice = input("请输入您的选择(1/2/3/4):")
+num1 = int(input("请输入第一个数字:"))
+num2 = int(input("请输入第二个数字:"))
+
+if choice == '1':
+ print(num1, "+", num2, "=", add(num1, num2))
+elif choice == '2':
+ print(num1, "-", num2, "=", subtract(num1, num2))
+elif choice == '3':
+ print(num1, "*", num2, "=", multiply(num1, num2))
+elif choice == '4':
+ print(num1, "/", num2, "=", divide(num1, num2))
+else:
+ print("非法输入")
+
选择要进行的运算: +1. 加法 +2. 减法 +3. 乘法 +4. 除法 +请输入您的选择(1/2/3/4):4 +请输入第一个数字:2 +请输入第二个数字:1 +2 / 1 = 2.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)}
+
+# 拆分数据
+x1, y1 = zip(*sorted([(x, y) for x, y in data if x < 0]))
+x2, y2 = zip(*sorted([(x, y) for x, y in data if x > 0]))
+
+# 绘图
+plt.bar(x1, y1, width=0.3, color='b', align='center')
+plt.bar(x2, y2, width=0.3, color='g', align='center')
+
+plt.xlabel('x')
+plt.ylabel('y')
+
+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
+import matplotlib.pyplot as plt
+from sklearn.model_selection import train_test_split
+from sklearn.linear_model import LinearRegression, Ridge, Lasso
+
+# 导入数据
+data = pd.DataFrame({
+ '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]
+})
+
+# 划分训练集和测试集
+X_train, X_test, y_train, y_test = train_test_split(data[['X1', 'X2', 'X3', 'X4']], data['Y'], test_size=0.2, random_state=15)
+
+# 线性回归
+lr_model = LinearRegression()
+lr_model.fit(X_train, y_train)
+print('线性回归:')
+print('w:', lr_model.intercept_)
+print('b:', lr_model.coef_)
+
+# 岭回归
+ridge_model = Ridge(alpha=1)
+ridge_model.fit(X_train, y_train)
+print('岭回归:')
+print('w:', ridge_model.intercept_)
+print('b:', ridge_model.coef_)
+
+# Lasso回归
+lasso_model = Lasso(alpha=1)
+lasso_model.fit(X_train, y_train)
+print('Lasso回归:')
+print('w:', lasso_model.intercept_)
+print('b:', lasso_model.coef_)
+
线性回归: +w: 180.60992642787454 +b: [ 0.26655739 -0.64835198 -1.35191198 -1.32541508] +岭回归: +w: 144.59851372290143 +b: [ 0.63515406 -0.28031503 -0.95811354 -0.96262718] +Lasso回归: +w: 116.94963304605514 +b: [ 0.90611156 0. -0.64062776 -0.68340473] ++
注:训练集:测试集=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 GaussianNB
+from sklearn import preprocessing
+from sklearn.model_selection import train_test_split
+
+# 读取数据集
+data = pd.DataFrame({
+ '年龄': ['<=30', '<=30', '31-40', '>40', '>40', '>40', '31-40', '<=30', '<=30', '>40', '<=30', '31-40', '31-40', '>40'],
+ '收入': ['高', '高', '高', '中', '低', '低', '低', '中', '低', '中', '中', '中', '高', '中'],
+ '是否为学生': ['否', '否', '否', '否', '是', '是', '是', '否', '是', '是', '是', '否', '是', '否'],
+ '信誉': ['中', '优', '中', '中', '中', '优', '优', '中', '中', '中', '优', '优', '中', '优'],
+ '购买计算机': ['否', '否', '是', '是', '是', '否', '是', '否', '是', '是', '是', '是', '是', '否']
+})
+
+# 对数据进行编码
+le = preprocessing.LabelEncoder()
+data['年龄'] = le.fit_transform(data['年龄'])
+data['收入'] = le.fit_transform(data['收入'])
+data['是否为学生'] = le.fit_transform(data['是否为学生'])
+data['信誉'] = le.fit_transform(data['信誉'])
+data['购买计算机'] = le.fit_transform(data['购买计算机'])
+
+# 划分训练集和测试集
+X = data.iloc[:, :-1]
+y = data.iloc[:, -1]
+X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=15)
+
+# 建立朴素贝叶斯模型
+gnb = GaussianNB()
+gnb.fit(X_train, y_train)
+
+# 预测测试集
+y_pred = gnb.predict(X_test)
+
+# 输出预测结果、实际结果和模型得分
+print('预测结果:', y_pred)
+print('实际结果:', y_test.values)
+print('模型得分:', gnb.score(X_test, y_test))
+
预测结果: [0 1 1 1 0 1 1] +实际结果: [0 1 1 1 0 1 1] +模型得分: 1.0 ++
+