1.编程题# 将年龄段转为数值变量ydf[Your age].apply(lambdax:int(x.split( )[0]))# 假设年龄段为整数# 从标签文件中读取每一行并去除行首尾的空白字符得到类别名称列表 2分class_names[name.strip()fornameinopen(voc-model-labels.txt).readlines()]# 统计不同性别的平均车速、行驶距离和行驶时间 2分gender_statsdata.groupby(Gender)[[Speed,TravelDistance,TravelTime]].mean()print(不同性别的平均车速、行驶距离和行驶时间:\n,gender_stats)# 统计不同年龄段的驾驶员数 5分age_bins[18,26,36,46,56,66,np.inf]age_labels[18-25,26-35,36-45,46-55,56-65,65]data[AgeGroup]pd.cut(data[Age],binsage_bins,labelsage_labels,rightFalse)age_group_countsdata[AgeGroup].value_counts()print(不同年龄段的驾驶员数:\n,age_group_counts)# 修改列名data.rename(columns{病人ID:患者ID},inplaceTrue)# 将 horsepower 列中的所有值转换为数值类型df[horsepower]pd.to_numeric(df[horsepower],errorscoerce)Xpd.get_dummies(X)# 将分类变量转为数值变量img_datanp.expand_dims(img_data,axis1)# 添加 channel 维度# 处理数据不平衡smoteSMOTE(random_state42)X_resampled,y_resampledsmote.fit_resample(X_train,y_train)# 归一化 How do you describe your current level of fitness ? 列label_encoderLabelEncoder()data_cleaned[How do you describe your current level of fitness ?]label_encoder.fit_transform(data_cleaned[How do you describe your current level of fitness ?])#柱状图treatment_outcome_distribution.plot(kindbar,stackedTrue)#散点图plt.scatter(data[年龄]data[疾病严重程度])# 绘制饼图plt.figure(figsize(10,6))exercise_frequency_counts.plot.pie(autopct%1.1f%%,startangle90,colorsplt.cm.Paired.colors)plt.title(Distribution of Exercise Frequency)plt.ylabel()plt.show()# 合并处理后得数据并将其保存保存中不用额外创建索引cleaned_datapd.concat([X,y],axis1)cleaned_data.to_csv(2.1.2_cleaned_data.csv,indexFalse)# 合并处理后得数据并将其保存保存中不用额外创建索引cleaned_datapd.concat([X,y],axis1)cleaned_data.to_csv(2.1.2_cleaned_data.csv,indexFalse)# 初始化XGBoost模型设定树的数量为1000学习率为0.05每棵树的最大深度为5xgb_modelXGBRegressor(n_estimators1000,learning_rate0.05,max_depth5,subsample0.8,colsample_bytree0.8)# 获取预测的准确值转换为百分比prob_percentageaccuracy[0,predicted_idx]*100# 获取预测的类别标签predicted_labellabels[predicted_idx]# 应用 softmax 函数获取概率 2分probabilitiesscipy.special.softmax(output,axis-1)# 获取最高的5个概率和对应的类别索引 3分top5_idxnp.argsort(probabilities[0])[-5:][::-1]top5_probprobabilities[0][top5_idx]2.问答题