Understanding Mean and Variance in Scatter Plots
In this section, we generate random datasets and visualize their distributions using scatter plots. Additionally, we calculate and annotate the mean and variance for each dataset to gain insights into their statistical properties.
## Mean & variance
size = 100
x = np.random.randn(size)
y = np.random.randn(size)
fig, ax = plt.subplots(2,2, figsize=(12,10), dpi=100)
data = ( (x,y), (x*5,y), (x,y*5), (x*5,y*5))
for (xx,yy), axx in zip(data, ax.flatten()):
axx.scatter(xx, yy)
axx.set_xlim([-10, 10])
axx.set_ylim([-10, 10])
axx.minorticks_on()
axx.grid(True, axis='both', which='major', color="red", linestyle='-', alpha=0.2) # major grid
axx.grid(True, axis='both', which='minor', color='k', linestyle='--', alpha=0.1) # minor grid
mean_x, mean_y = np.mean(xx), np.mean(yy)
var_x, var_y = np.var(xx), np.var(yy)
# Annotate mean and variance
axx.text(0.05, 0.95, f"Mean (x): {mean_x:.2f}", transform=axx.transAxes, ha="left", va="top")
axx.text(0.05, 0.90, f"Mean (y): {mean_y:.2f}", transform=axx.transAxes, ha="left", va="top")
axx.text(0.05, 0.85, f"Variance (x): {var_x:.2f}", transform=axx.transAxes, ha="left", va="top")
axx.text(0.05, 0.80, f"Variance (y): {var_y:.2f}", transform=axx.transAxes, ha="left", va="top")
Explanation:
-
Data Generation:
- We start by generating two sets of random numbers
x and y, each containing 100 samples drawn from a standard normal distribution (np.random.randn).
- To explore how scaling affects the data, we create four different datasets:
(x, y): Original data.
(x*5, y): Scaling x by a factor of 5.
(x, y*5): Scaling y by a factor of 5.
(x*5, y*5): Scaling both x and y by a factor of 5.
-
Visualization:
- We create a 2x2 grid of scatter plots using
plt.subplots(2,2).
- For each dataset, we plot the scatter points within the range [-10, 10] for both axes.
- Gridlines are added for better readability, with major grids in red and minor grids in black.
-
Statistical Annotation:
- For each subplot, we calculate the mean and variance of both
x and y.
- These statistics are annotated directly on the plots for quick reference.
Applying Shear Transformations
In the following section, we attempt to apply shear transformations to one of our datasets and visualize the results. Shear transformations can distort the data by shifting its shape, which is useful in various applications like image processing and data augmentation.
## But they fail ..
xxyy = np.vstack(data[2]) # xx,yy = data[2]
shear_right = np.array([[1, 1], [0, 1]])
shear_left = np.array([[1, -1], [1, 1]])
shear_matrix = np.stack([shear_right, shear_left], axis=0)
fig, ax = plt.subplots(1,2, figsize=(10,6), dpi=100)
for axx, shm in zip(ax.flatten(), shear_matrix):
axx.scatter(*shm.dot(xxyy))
axx.set_xlim([-10, 10])
axx.set_ylim([-10, 10])
axx.minorticks_on()
axx.grid(True, axis='both', which='major', color="red", linestyle='-', alpha=0.2) # major grid
axx.grid(True, axis='both', which='minor', color='k', linestyle='--', alpha=0.1) # minor grid
mean_x, mean_y = np.mean(xx), np.mean(yy)
var_x, var_y = np.var(xx), np.var(yy)
# Annotate mean and variance
axx.text(0.05, 0.95, f"Mean (x): {mean_x:.2f}", transform=axx.transAxes, ha="left", va="top")
axx.text(0.05, 0.90, f"Mean (y): {mean_y:.2f}", transform=axx.transAxes, ha="left", va="top")
axx.text(0.05, 0.85, f"Variance (x): {var_x:.2f}", transform=axx.transAxes, ha="left", va="top")
axx.text(0.05, 0.80, f"Variance (y): {var_y:.2f}", transform=axx.transAxes, ha="left", va="top")
Issues Encountered:
Despite setting up the shear transformations, the above code fails to execute as intended. Here's a breakdown of the potential issues:
-
Data Selection:
xxyy = np.vstack(data[2]) selects the third dataset (x, y*5) from the data tuple. This combines x and y*5 vertically, resulting in a 2x100 array suitable for matrix multiplication.
-
Shear Matrices:
- Two shear matrices are defined:
shear_right: Shears the data to the right.
shear_left: Shears the data to the left.
- These matrices are stacked into a single array for iteration.
-
Plotting Transformed Data:
- For each shear matrix, we apply the transformation using matrix multiplication (
shm.dot(xxyy)) and plot the transformed data.
- However, the variables
xx and yy used for calculating means and variances inside the loop are undefined in this context. They were defined in the previous loop but are not available here.
Solution:
To fix the errors and successfully visualize the shear transformations, consider the following modifications:
-
Update Variable Names:
- After transforming the data with
shm.dot(xxyy), unpack the transformed coordinates into xx and yy for consistency.
- This ensures that
mean_x, mean_y, var_x, and var_y are calculated correctly.
-
Correct the Loop:
fig, ax = plt.subplots(1,2, figsize=(10,6), dpi=100)
for axx, shm in zip(ax.flatten(), shear_matrix):
transformed = shm.dot(xxyy)
xx, yy = transformed
axx.scatter(xx, yy)
axx.set_xlim([-10, 10])
axx.set_ylim([-10, 10])
axx.minorticks_on()
axx.grid(True, axis='both', which='major', color="red", linestyle='-', alpha=0.2) # major grid
axx.grid(True, axis='both', which='minor', color='k', linestyle='--', alpha=0.1) # minor grid
mean_x, mean_y = np.mean(xx), np.mean(yy)
var_x, var_y = np.var(xx), np.var(yy)
# Annotate mean and variance
axx.text(0.05, 0.95, f"Mean (x): {mean_x:.2f}", transform=axx.transAxes, ha="left", va="top")
axx.text(0.05, 0.90, f"Mean (y): {mean_y:.2f}", transform=axx.transAxes, ha="left", va="top")
axx.text(0.05, 0.85, f"Variance (x): {var_x:.2f}", transform=axx.transAxes, ha="left", va="top")
axx.text(0.05, 0.80, f"Variance (y): {var_y:.2f}", transform=axx.transAxes, ha="left", va="top")
- Final Observations:
- After implementing the fixes, the scatter plots will correctly display the sheared data.
- The annotations will accurately reflect the statistical changes introduced by the shear transformations.
Conclusion
This notebook demonstrates how to visualize and analyze the mean and variance of datasets using scatter plots. Additionally, it explores the effect of shear transformations on data distributions. By identifying and resolving the issues in the transformation section, we ensure accurate and meaningful visualizations that can aid in data analysis and interpretation.