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

  • Deep Learning By Example
  • Ahmed Menshawy
  • 220字
  • 2021-06-24 18:52:45

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
主站蜘蛛池模板: 英德市| 阆中市| 大名县| 鄢陵县| 新邵县| 商河县| 江都市| 兰考县| 牡丹江市| 正镶白旗| 连南| 永善县| 蓬安县| 大厂| 秦皇岛市| 渝北区| 长海县| 高陵县| 平江县| 公安县| 丰宁| 武安市| 金门县| 大悟县| 诸暨市| 城口县| 黄龙县| 吉林市| 咸丰县| 扶风县| 宁强县| 思茅市| 革吉县| 六盘水市| 临沧市| 成都市| 盐源县| 康定县| 府谷县| 溧阳市| 苏尼特右旗|