Answers 1
Answers 1
Answers 1
Data visualization is crucial for gaining insights from data. Different types of
plots are used based on the nature of the data and the relationships we want to
visualize. Some common types of plots include:
Scatter Plot:
Histogram:
# Create a histogram
plt.figure(figsize=(8, 6))
plt.hist(data, bins=30, color='green', edgecolor='black', alpha=0.7)
plt.title('Histogram')
plt.xlabel('Values')
plt.ylabel('Frequency')
plt.grid(True)
plt.show()
Here, plt.hist is used to create a histogram where data is the input data, bins
specify the number of bins, and other parameters control the appearance of the
histogram.
Bar Plot:
# Sample data
categories = ['A', 'B', 'C', 'D']
values = [20, 35, 30, 25]
Line Plot:
A line plot is used to visualize trends or patterns over time or other ordered
categories.
It consists of data points connected by straight lines.
Example:
# Generate data
x = np.linspace(0, 10, 100)
y = np.sin(x)
These are just a few examples of common plot types used in data visualization.
Choosing the right type of plot depends on the data characteristics and the specific
insights you want to convey.
# Create an array
arr = np.array([1, 2, 3, 4, 5])
np.sqrt computes the square root of each element in the array arr.
np.exp computes the exponential of each element in arr.
np.sin computes the sine of each element in arr.
These ufuncs allow for efficient and concise computation of mathematical
operations on arrays in NumPy.
3) Data processing using array
NumPy arrays are fundamental for data processing tasks in Python. They provide
efficient data structures and built-in functions for performing operations such as
aggregation, filtering, and statistical computations.
np.sum(data, axis=1) computes the sum along each row of the 2D array data.
np.mean(data) calculates the mean of all elements in data.
np.max(data, axis=0) finds the maximum value in each column of data.
NumPy's array operations provide a concise and efficient way to process and
analyze data in scientific computing and data science workflows.
python
Copy code
import numpy as np
python
Copy code
import numpy as np
print("2D Array:")
print(arr_2d)
print("3D Array:")
print(arr_3d)
In this example:
plt.show()
In this example:
plt.xlabel and plt.ylabel add labels to the x-axis and y-axis, respectively.
plt.title sets the title of the plot.
plt.xticks and plt.yticks customize the ticks on the x-axis and y-axis using NumPy
arrays.
plt.legend displays the legend based on the label specified in plt.plot.
Adding labels and ticks improves the readability and interpretability of plots in
data visualization.
# Create a DataFrame
data = pd.DataFrame({
'Category': ['A', 'B', 'C', 'D'],
'Values': [10, 20, 15, 25]
})
Matplotlib is used to create a line plot (plt.plot) and customize labels, title, legend,
and grid.
Seaborn is used to create a bar plot (sns.barplot) from a DataFrame, customizing
the appearance using the palette parameter.
These libraries provide powerful tools for creating informative and visually
appealing plots for data analysis and presentation.
9) Explain about basic indexing and slicing?
Indexing and slicing are fundamental operations for accessing and manipulating
elements of NumPy arrays efficiently.
Basic Indexing:
python
Copy code
import numpy as np
# Create a 2D array
arr = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Basic indexing
print(arr[0, 1]) # Access element at row 0, column 1
# Slicing
print(arr[:, 1]) # Get all elements in column 1
print(arr[1:3, :]) # Get rows 1 and 2, all columns
In this example:
arr[0, 1] accesses the element at the first row and second column of the 2D array
arr.
arr[:, 1] retrieves all elements in the second column.
arr[1:3, :] extracts rows 1 and 2 with all columns.
Indexing and slicing provide a powerful mechanism for data extraction and
manipulation in NumPy arrays.
Array Computation:
# Create arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Array computation
print(arr1 + arr2) # Element-wise addition
print(np.dot(arr1, arr2)) # Dot product of arrays
print(np.mean(arr1)) # Compute mean of elements
In this example:
Series:
python
Copy code
import pandas as pd
# Creating a Series
s = pd.Series([1, 2, 3, 4], index=['A', 'B', 'C', 'D'])
# Creating a DataFrame
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'San Francisco', 'Los Angeles']
}
df = pd.DataFrame(data)
print("Series:")
print(s)
print("\nDataFrame:")
print(df)
In this example: