官术网_书友最值得收藏!

Using a regression or another simple model to predict the values of missing variables

This is the approach that we will use for the Age feature of the Titanic example. The Age feature is an important step towards predicting the survival of passengers, and applying the previous approach by taking the mean will make us lose some information.

In order to predict the missing values, you need to use a supervised learning algorithm that takes the available features as input and the available values of the feature that you want to predict for its missing value as output. In the following code snippet, we are using the random forest classifier to predict the missing values of the Age feature:

# Define a helper function that can use RandomForestClassifier for handling the missing values of the age variable
def set_missing_ages():
global df_titanic_data

age_data = df_titanic_data[
['Age', 'Embarked', 'Fare', 'Parch', 'SibSp', 'Title_id', 'Pclass', 'Names', 'CabinLetter']]
input_values_RF = age_data.loc[(df_titanic_data.Age.notnull())].values[:, 1::]
target_values_RF = age_data.loc[(df_titanic_data.Age.notnull())].values[:, 0]

# Creating an object from the random forest regression function of sklearn<use the documentation for more details>
regressor = RandomForestRegressor(n_estimators=2000, n_jobs=-1)

# building the model based on the input values and target values above
regressor.fit(input_values_RF, target_values_RF)

# using the trained model to predict the missing values
predicted_ages = regressor.predict(age_data.loc[(df_titanic_data.Age.isnull())].values[:, 1::])

    # Filling the predicted ages in the original titanic dataframe
age_data.loc[(age_data.Age.isnull()), 'Age'] = predicted_ages
主站蜘蛛池模板: 陇南市| 甘孜县| 丹东市| 理塘县| 枣强县| 哈尔滨市| 莱州市| 济南市| 桑日县| 和田县| 阜新| 和平区| 农安县| 来凤县| 南宫市| 无锡市| 谷城县| 汤阴县| 龙门县| 田林县| 叶城县| 镇远县| 道孚县| 即墨市| 北宁市| 布尔津县| 揭阳市| 凤城市| 修文县| 福清市| 巴马| 岑溪市| 贵溪市| 和田县| 全椒县| 齐齐哈尔市| 泾川县| 焉耆| 奉化市| 温泉县| 齐齐哈尔市|