👾 The Alien Invasion! (Solution)¶
Scenario:
You're a data analyst at the Earth Defence Organisation (EDO). You've just received a transmission containing data about an alien fleet heading towards Earth! The transmission contains vital data about each alien spaceship's dimensions, speed, and weapon power. Your job is to analyze this data to provide the EDO with the insights needed to prepare Earth's defenses.
Exercise:
The transmission is an matrix (an array) which contains data for each spaceship. Every line represents one spaceship, with values in the order of: length (in meters), width (in meters), height (in meters), speed (in km/s), and weapon power (on a scale of 1-10).
In [1]:
Copied!
import numpy as np
fleet_data = np.array([
[120, 40, 25, 2.5, 10],
[85, 60, 30, 5.0, 5],
[100, 50, 35, 4.5, 7],
[120, 40, 25, 2.5, 9],
[150, 50, 30, 3.0, 10],
], dtype=np.float32)
import numpy as np
fleet_data = np.array([
[120, 40, 25, 2.5, 10],
[85, 60, 30, 5.0, 5],
[100, 50, 35, 4.5, 7],
[120, 40, 25, 2.5, 9],
[150, 50, 30, 3.0, 10],
], dtype=np.float32)
Your tasks:
- Calculate and print the total number of spaceships.
In [2]:
Copied!
total_spaceships = fleet_data.shape[0]
print(f'Total number of spaceships: {total_spaceships}')
total_spaceships = fleet_data.shape[0]
print(f'Total number of spaceships: {total_spaceships}')
Total number of spaceships: 5
- Calculate and print the average spaceship size (volume).
In [3]:
Copied!
length, width, height = fleet_data[:, 0], fleet_data[:, 1], fleet_data[:, 2]
average_volume = np.mean(length * width * height)
print(f'Average spaceship volume: {average_volume} cubic meters')
length, width, height = fleet_data[:, 0], fleet_data[:, 1], fleet_data[:, 2]
average_volume = np.mean(length * width * height)
print(f'Average spaceship volume: {average_volume} cubic meters')
Average spaceship volume: 158600.0 cubic meters
- Identify and print the speed of the fastest spaceship.
In [4]:
Copied!
max_speed = np.max(fleet_data[:, 3])
print(f'The fastest spaceship speed: {max_speed} km/s')
max_speed = np.max(fleet_data[:, 3])
print(f'The fastest spaceship speed: {max_speed} km/s')
The fastest spaceship speed: 5.0 km/s
- Determine and print the number of spaceships with the maximum weapon power.
In [5]:
Copied!
max_weapon_power_spaceships = np.sum(fleet_data[:, 4] == 10)
print(f'Number of spaceships with maximum weapon power: {max_weapon_power_spaceships}')
max_weapon_power_spaceships = np.sum(fleet_data[:, 4] == 10)
print(f'Number of spaceships with maximum weapon power: {max_weapon_power_spaceships}')
Number of spaceships with maximum weapon power: 2
- Find and print the mean speed of spaceships with the maximum weapon power.
In [6]:
Copied!
mean_speed_max_power = np.mean(fleet_data[fleet_data[:, 4] == 10, 3])
print(f'Mean speed of spaceships with maximum weapon power: {mean_speed_max_power} km/s')
mean_speed_max_power = np.mean(fleet_data[fleet_data[:, 4] == 10, 3])
print(f'Mean speed of spaceships with maximum weapon power: {mean_speed_max_power} km/s')
Mean speed of spaceships with maximum weapon power: 2.75 km/s
Remember, the fate of Earth depends on your quick and accurate analysis. Good luck! ๐