练习 - 清理天气数据以分析火箭发射条件

已完成

现在我们已导了数据,接下来需要应用一种称为“清理数据”的机器学习实践。我们获取看起来不正确或杂乱的数据,并通过更改值或完全将其删除来对它进行清理。 清理数据的常见示例有:

  • 确保没有 null 值
  • 使列中的每个值看起来都相同

我们清理数据是因为如果计算机看到不一致的数据或数据中有大量值为 null,会扰乱计算机的操作。

数据清理

清理数据的第一步是将所有缺失值替换为其他内容。 替换这些值通常需要行业专业知识。 但在本例中,你需要使用自己的最佳判断。 某些行缺少天气或发射数据(请记住,行表示的是日期)。

若要开始,请先通过在 .ipynb 文件中运行以下命令来获取发射数据的概述:

launch_data.info()

在 300 行中,有部分列的信息缺失:

RangeIndex: 300 entries, 0 to 299
Data columns (total 26 columns):
 #   Column                        Non-Null Count  Dtype         
---  ------                        --------------  -----         
 0   Name                          60 non-null     object        
 1   Date                          300 non-null    datetime64[ns]
 2   Time (East Coast)             59 non-null     object        
 3   Location                      300 non-null    object        
 4   Crewed or Uncrewed            60 non-null     object        
 5   Launched?                     60 non-null     object        
 6   High Temp                     299 non-null    float64       
 7   Low Temp                      299 non-null    float64       
 8   Ave Temp                      299 non-null    float64       
 9   Temp at Launch Time           59 non-null     float64       
 10  Hist High Temp                299 non-null    float64       
 11  Hist Low Temp                 299 non-null    float64       
 12  Hist Ave Temp                 299 non-null    float64       
 13  Precipitation at Launch Time  299 non-null    float64       
 14  Hist Ave Precipitation        299 non-null    float64       
 15  Wind Direction                299 non-null    object        
 16  Max Wind Speed                299 non-null    float64       
 17  Visibility                    299 non-null    float64       
 18  Wind Speed at Launch Time     59 non-null     float64       
 19  Hist Ave Max Wind Speed       0 non-null      float64       
 20  Hist Ave Visibility           0 non-null      float64       
 21  Sea Level Pressure            299 non-null    object        
 22  Hist Ave Sea Level Pressure   0 non-null      float64       
 23  Day Length                    298 non-null    object        
 24  Condition                     298 non-null    object        
 25  Notes                         3 non-null      object 

你可看到 Hist Ave Max Wind SpeedHist Ave VisibilityHist Ave Sea Level Pressure 没有数据。

Wind Speed at Launch TimeTemp at Launch TimeLaunchedCrewed or UncrewedTimeName 只有 60 个值是合理的,因为数据只包含 60 次发射。 其余的是发射前后的几日。

下面是清理数据的几种方法:

  • Launched 列中没有 Y 的行没有火箭发射,因此请将这些缺失值设为 N
  • 对于缺少信息来确定火箭是载人火箭还是无人火箭的行,假定是无人火箭。 是无人火箭的可能性更大,因为载人任务更少。
  • 如果缺少风向信息,则将其标记为 unknown
  • 如果缺少天气情况数据,则假定是典型的一天并使用 fair
  • 对于任何其他数据,请使用值 0

在下一个单元中,粘贴并运行以下代码:

## To handle missing values, we will fill the missing values with appropriate values 
launch_data['Launched?'].fillna('N',inplace=True)
launch_data['Crewed or Uncrewed'].fillna('Uncrewed',inplace=True)
launch_data['Wind Direction'].fillna('unknown',inplace=True)
launch_data['Condition'].fillna('Fair',inplace=True)
launch_data.fillna(0,inplace=True)
launch_data.head()

再次尝试运行 launch_data.info() 以查看刚刚对数据所做的更改。

备注

你要更改的是存储在 launch_data 变量中的数据,而不是保存在 Excel 文件中的数据。 如果你发现你修改或删除了任何你不希望进行此类操作的数据,可重新运行笔记本来恢复原始数据。

数据操作

由于计算最适用于数字输入,因此请将所有文本都转换为数字。 例如,如果火箭为载人火箭,使用 1;如果火箭为无人火箭,则使用 0

## As part of the data cleaning process, we have to convert text data to numerical because computers understand only numbers
label_encoder = preprocessing.LabelEncoder()

# Three columns have categorical text info, and we convert them to numbers
launch_data['Crewed or Uncrewed'] = label_encoder.fit_transform(launch_data['Crewed or Uncrewed'])
launch_data['Wind Direction'] = label_encoder.fit_transform(launch_data['Wind Direction'])
launch_data['Condition'] = label_encoder.fit_transform(launch_data['Condition'])

让我们再次查看所有数据,并验证其是否已被清除。

launch_data.head()