Certainly! Below is a Python script that accomplishes what you've described:
- Groups the
input_features array by the unique values in the first column.
- Calculates the mean of the second-to-last and last columns for each group.
- Plots these means for each unique first index.
import numpy as np
import matplotlib.pyplot as plt
# Assuming input_features is your numpy array with shape (39072, 26)
# For demonstration purposes, let's create a sample array
# Remove or replace this with your actual input_features array
# input_features = np.random.rand(39072, 26)
# For testing, let's create a smaller array
np.random.seed(0)
input_features = np.random.rand(1000, 26)
# Print the shape of input_features
print(f"Input features shape: {input_features.shape}")
# Extract the first column (used for grouping)
first_indices = input_features[:, 0]
# Get the unique values in the first column
unique_first_indices = np.unique(first_indices)
# Initialize lists to store the means
mean_second_last = []
mean_last = []
unique_indices_list = []
# Loop over each unique value
for unique_value in unique_first_indices:
# Find the rows where the first column equals the unique value
mask = first_indices == unique_value
# Extract the rows matching the current unique value
group = input_features[mask]
# Calculate the mean of the second-to-last and last columns
mean_second_last_value = group[:, -2].mean()
mean_last_value = group[:, -1].mean()
# Append the results to the lists
mean_second_last.append(mean_second_last_value)
mean_last.append(mean_last_value)
unique_indices_list.append(unique_value)
# Convert lists to numpy arrays for plotting
mean_second_last = np.array(mean_second_last)
mean_last = np.array(mean_last)
unique_indices_list = np.array(unique_indices_list)
# Plot the means
plt.figure(figsize=(12, 6))
# Plot for the second-to-last column
plt.subplot(1, 2, 1)
plt.plot(unique_indices_list, mean_second_last, 'o-', label='Mean of Second-to-Last Column')
plt.xlabel('Unique First Index')
plt.ylabel('Mean Value')
plt.title('Mean of Second-to-Last Column by Unique First Index')
plt.legend()
# Plot for the last column
plt.subplot(1, 2, 2)
plt.plot(unique_indices_list, mean_last, 'o-', color='orange', label='Mean of Last Column')
plt.xlabel('Unique First Index')
plt.ylabel('Mean Value')
plt.title('Mean of Last Column by Unique First Index')
plt.legend()
plt.tight_layout()
plt.show()
Explanation:
-
Data Preparation:
- Extract the first column from
input_features to use for grouping.
- Find unique values in the first column using
np.unique().
-
Grouping and Calculation:
- Loop over each unique value in the first column.
- Create a boolean mask to filter rows where the first column matches the current unique value.
- Extract the group of rows that correspond to the current unique value.
- Calculate the mean of the second-to-last (
-2) and last (-1) columns for the group.
- Store the means and the unique value for plotting.
-
Plotting:
- Use
matplotlib.pyplot to create plots.
- Plot the means of the second-to-last and last columns against the unique first indices.
- Customize the plots with titles, labels, and legends.
Note: The script assumes that input_features is a NumPy array of shape (39072, 26). If you have your actual data, make sure to replace or remove the sample data generation part.
Sample Output:
Since we're using random data in this example, the output will be a plot of mean values computed from that data.
Tips:
- If the number of unique first indices is large, the plot might become cluttered.
- Consider sorting the
unique_indices_list and corresponding means if you want the x-axis to be ordered.
- You can adjust the plotting style to better suit your data (e.g., using a bar chart if that makes more sense).
Alternate Approach with Pandas:
If you prefer using Pandas for easier data manipulation:
import pandas as pd
import matplotlib.pyplot as plt
# Convert the numpy array to a DataFrame
df = pd.DataFrame(input_features)
# Group by the first column
grouped = df.groupby(0)
# Calculate the mean of the second-to-last and last columns
mean_values = grouped[[24, 25]].mean().reset_index()
# Plotting
plt.figure(figsize=(12, 6))
# Plot for the second-to-last column
plt.subplot(1, 2, 1)
plt.plot(mean_values[0], mean_values[24], 'o-', label='Mean of Second-to-Last Column')
plt.xlabel('Unique First Index')
plt.ylabel('Mean Value')
plt.title('Mean of Second-to-Last Column by Unique First Index')
plt.legend()
# Plot for the last column
plt.subplot(1, 2, 2)
plt.plot(mean_values[0], mean_values[25], 'o-', color='orange', label='Mean of Last Column')
plt.xlabel('Unique First Index')
plt.ylabel('Mean Value')
plt.title('Mean of Last Column by Unique First Index')
plt.legend()
plt.tight_layout()
plt.show()
This approach uses Pandas DataFrame for grouping and computing the mean, which can be more concise and readable.