From b759ef495e7f99f1481186bc47ab21e2bc531cdb Mon Sep 17 00:00:00 2001 From: p2xcvkjew <2240818509@qq.com> Date: Sun, 28 May 2023 20:09:56 +0800 Subject: [PATCH] ADD file via upload --- 20407137-宋立群-计科2001班.html | 15552 ++++++++++++++++++++++++ 1 file changed, 15552 insertions(+) create mode 100644 20407137-宋立群-计科2001班.html diff --git a/20407137-宋立群-计科2001班.html b/20407137-宋立群-计科2001班.html new file mode 100644 index 0000000..50d8572 --- /dev/null +++ b/20407137-宋立群-计科2001班.html @@ -0,0 +1,15552 @@ + + +
+ + +# 计算 1!+2!+3!+...20! 的和
+
+# 定义一个变量用于存储总和
+total = 0
+
+# 循环计算每个数的阶乘并累加到总和中
+for i in range(1, 21):
+ factorial = 1
+ for j in range(1, i + 1):
+ factorial *= j
+ total += factorial
+
+# 输出结果
+print(total)
+
2561327494111820313 ++
# 定义列表
+s = [9, 7, 8, 3, 2, 1, 55, 6]
+
+# 求元素个数、最大值和最小值
+count = len(s)
+maximum = max(s)
+minimum = min(s)
+print("元素个数:", count)
+print("最大数:", maximum)
+print("最小数:", minimum)
+
+# 添加元素10并删除元素55
+s.append(10)
+s.remove(55)
+print("添加元素10后的列表为:", s)
+
元素个数: 8 +最大数: 55 +最小数: 1 +添加元素10后的列表为: [9, 7, 8, 3, 2, 1, 6, 10] ++
TTTTTx
+TTTTxx
+TTTxxx
+TTxxxx
+Txxxxx
+
+# 外层循环控制行数
+T = 'T'
+x = 'x'
+length = 6
+for i in range(1, length):
+ print(T * (length - i) + x * i)
+
TTTTTx +TTTTxx +TTTxxx +TTxxxx +Txxxxx ++
# 定义加法函数
+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:
+ print("除数不能为零!")
+ return None
+ else:
+ 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':
+ 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':
+ result = divide(num1, num2)
+ if result is not None:
+ print(num1, "/", num2, "=", result)
+else:
+ print("抱歉,您输入的功能序号有误,请重新运行程序。")
+
请选择您需要的功能: +1. 加法 +2. 减法 +3. 乘法 +4. 除法 +请输入要使用的功能序号(1/2/3/4):3 +请输入第一个数字:1 +请输入第二个数字:37 +1.0 * 37.0 = 37.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
+
+# 手动输入数据
+x = [-3.00, -2.50, -1.75, -1.15, -0.50, 0.15, 0.75, 1.25, 1.85, 2.45]
+y = [4, 12, 50, 120, 205, 255, 170, 100, 20, 14]
+label=[-3.00,-2.50,-1.75,-1.15,-0.50,0.15,0.75,1.25,1.85,2.45]
+
+# 绘制柱状图
+plt.bar(x,y,tick_label = label);
+
+# 显示图形
+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 pandas as pd
+from sklearn.linear_model import LinearRegression, Ridge, Lasso
+from sklearn.model_selection import train_test_split
+
+# 读取数据
+data = pd.DataFrame(
+ [[7, 26, 6, 60, 78.5],
+ [1, 29, 15, 52, 74.3],
+ [11, 56, 8, 20, 104.3],
+ [11, 31, 8, 47, 87.6],
+ [7, 52, 6, 33, 95.9],
+ [11, 55, 9, 22, 109.2],
+ [3, 71, 17, 6, 102.7],
+ [1, 31, 22, 44, 72.5],
+ [2, 54, 18, 22, 93.1],
+ [21, 47, 4, 26, 115.9],
+ [1, 40, 23, 34, 83.8],
+ [11, 66, 9, 12, 113.3],
+ [10, 68, 8, 12, 109.4]],
+ columns=['X1', 'X2', 'X3', 'X4', 'Y']
+)
+
+# 将数据分为训练集和测试集
+seed = 37 # 以学号后两位为随机种子
+train_data, test_data = train_test_split(data, test_size=0.2, random_state=seed)
+
+# 线性回归
+linreg = LinearRegression()
+linreg.fit(train_data[['X1', 'X2', 'X3', 'X4']], train_data['Y'])
+
+print('=====线性回归=====')
+print('W:', linreg.coef_)
+print('b:', linreg.intercept_)
+print('Train R^2 Score:', linreg.score(train_data[['X1', 'X2', 'X3', 'X4']], train_data['Y']))
+print('Test R^2 Score:', linreg.score(test_data[['X1', 'X2', 'X3', 'X4']], test_data['Y']))
+
+# 岭回归
+ridge = Ridge(alpha=0.1)
+ridge.fit(train_data[['X1', 'X2', 'X3', 'X4']], train_data['Y'])
+
+print('\n=====岭回归=====')
+print('W:', ridge.coef_)
+print('b:', ridge.intercept_)
+print('Train R^2 Score:', ridge.score(train_data[['X1', 'X2', 'X3', 'X4']], train_data['Y']))
+print('Test R^2 Score:', ridge.score(test_data[['X1', 'X2', 'X3', 'X4']], test_data['Y']))
+
+# Lasso回归
+lasso = Lasso(alpha=0.1)
+lasso.fit(train_data[['X1', 'X2', 'X3', 'X4']], train_data['Y'])
+
+print('\n=====Lasso回归=====')
+print('W:', lasso.coef_)
+print('b:', lasso.intercept_)
+print('Train R^2 Score:', lasso.score(train_data[['X1', 'X2', 'X3', 'X4']], train_data['Y']))
+print('Test R^2 Score:', lasso.score(test_data[['X1', 'X2', 'X3', 'X4']], test_data['Y']))
+
=====线性回归===== +W: [1.98745297 0.7125071 0.60342221 0.09234506] +b: 36.81325582050998 +Train R^2 Score: 0.9832895760371373 +Test R^2 Score: 0.9620380437756655 + +=====岭回归===== +W: [1.94480909 0.67978818 0.56291831 0.05946734] +b: 40.15978044847303 +Train R^2 Score: 0.9832822368230552 +Test R^2 Score: 0.9628744421890784 + +=====Lasso回归===== +W: [ 1.7448631 0.53538928 0.37348389 -0.08626773] +b: 55.156351700868306 +Train R^2 Score: 0.9830535584966494 +Test R^2 Score: 0.9669995284733632 ++
注:训练集:测试集=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 | +中 | +否 | +优 | +否 | +
from sklearn.naive_bayes import CategoricalNB
+from sklearn.model_selection import train_test_split
+import pandas as pd
+
+# 数据读取
+data = pd.DataFrame({'年龄': ['<=30', '<=30', '31-40', '>40', '>40', '>40', '31-40', '<=30', '<=30', '>40', '<=30', '31-40', '31-40', '>40'],
+ '收入': ['高', '高', '高', '中', '低', '低', '低', '中', '低', '中', '中', '中', '高', '中'],
+ '是否为学生': ['否', '否', '否', '否', '是', '是', '是', '否', '是', '是', '是', '否', '是', '否'],
+ '信誉': ['中', '优', '中', '中', '中', '优', '优', '中', '中', '中', '优', '优', '中', '优'],
+ '购买计算机': ['否', '否', '是', '是', '是', '否', '是', '否', '是', '是', '是', '是', '是', '否']})
+
+# 特征和标签分离
+X = data.iloc[:, :-1]
+y = data.iloc[:, -1]
+
+# 将特征转换为数值类型
+X = pd.get_dummies(X)
+
+# 数据集划分
+random_state = 37 # 这里以学号后两位的值作为随机种子
+X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=random_state)
+
+# 定义模型,训练
+clf = CategoricalNB()
+clf.fit(X_train, y_train)
+
+# 预测
+y_pred = clf.predict(X_test)
+
+# 输出预测结果、实际结果以及模型得分
+print('预测结果:', list(y_pred))
+print('实际结果:', list(y_test))
+print('模型得分:', clf.score(X_test, y_test))
+
预测结果: ['是', '是', '是', '是', '是', '是', '是'] +实际结果: ['否', '是', '是', '否', '否', '是', '是'] +模型得分: 0.5714285714285714 ++
+