# your code
# 定义变量和初始值
n = 1
sum = 0
# 循环计算每项的值
while n <= 20:
# 计算当前项的阶乘
factorial = 1
for i in range(1, n + 1):
factorial *= i
# 将当前项加入总和
sum += factorial
# 更新 n 的值
n += 1
# 输出结果
print('1! + 2! + 3! + ... + 20! =', sum)
1! + 2! + 3! + ... + 20! = 2561327494111820313
# your code
s = [9, 7, 8, 3, 2, 1, 55, 6]
print('列表s的元素个数为:', len(s))
print('列表s的最大值为:', max(s))
print('列表s的最小值为:', min(s))
s.append(10)
s.remove(55)
print('操作后的列表s为:', s)
列表s的元素个数为: 8 列表s的最大值为: 55 列表s的最小值为: 1 操作后的列表s为: [9, 7, 8, 3, 2, 1, 6, 10]
TTTTTx
TTTTxx
TTTxxx
TTxxxx
Txxxxx
# your code
n = 6
x = 'x'
T = 'T'
# 循环打印每一行
for i in range(n):
# 打印每一行的 T
for j in range(n - i - 1):
print(T, end='')
# 打印每一行的 x
for k in range(i + 1):
print(x, end='')
# 换行
print()
TTTTTx TTTTxx TTTxxx TTxxxx Txxxxx xxxxxx
# your code
def Add(a,b):
return a+b
def Sub(a,b):
return a-b
def Mul(a,b):
return a*b
def Div(a,b):
if(b==0):
print("Error!")
return
return a/b
while True:
Choice = input("Choice:")
if(Choice == '0'):
break
a = int(input("a:"))
b = int(input("b:"))
if(Choice == '1'):
print(Add(a,b))
elif(Choice == '2'):
print(Sub(a,b))
elif(Choice == '3'):
print(Mul(a,b))
elif(Choice == '4'):
print(Div(a,b))
Choice:1 a:2 b:3 5
--------------------------------------------------------------------------- KeyboardInterrupt Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_11956\659078927.py in <module> 12 return a/b 13 while True: ---> 14 Choice = input("Choice:") 15 if(Choice == '0'): 16 break ~\anaconda3\lib\site-packages\ipykernel\kernelbase.py in raw_input(self, prompt) 1175 "raw_input was called, but this frontend does not support input requests." 1176 ) -> 1177 return self._input_request( 1178 str(prompt), 1179 self._parent_ident["shell"], ~\anaconda3\lib\site-packages\ipykernel\kernelbase.py in _input_request(self, prompt, ident, parent, password) 1217 except KeyboardInterrupt: 1218 # re-raise KeyboardInterrupt, to truncate traceback -> 1219 raise KeyboardInterrupt("Interrupted by user") from None 1220 except Exception: 1221 self.log.warning("Invalid Message:", exc_info=True) KeyboardInterrupt: Interrupted by user
# your code
class Student:
def __init__(self, name, age, courses):
self.name = name
self.age = age
self.courses = courses
def __str__(self):
info = '姓名:' + self.name + '\n'
info += '年龄:' + str(self.age) + '\n'
max_score = max(self.courses)
info += '最高分数:' + str(max_score)
return info
# 实例化学生对象并测试
st = Student('zhangming', 20, [69, 88, 100])
# 输出学生信息
print(st)
姓名: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]
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);
注:训练集:测试集=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
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, 13],
'X4': [60, 52, 20, 47, 33, 22, 6, 44, 22, 26, 34, 22, 22],
'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=45)
# 线性回归模型
linear_model = LinearRegression()
linear_model.fit(X_train, y_train)
# 输出线性回归的 w 和 b 系数
print('线性回归 w:', linear_model.coef_)
print('线性回归 b:', linear_model.intercept_)
# 岭回归模型
ridge_model = Ridge(alpha=1.0)
ridge_model.fit(X_train, y_train)
# 输出岭回归的 w 和 b 系数
print('岭回归 w:', ridge_model.coef_)
print('岭回归 b:', ridge_model.intercept_)
# Lasso 回归模型
lasso_model = Lasso(alpha=1.0)
lasso_model.fit(X_train, y_train)
# 输出 Lasso 回归的 w 和 b 系数
print('Lasso 回归 w:', lasso_model.coef_)
print('Lasso 回归 b:', lasso_model.intercept_)
线性回归 w: [ 1.5764689 0.50856464 0.17271624 -0.13227528] 线性回归 b: 61.50607264818487 岭回归 w: [ 1.54422831 0.50705187 0.15123212 -0.13682296] 岭回归 b: 62.20569483922765 Lasso 回归 w: [ 1.33296796 0.50248893 0. -0.1587709 ] Lasso 回归 b: 66.39529542615554
注:训练集:测试集=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 import metrics
# 导入高斯朴素贝叶斯分类器
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import train_test_split
x = np.array(
[
[1, 3, 0, 1, 0],
[1, 3, 0, 2, 1],
[2, 3, 0, 2, 1],
[3, 2, 0, 1, 1],
[3, 1, 1, 1, 1],
[3, 1, 1, 2, 0],
[2, 1, 1, 2, 1],
[1, 2, 0, 1, 0],
[1, 1, 1, 1, 1],
[3, 2, 1, 1, 1],
[1, 2, 1, 2, 1],
[2, 2, 0, 2, 1],
[2, 3, 1, 1, 1],
[3, 2, 0, 2, 0],
]
)
y = np.array(
[
0,1,1,1,1,0,1,0,1,1,1,1,1,0
]
)
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.5, random_state=45
)
# 使用高斯朴素贝叶斯进行计算
clf = GaussianNB()
clf.fit(X_train, y_train)
# 评估
y_predict = clf.predict(X_test)
score_gnb = metrics.accuracy_score(y_predict,y_test)
print('该用户是否购买计算机:',y_predict)
print(y_test)
print(score_gnb)
该用户是否购买计算机: [1 1 1 1 1 0 0] [1 1 1 1 1 0 0] 1.0