diff --git a/20407103-崔婕-计科2001.html b/20407103-崔婕-计科2001.html new file mode 100644 index 0000000..e228ecd --- /dev/null +++ b/20407103-崔婕-计科2001.html @@ -0,0 +1,15503 @@ + + +
+ + +# your code
+n = int(input('请输入所求阶乘数:'))
+m = 1
+sum = 0
+i = 1
+while n >= i:
+ m *= i
+ sum += m
+ i = i + 1
+print("结果:",sum)
+
请输入所求阶乘数:20 +结果: 2561327494111820313 ++
# your code
+s = [9, 7, 8, 3, 2, 1, 55, 6]
+
+# 求列表元素个数
+count = len(s)
+print('元素个数为:', count)
+
+# 求列表最大值和最小值
+max_num = max(s)
+min_num = min(s)
+print('最大数为:', max_num)
+print('最小数为:', min_num)
+
+# 向列表中添加元素 10
+s.append(10)
+print('添加元素后的列表为:', s)
+
+# 从列表中删除元素 55
+s.remove(55)
+print('删除元素后的列表为:', s)
+
元素个数为: 8 +最大数为: 55 +最小数为: 1 +添加元素后的列表为: [9, 7, 8, 3, 2, 1, 55, 6, 10] +删除元素后的列表为: [9, 7, 8, 3, 2, 1, 6, 10] ++
TTTTTx
+TTTTxx
+TTTxxx
+TTxxxx
+Txxxxx
+
+# your code
+for i in range(0,5):
+ #通过与i相关联,实现T字符的打印
+ for t in range(0,5-i):
+ print("T",end="")
+ for x in range(5-i,6):
+ 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.加法 2.减法 3.乘法 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-4的数字!")
+
在下列功能中选择: +1.加法 2.减法 3.乘法 4.除法 +请输入对应功能项的数字(1.2.3.4):2 +请输入第一个数字:3 +请输入第二个数字:5 +3 - 5 = -2 ++
# 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 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]
+plt.bar(x,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 | +
# your code
+import pandas as pd
+import numpy as np
+from sklearn.model_selection import train_test_split
+from sklearn.linear_model import LinearRegression, Ridge, Lasso
+
+# 将数据读取为 data frame 格式
+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[['X1', 'X2', 'X3', 'X4']]
+y = data[['Y']]
+
+# 将训练集和测试集按 8:2 分割,随机种子为学号后两位
+X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=3)
+
+# 线性回归模型
+linear_model = LinearRegression()
+linear_model.fit(X_train, y_train)
+print('线性回归:')
+print('w=', linear_model.coef_)
+print('b=', linear_model.intercept_)
+print('score=', linear_model.score(X_test, y_test))
+
+# 岭回归模型
+ridge_model = Ridge(alpha=1.0)
+ridge_model.fit(X_train, y_train)
+print('\n岭回归:')
+print('w=', ridge_model.coef_)
+print('b=', ridge_model.intercept_)
+print('score=', ridge_model.score(X_test, y_test))
+
+# LASSO 回归模型
+lasso_model = Lasso(alpha=1.0)
+lasso_model.fit(X_train, y_train)
+print('\nLASSO 回归:')
+print('w=', lasso_model.coef_)
+print('b=', lasso_model.intercept_)
+print('score=', lasso_model.score(X_test, y_test))
+
线性回归: +w= [[1.75145805 0.6933228 0.31811437 0.00410042]] +b= [45.05304232] +score= 0.9746366372530989 + +岭回归: +w= [[ 1.54896781 0.50558787 0.11651444 -0.1790819 ]] +b= [63.48864447] +score= 0.9774060129462268 + +LASSO 回归: +w= [ 1.4111527 0.39810105 -0. -0.28208021] +b= [74.1882953] +score= 0.9800239277857916 ++
注:训练集:测试集=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 pandas as pd
+from sklearn.model_selection import train_test_split
+from sklearn.naive_bayes import GaussianNB
+from sklearn.metrics import accuracy_score
+
+# 读取原始数据并创建数据框
+data = pd.DataFrame({
+ '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': ['fair', 'excellent', 'fair', 'fair', 'fair', 'excellent', 'excellent', 'fair', 'fair', 'fair', 'excellent', 'excellent', 'fair', 'excellent'],
+ 'Computer_purchased': ['no', 'no', 'yes', 'yes', 'yes', 'no', 'yes', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'no']
+})
+
+# 将标称属性转为数值型
+data = data.replace({'Age': {'<=30': 0, '31-40': 1, '>40': 2}})
+data = data.replace({'Income': {'low': 0, 'medium': 1, 'high': 2}})
+data = data.replace({'Student': {'no': 0, 'yes': 1}})
+data = data.replace({'Credit_rating': {'fair': 0, 'excellent': 1}})
+data = data.replace({'Computer_purchased': {'no': 0, 'yes': 1}})
+
+# 分离出自变量和因变量
+X = data[['Age', 'Income', 'Student', 'Credit_rating']]
+y = data[['Computer_purchased']]
+
+# 将训练集和测试集按 1:1 分割,随机种子为学号后两位
+X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=3)
+
+# 创建高斯朴素贝叶斯分类器并进行训练
+gnb_model = GaussianNB()
+gnb_model.fit(X_train, y_train.values.ravel())
+
+# 输出预测结果、实际结果和模型得分
+y_pred = gnb_model.predict(X_test)
+print('预测结果:', y_pred.tolist())
+print('实际结果:', y_test.values.ravel().tolist())
+print('模型得分:', accuracy_score(y_test, y_pred))
+
预测结果: [1, 1, 1, 1, 1, 1, 1] +实际结果: [0, 1, 0, 1, 0, 1, 0] +模型得分: 0.42857142857142855 ++