Assignment 2
Assignment 2
```python
import seaborn as sns
import pandas as pd
This code will create countplots for the quantile bins of columns X, Y, and Z in the
diamonds dataset. Each bin represents a range of values for each column, and the
countplot shows the distribution of data points within each bin. Certainly! Here's a
more concise version:
```python
import seaborn as sns
import pandas as pd
diamonds = sns.load_dataset('diamonds')
```python
import seaborn as sns
import pandas as pd
# Load the housing dataset (replace 'housing_data.csv' with the actual file
name)
housing_data = pd.read_csv('housing_data.csv')
In this approach:
```python
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
# Load the housing dataset (replace 'housing_data.csv' with the actual file
name)
housing_data = pd.read_csv('housing_data.csv')
# Plot heatmap
plt.figure(figsize=(10, 8))
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', fmt=".2f")
plt.title('Correlation Heatmap of Continuous Variables')
plt.show()
```
In this code:
```python
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
# Load the housing dataset (replace 'housing_data.csv' with the actual file
name)
housing_data = pd.read_csv('housing_data.csv')
# Plot heatmap
plt.figure(figsize=(10, 8)) # Set the size of the heatmap
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', fmt=".2f")
# annot=True shows correlation values, cmap='coolwarm' selects color
palette, fmt=".2f" formats values to 2 decimal places
plt.title('Correlation Heatmap of Continuous Variables') # Set the title of the
heatmap
plt.show() # Display the heatmap
```
1. **Load the dataset**: Reads the housing dataset into a pandas DataFrame.
Replace `'housing_data.csv'` with the actual filename.
This heatmap allows you to quickly identify patterns of association between pairs
of continuous variables in the housing dataset. Strong correlations (either positive
or negative) are indicated by darker shades, while weaker correlations are
represented by lighter shades or near-zero values.
Q4. FIND THE ROOTS OF THE EQUATION OF “x^2 – 6X + 5 AND
POINT OUT THE ROOTS ON MATPLOTIB GRAPH.
ANS 4. To find the roots of the equation \(x^2 - 6x + 5\), you can use the quadratic
formula:
For the given equation \(x^2 - 6x + 5\), \(a = 1\), \(b = -6\), and \(c = 5\).
Now, let's calculate the roots and then plot them on a Matplotlib graph:
```python
import numpy as np
import matplotlib.pyplot as plt
elif discriminant == 0:
# One real root (repeated)
root = -b / (2*a)
print("Root is real and repeated.")
print("Root:", root)
else:
# Complex roots
real_part = -b / (2*a)
imaginary_part = np.sqrt(abs(discriminant)) / (2*a)
root1 = complex(real_part, imaginary_part)
root2 = complex(real_part, -imaginary_part)
print("Roots are complex.")
print("Root 1:", root1)
print("Root 2:", root2)
```python
import numpy as np
import matplotlib.pyplot as plt
# Generate x values
x = np.linspace(0, 2*np.pi, 100)
# Show plot
plt.show()
```
This code generates \( x \) values ranging from 0 to \( 2\pi \) (a full period of the
sine and cosine functions), computes the corresponding \( \sin(x) \) and \( \cos(x) \)
values, and then plots them on the same graph using `plt.plot()`. Finally, it adds
labels, a title, and a legend to the plot using `plt.xlabel()`, `plt.ylabel()`, `plt.title()`,
and `plt.legend()` functions respectively. Certainly! Here's a more concise version:
```python
import numpy as np
import matplotlib.pyplot as plt
This code accomplishes the same task in a more condensed form. It generates \(x\)
values from 0 to \(2\pi\), computes \( \sin(x) \) and \( \cos(x) \) values, plots them
on the same graph, adds labels, a title, and a legend, and finally displays the plot.
Q6. GENERATE A 3D PLOT TO REPRESENT USING NUMPY
MESHGRID TO REPRESENT A PEAK AND A VALLEY IN A GRAPH.
ANS 6. Certainly! You can use NumPy's meshgrid to create a grid of points and
then plot a function that represents a peak and a valley in 3D using Matplotlib.
Here's an example:
```python
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Create 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Show plot
plt.show()
```
In this code:
You can adjust the parameters of the functions and the range of the grid points to
visualize different peaks and valleys. Sure! Here's a more concise version:
```python
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('Peak and Valley')
plt.show()
```
This code generates a grid of points, calculates the values for a peak and a valley
function, and plots them in 3D using matplotlib.
Q7. WHY DO YOU THINK IT IS IMPORTANT TO MELT THE
DATAFRAME BEFORE WE DO SOME PLOTTING, IN WHAT
SCENERIOS THIS WILL BE IMPORTANT.
ANS 7. Melting a Data Frame is often important in scenarios where the data is in a
wide format (multiple columns) and needs to be converted into a long format
(single column) for visualization or analysis purposes. Here are a few reasons why
melting data might be important and the scenarios where it's particularly useful:
2. **Facet Plots**: In scenarios where you want to create facet plots (plots with
multiple subplots arranged in a grid, each showing a different subset of the data),
melting the data is often a prerequisite. Libraries like Seaborn provide functions
like `sns.catplot ()` or `sns.relplot()` which expect data in a long format for creating
facet plots.
6. **Time Series Analysis**: In time series analysis, you might have data in a
wide format where each column represents a time point. Melting the DataFrame
can transform it into a long format where each row represents a unique time point,
making it easier to perform time-based analysis or plotting.