from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
import pandas as pd
data = pd.read_excel('data1-1.xlsx')
data = data.drop(columns=['编号'])
prediction_set = data[data['婴儿行为特征'].isna()]
training_set = data.dropna()
features = ['母亲年龄', '婚姻状况', '教育程度', '妊娠时间(周数)', '分娩方式', 'CBTS', 'EPDS', 'HADS']
X = training_set[features]
y_behavior = training_set['婴儿行为特征']
X_train_behavior_class, X_test_behavior_class, y_train_behavior_class, y_test_behavior_class = train_test_split(
X, y_behavior, test_size=0.2, random_state=42)
model_behavior_class = RandomForestClassifier(random_state=42)
model_behavior_class.fit(X_train_behavior_class, y_train_behavior_class)
y_pred_behavior_class = model_behavior_class.predict(X_test_behavior_class)
y_pred_behavior_class = y_pred_behavior_class.astype(int)
X_pred = prediction_set[features]
y_pred_behavior_class_prediction_set = model_behavior_class.predict(X_pred)
y_pred_behavior_class_prediction_set = y_pred_behavior_class_prediction_set.astype(
int)
print(y_pred_behavior_class_prediction_set)
随机森林分类器预测
于 2023-08-09 13:25:25 首次发布