diff --git a/20407227 吴玉强 计科2002.html b/20407227 吴玉强 计科2002.html new file mode 100644 index 0000000..57d8a28 --- /dev/null +++ b/20407227 吴玉强 计科2002.html @@ -0,0 +1,15536 @@ + + +
+ + +# your code
+n=0
+s=0
+t=1
+for n in range(1,21):
+ t*=n
+ s+=t
+print("1!+2!+3!+……+20!=%d"%s)
+
1!+2!+3!+……+20!=2561327494111820313 ++
# your code
+def choose(s):
+ sum = 0
+ all = 0
+ maxnum = max(s)
+ minnum = min(s)
+ for i in s:
+ sum = sum + 1
+ print(str("元素个数{0},最大值{1},最小值{2}").format(sum, maxnum, minnum,))
+def main():
+ s = [9,7,8,3,2,1,55,6]
+ choose(s)
+main()
+s = [9, 7, 8, 3, 2, 1, 55, 6]
+
+# 添加元素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
+
+# your code
+for i in range(5):
+ for j in range(5 - i):
+ print("T", end="")
+ for k in range(i+1):
+ print("x", end="")
+ print()
+
TTTTTx +TTTTxx +TTTxxx +TTxxxx +Txxxxx ++
# your code
+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':
+ 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':
+ if num2 == 0:
+ print("除数不能为0")
+ else:
+ print(num1, "/", num2, "=", divide(num1, num2))
+else:
+ print("非法输入")
+
请选择功能: +1. 加法 +2. 减法 +3. 乘法 +4. 除法 +68.0 - 34.0 = 34.0 ++
# your code
+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 | +
# your code
+import plotly.io as pio
+pio.renderers.default = 'iframe_connected'
+import matplotlib.pyplot as plt
+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()
+ax.bar(x1, y1, width=0.3, align='edge', label='X-Y1')
+ax.bar(x2, y2, width=-0.3, align='edge', label='X-Y2')
+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 | +
# your code
+import numpy as np
+import pandas as pd
+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 = data.iloc[:, :-1]
+Y = data.iloc[:, -1]
+# 分割训练集和测试集
+X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=27)
+# 线性回归
+lr = LinearRegression()
+lr.fit(X_train, Y_train)
+print("线性回归:")
+print("系数:", lr.coef_)
+print("截距:", lr.intercept_)
+print("训练集得分:", lr.score(X_train, Y_train))
+print("测试集得分:", lr.score(X_test, Y_test))
+# 岭回归
+ridge = Ridge(alpha=1.0)
+ridge.fit(X_train, Y_train)
+print("\n岭回归:")
+print("系数:", ridge.coef_)
+print("截距:", ridge.intercept_)
+print("训练集得分:", ridge.score(X_train, Y_train))
+print("测试集得分:", ridge.score(X_test, Y_test))
+# Lasso回归
+lasso = Lasso(alpha=0.1)
+lasso.fit(X_train, Y_train)
+print("\nLasso回归:")
+print("系数:", lasso.coef_)
+print("截距:", lasso.intercept_)
+print("训练集得分:", lasso.score(X_train, Y_train))
+print("测试集得分:", lasso.score(X_test, Y_test))
+
线性回归: +系数: [1.93112493 1.168554 0.55044472 0.41963723] +截距: 6.607111440049607 +训练集得分: 0.9878573641308772 +测试集得分: 0.400937866938589 + +岭回归: +系数: [1.63846223 0.86160385 0.25159327 0.12417071] +截距: 35.875248491621804 +训练集得分: 0.9875037022516316 +测试集得分: 0.49152089175412883 + +Lasso回归: +系数: [1.55111046 0.76149286 0.15902803 0.02850176] +截距: 45.27515860791298 +训练集得分: 0.9872489467898224 +测试集得分: 0.5256639300276629 ++
注:训练集:测试集=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 | +中 | +否 | +优 | +否 | +
# your code
+import numpy as np
+import pandas as pd
+from sklearn.model_selection import train_test_split
+from sklearn.naive_bayes import GaussianNB
+# 读入数据
+data = pd.DataFrame({
+ '年龄': ['<=30', '<=30', '31-40', '>40', '>40', '>40', '31-40', '<=30', '<=30', '>40', '<=30', '31-40', '31-40', '>40'],
+ '收入': ['高', '高', '高', '中', '低', '低', '低', '中', '低', '中', '中', '中', '高', '中'],
+ '是否为学生': ['否', '否', '否', '否', '是', '是', '是', '否', '是', '是', '是', '否', '是', '否'],
+ '信誉': ['中', '优', '中', '中', '中', '优', '优', '中', '中', '中', '优', '优', '中', '优'],
+ '购买计算机': ['否', '否', '是', '是', '是', '否', '是', '否', '是', '是', '是', '是', '是', '否']
+})
+# 将文本特征转换为数值特征
+data = data.replace({'年龄': {'<=30':1, '31-40':2, '>40':3},
+ '收入': {'低':1, '中':2, '高':3},
+ '是否为学生': {'否':0, '是':1},
+ '信誉': {'中':1, '优':0},
+ '购买计算机': {'否':0, '是':1}})
+# 数据预处理
+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=27)
+# 朴素贝叶斯
+gnb = GaussianNB()
+gnb.fit(X_train, Y_train)
+# 预测
+Y_pred = gnb.predict(X_test)
+# 计算得分
+score = gnb.score(X_test, Y_test)
+# 输出结果
+print("预测结果:", Y_pred)
+print("实际结果:", np.array(Y_test))
+print("模型得分:", score)
+
预测结果: [1 1 1 1 0 0 0] +实际结果: [0 1 0 1 1 1 0] +模型得分: 0.42857142857142855 ++
+