第2关:数据预处理
print("""Index(['playoffs', 'shot_made_flag', 'dist', 'angle', 'remaining_time',
'action_type_Alley Oop Dunk Shot', 'action_type_Alley Oop Layup shot',
'action_type_Cutting Finger Roll Layup Shot',
'action_type_Cutting Layup Shot', 'action_type_Driving Bank shot',
...
'season_2006--07', 'season_2007--08', 'season_2008--09',
'season_2009--10', 'season_2010--11', 'season_2011--12',
'season_2012--13', 'season_2013--14', 'season_2014--15',
'season_2015--16'],
dtype='object', length=130) """)
第3关:构造模型并调参
def student():
pass
print("""n_estimators:100
max_depth:10""")
第4关:参数验证
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore")
def visual(range_n, scores_n, range_m, scores_m):
# 创建一个大小为10x5的图形
plt.figure(figsize=(10, 5))
# 绘制 n_estimators 的折线图
plt.subplot(1, 2, 1) # 1行2列的第1个子图
plt.plot(range_n, scores_n)
plt.xlabel('number of trees')
plt.ylabel('score')
# 绘制 max_depth 的折线图
plt.subplot(1, 2, 2) # 1行2列的第2个子图
plt.plot(range_m, scores_m)
plt.xlabel('max depth')
plt.ylabel('score')
# 调整子图之间的间距
# 保存图形到指定路径
plt.savefig('task4/result/pict1.jpg')
plt.close() # 关闭图形,释放资源
# 示例数据(实际使用时请替换为真实的取值范围和分数)
range_n = [10, 50, 100, 150, 200]
scores_n = [0.75, 0.80, 0.85, 0.84, 0.83]
range_m = [3, 5, 7, 9, 11]
scores_m = [0.78, 0.82, 0.84, 0.83, 0.81]
# 调用函数绘制图形
visual(range_n, scores_n, range_m, scores_m)
第5关:预测结果
import numpy as np
import pandas as pd
import scipy as sp
import warnings
warnings.filterwarnings("ignore") # 忽略警告
with warnings.catch_warnings():
warnings.filterwarnings("ignore",category=DeprecationWarning)
from numpy.core.umath_tests import inner1d
from sklearn.ensemble import RandomForestClassifier
def student(n_estimators,max_depth):
# ********* Begin *********#
raw = pd.read_csv("task5/data.csv",index_col=0)
df = raw[pd.notnull(raw['shot_made_flag'])]
submission = raw[pd.isnull(raw['shot_made_flag'])]
submission = submission.drop('shot_made_flag', 1)
train = df.drop('shot_made_flag', 1)
train_y = df['shot_made_flag']
model = RandomForestClassifier(n_estimators=n_estimators, max_depth=max_depth)
model.fit(train, train_y)
pred = model.predict_proba(submission)
# ********* End *********#
return pred
都是实测的答案,若实在不会可以私信我,我可以代做(五米/关)