diff --git a/20407133-师艺南-计科2001班.html b/20407133-师艺南-计科2001班.html new file mode 100644 index 0000000..550a741 --- /dev/null +++ b/20407133-师艺南-计科2001班.html @@ -0,0 +1,15549 @@ + + +
+ + +result = 0
+factorial = 1
+for i in range(1, 21):
+ factorial *= i
+ result += factorial
+print(result)
+
2561327494111820313 ++
s = [9, 7, 8, 3, 2, 1, 55, 6]
+
+# 计算元素个数、最大值和最小值
+count = len(s)
+max_num = max(s)
+min_num = min(s)
+
+# 添加一个元素10
+s.append(10)
+
+# 删除一个元素55
+s.remove(55)
+
+# 输出结果
+print("列表s中元素的个数为:", count)
+print("列表s中的最大数为:", max_num)
+print("列表s中的最小数为:", min_num)
+print("添加元素后的列表s为:", s)
+
列表s中元素的个数为: 8 +列表s中的最大数为: 55 +列表s中的最小数为: 1 +添加元素后的列表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 +请输入第一个数字:782 +请输入第二个数字:712 +计算结果为: 556784.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
+import numpy as np
+
+# 定义数据
+x1 = [-3.00, -2.50, -1.75, -1.15, -0.50]
+y1 = [4, 12, 50, 120, 205]
+x2 = [0.15, 0.75, 1.25, 1.85, 2.45]
+y2 = [255, 170, 100, 20, 14]
+
+# 创建画布和子图对象
+fig, ax = plt.subplots()
+
+# 创建柱状图
+width = 0.35
+rects1 = ax.bar(x1, y1, width, label='Values 1')
+rects2 = ax.bar(x2, y2, width, label='Values 2')
+
+# 添加轴标签、标题和图例
+ax.set_xlabel('X Value')
+ax.set_ylabel('Y Value')
+ax.set_title('Comparison of Values in Different Categories')
+ax.set_xticks(x1 + x2)
+ax.legend()
+
+# 显示图形
+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
+from sklearn.model_selection import train_test_split
+from sklearn.linear_model import LinearRegression, Ridge, Lasso
+
+# 手动输入数据
+X = np.array([[7,26,6,60],
+ [1,29,15,52],
+ [11,56,8,20],
+ [11,31,8,47],
+ [7,52,6,33],
+ [11,55,9,22],
+ [3,71,17,6],
+ [1,31,22,44],
+ [2,54,18,22],
+ [21,47,4,26],
+ [1,40,23,34],
+ [11,66,9,12],
+ [10,68,8,12]])
+y = np.array([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(X, y, test_size=0.2, random_state=33)
+
+# 线性回归
+lr = LinearRegression()
+lr.fit(X_train, y_train)
+print('线性回归: w =', lr.coef_, ', b =', lr.intercept_)
+
+
+# 岭回归
+ridge = Ridge(alpha=1)
+ridge.fit(X_train, y_train)
+print('岭回归: w =', ridge.coef_, ', b =', ridge.intercept_)
+
+
+# Lasso回归
+lasso = Lasso(alpha=0.1)
+lasso.fit(X_train, y_train)
+print('Lasso回归: w =', lasso.coef_, ', b =', lasso.intercept_)
+
线性回归: w = [1.8345409 0.94625926 0.44872786 0.21458594] , b = 25.07192199710964 +岭回归: w = [ 1.57447024 0.68161575 0.18808347 -0.04549686] , b = 50.56897681218462 +Lasso回归: w = [ 1.61643393 0.72707903 0.22806763 -0. ] , b = 46.242690616035496 ++
注:训练集:测试集=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 = {"Age": ["<=30", "<=30", "31-40", ">40", ">40", ">40", "31-40", "<=30", "<=30", ">40", "<=30",
+ "31-40", "31-40", ">40"],
+ "Income": ["high", "high", "high", "medium", "low", "low", "low", "medium", "low", "medium",
+ "medium", "medium", "high", "medium"],
+ "Student": ["no", "no", "no", "no", "yes", "yes", "yes", "no", "yes", "yes", "yes", "no", "yes",
+ "no"],
+ "Credit_rating": ["mid", "high", "mid", "mid", "mid", "high", "high", "mid", "mid", "mid", "high",
+ "high", "mid", "high"],
+ "Buy_computer": ["no", "no", "yes", "yes", "yes", "no", "yes", "no", "yes", "yes", "yes", "yes",
+ "yes", "no"]
+ }
+
+# 将数据集转换为DataFrame格式
+df = pd.DataFrame(data)
+
+# 将字符特征转换成数值特征
+df["Age"] = df["Age"].map({"<=30": 0, "31-40": 1, ">40": 2})
+df["Income"] = df["Income"].map({"low": 0, "medium": 1, "high": 2})
+df["Student"] = df["Student"].map({"no": 0, "yes": 1})
+df["Credit_rating"] = df["Credit_rating"].map({"low": 0, "mid": 1, "high": 2})
+df["Buy_computer"] = df["Buy_computer"].map({"no": 0, "yes": 1})
+
+# 将数据集分割成训练集和测试集
+X_train, X_test, y_train, y_test = train_test_split(df.drop(columns=["Buy_computer"]), df["Buy_computer"], test_size=0.5, random_state=33)
+
+# 初始化模型
+gnb = GaussianNB()
+
+# 训练模型
+gnb.fit(X_train, y_train)
+
+# 预测测试集结果
+y_pred = gnb.predict(X_test)
+
+# 计算准确率
+score = accuracy_score(y_test, y_pred)
+
+# 输出预测结果、实际结果和模型得分
+print("Predict\tActual")
+for i in range(len(y_test)):
+ print("{}\t{}".format(y_pred[i], y_test.iloc[i]))
+print("Model score:", score)
+
Predict Actual +1 0 +1 1 +1 0 +1 1 +1 0 +1 1 +1 0 +Model score: 0.42857142857142855 ++
+