diff --git a/20407119-亢世帆-计科2001班.html b/20407119-亢世帆-计科2001班.html new file mode 100644 index 0000000..2984df6 --- /dev/null +++ b/20407119-亢世帆-计科2001班.html @@ -0,0 +1,15543 @@ + + +
+ + +total = 0
+factorial = 1
+for i in range(1, 21):
+ factorial *= i
+ total += factorial
+print(total)
+
2561327494111820313 ++
s = [9, 7, 8, 3, 2, 1, 55, 6]
+
+count = len(s) # 计算s中元素的个数
+maximum = max(s) # 计算s中的最大值
+minimum = min(s) # 计算s中的最小值
+
+s.append(10) # 在s中添加一个元素10
+s.remove(55) # 从s中删除一个元素55
+
+print("元素个数:", count)
+print("最大值:", maximum)
+print("最小值:", minimum)
+print("添加10后的列表s:", s)
+
元素个数: 8 +最大值: 55 +最小值: 1 +添加10后的列表s: [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):
+ 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):3 +请输入第一个数字:61 +请输入第二个数字:3 +计算结果为: 183.0 ++
class Student:
+ def __init__(self, name, age, course):
+ self.name = name
+ self.age = age
+ self.course = course
+
+ def get_name(self):
+ return str(self.name)
+
+ def get_age(self):
+ return int(self.age)
+
+ def get_course(self):
+ return int(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 和 Y 值
+x1_values = [-3.00, -2.50, -1.75, -1.15, -0.50]
+y1_values = [4, 12, 50, 120, 205]
+
+x2_values = [0.15, 0.75, 1.25, 1.85, 2.45]
+y2_values = [255, 170, 100, 20, 14]
+
+# 绘制图形
+fig, ax = plt.subplots()
+ax.bar(x1_values, y1_values, width=0.3, color='blue')
+ax.bar(x2_values, y2_values, width=0.3, color='green')
+ax.set_xlabel('X')
+ax.set_ylabel('Y')
+ax.set_title('柱状图')
+plt.show()
+
E:\Anaconda3\lib\site-packages\IPython\core\pylabtools.py:151: UserWarning: Glyph 26609 (\N{CJK UNIFIED IDEOGRAPH-67F1}) missing from current font. + fig.canvas.print_figure(bytes_io, **kw) +E:\Anaconda3\lib\site-packages\IPython\core\pylabtools.py:151: UserWarning: Glyph 29366 (\N{CJK UNIFIED IDEOGRAPH-72B6}) missing from current font. + fig.canvas.print_figure(bytes_io, **kw) +E:\Anaconda3\lib\site-packages\IPython\core\pylabtools.py:151: UserWarning: Glyph 22270 (\N{CJK UNIFIED IDEOGRAPH-56FE}) missing from current font. + fig.canvas.print_figure(bytes_io, **kw) ++
注:训练集:测试集=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
+from sklearn.metrics import mean_squared_error
+
+# 将数据存储到 Pandas DataFrame
+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]}
+df = pd.DataFrame(data)
+
+# 分割训练集和测试集
+X = df.iloc[:, :-1] # 提取所有的特征变量
+Y = df['Y'] # 提取输出变量
+
+X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=19)
+
+# 线性回归
+lr = LinearRegression()
+lr.fit(X_train, y_train)
+y_pred_lr = lr.predict(X_test)
+w_lr = lr.coef_
+b_lr = lr.intercept_
+mse_lr = mean_squared_error(y_test, y_pred_lr)
+
+# 岭回归
+ridge = Ridge(alpha=1)
+ridge.fit(X_train, y_train)
+y_pred_ridge = ridge.predict(X_test)
+w_ridge = ridge.coef_
+b_ridge = ridge.intercept_
+mse_ridge = mean_squared_error(y_test, y_pred_ridge)
+
+# Lasso回归
+lasso = Lasso(alpha=0.1)
+lasso.fit(X_train, y_train)
+y_pred_lasso = lasso.predict(X_test)
+w_lasso = lasso.coef_
+b_lasso = lasso.intercept_
+mse_lasso = mean_squared_error(y_test, y_pred_lasso)
+
+# 输出各个回归的 w 和 b 系数
+print('线性回归:w=', w_lr, ', b=', b_lr)
+print('岭回归: w=', w_ridge, ', b=', b_ridge)
+print('lasso回归:w=', w_lasso, ', b=', b_lasso)
+
线性回归:w= [ 1.37914915 0.52235563 -0.11353673 -0.16566386] , b= 66.18042444982297 +岭回归: w= [ 1.21471328 0.39359214 -0.26743013 -0.29337994] , b= 79.1913312989731 +lasso回归:w= [ 1.405279 0.55172639 -0.08210026 -0.13686687] , b= 63.3405231601846 ++
注:训练集:测试集=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.model_selection import train_test_split
+from sklearn.naive_bayes import GaussianNB
+from sklearn.metrics import accuracy_score
+
+# 读取数据
+data = {'年龄': ['<=30', '<=30', '31-40', '>40', '>40', '>40', '31-40', '<=30', '<=30', '>40', '<=30', '31-40', '31-40', '>40'],
+ '收入': ['高', '高', '高', '中', '低', '低', '低', '中', '低', '中', '中', '中', '高', '中'],
+ '是否为学生': ['否', '否', '否', '否', '是', '是', '是', '否', '是', '是', '是', '否', '是', '否'],
+ '信誉': ['中', '优', '中', '中', '中', '优', '优', '中', '中', '中', '优', '优', '中', '优'],
+ '购买计算机': ['否', '否', '是', '是', '是', '否', '是', '否', '是', '是', '是', '是', '是', '否']}
+
+df = pd.DataFrame(data)
+
+# 将特征转化为数字编码
+df['年龄'] = df['年龄'].map({'<=30': 0, '31-40': 1, '>40': 2})
+df['收入'] = df['收入'].map({'低': 0, '中': 1, '高': 2})
+df['是否为学生'] = df['是否为学生'].map({'否': 0, '是': 1})
+df['信誉'] = df['信誉'].map({'中': 0, '优': 1})
+df['购买计算机'] = df['购买计算机'].map({'否': 0, '是': 1})
+
+# 分割训练集和测试集
+X = df.iloc[:, :-1] # 特征变量
+y = df.iloc[:, -1] # 输出变量
+
+X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=19)
+
+# 创建模型并进行训练
+gnb = GaussianNB()
+gnb.fit(X_train, y_train)
+
+# 使用模型进行预测
+y_pred = gnb.predict(X_test)
+
+# 输出预测结果、实际结果和模型得分
+print('预测结果:', y_pred)
+print('实际结果:', y_test.values)
+print('模型得分:', accuracy_score(y_test, y_pred))
+
预测结果: [1 1 1 1 1 1 1] +实际结果: [1 1 1 0 1 0 0] +模型得分: 0.5714285714285714 ++
+