n = int(input('请输入所求阶乘数:'))
m = 1
sum = 0
i = 1
while n >= i:
m *= i
sum += m
i = i + 1
print("结果:",sum)
请输入所求阶乘数:20 结果: 2561327494111820313
s = [9,7,8,3,2,1,55,6]
print("length =",len(s)," max =",max(s)," min =",min(s))
s.append(10)
s.remove(55)
print(s)
length = 8 max = 55 min = 1 [9, 7, 8, 3, 2, 1, 6, 10]
TTTTTx
TTTTxx
TTTxxx
TTxxxx
Txxxxx
T = 'T'
x = 'x'
length = 6
for i in range(1, length):
print(T * (length - i) + x * i)
TTTTTx TTTTxx TTTxxx TTxxxx Txxxxx
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):1 请输入第一个数字:11 请输入第二个数字:22 11 + 22 = 33
class Student:
def __init__(self,name,age,*cou):
self.name = name
self.age = age
self.course = cou
def get_name(self):
return self.name
def get_age(self):
return self.age
def get_course(self):
return max(max(self.course))
st = Student('zhangming',20,[69,88,100])
print('学生姓名为:',st.get_name(),' 年龄为:',st.get_age(),' 最高分成绩为:',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]
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 |
import random
import numpy as np
from sklearn.linear_model import LinearRegression, Ridge, Lasso
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
# 读取数据
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])
# 数据预处理:标准化
mean = X.mean(axis=0)
std = X.std(axis=0)
X = (X - mean) / std
# 划分训练集和测试集
random.seed(53)
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=53)
# 线性回归
lr = LinearRegression()
lr.fit(X_train, Y_train)
Y_pred_lr = lr.predict(X_test)
mse_lr = mean_squared_error(Y_test, Y_pred_lr)
w_lr = lr.coef_
b_lr = lr.intercept_
print("线性回归: w=", w_lr, " b=", b_lr)
# 岭回归
ridge = Ridge(alpha=1.0)
ridge.fit(X_train, Y_train)
Y_pred_ridge = ridge.predict(X_test)
mse_ridge = mean_squared_error(Y_test, Y_pred_ridge)
w_ridge = ridge.coef_
b_ridge = ridge.intercept_
print("岭回归: w=", w_ridge, " b=", b_ridge)
# Lasso回归
lasso = Lasso(alpha=1.0)
lasso.fit(X_train, Y_train)
Y_pred_lasso = lasso.predict(X_test)
mse_lasso = mean_squared_error(Y_test, Y_pred_lasso)
w_lasso = lasso.coef_
b_lasso = lasso.intercept_
print("Lasso回归: w=", w_lasso, " b=", b_lasso)
线性回归: w= [10.75570087 11.32743024 2.29941907 1.70246304] b= 95.18721576543119 岭回归: w= [ 6.65496191 4.12252258 -1.6041754 -5.50750257] b= 95.51266373021079 Lasso回归: w= [ 7.87006935 5.47359453 -0. -3.50852623] b= 95.5908361231588
注:训练集:测试集=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 | 中 | 否 | 优 | 否 |
from sklearn.naive_bayes import CategoricalNB
from sklearn.model_selection import train_test_split
import pandas as pd
# 数据读取
data = pd.DataFrame({'Age': ['<=30', '<=30', '31-40', '>40', '>40', '>40', '31-40', '<=30', '<=30', '>40', '<=30', '31-40', '31-40', '>40'],
'Income': ['高', '高', '高', '中', '低', '低', '低', '中', '低', '中', '中', '中', '高', '中'],
'Student': ['否', '否', '否', '否', '是', '是', '是', '否', '是', '是', '是', '否', '是', '否'],
'Credit_rating': ['中', '优', '中', '中', '中', '优', '优', '中', '中', '中', '优', '优', '中', '优'],
'Buy_computer': ['否', '否', '是', '是', '是', '否', '是', '否', '是', '是', '是', '是', '是', '否']})
# 特征和标签分离
X = data.iloc[:, :-1]
y = data.iloc[:, -1]
# 将特征转换为数值类型
X = pd.get_dummies(X)
# 切分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=53)
# CategoricalNB分类器训练
clf = CategoricalNB()
clf.fit(X_train, y_train)
# 预测
y_pred = clf.predict(X_test)
# 输出预测结果、实际结果以及模型得分
print('预测结果:', list(y_pred))
print('实际结果:', list(y_test))
print('模型得分:', clf.score(X_test, y_test))
预测结果: ['否', '否', '否', '是', '否', '否', '否'] 实际结果: ['是', '否', '是', '是', '否', '否', '是'] 模型得分: 0.5714285714285714