Lesson 05 � Data Science
DATA KO
DISHA DO.
Data ko dikhana samjhna zaroori hai � charts se patterns easily samajh aati hain. Matplotlib, Seaborn, Plotly sab visualization libraries hain. Visualization ke bina data sirf numbers hai, story nahi.
WHY: Visualization kyun zaroori hai?
1000 rows ka data padhke samajhna mushkil hai, lekin ek chart mein sab samajh aa jaata hai. Visualization se outliers dikhte hain, trends milte hain, aur patterns saaf nazar aate hain. Presentation mein bhi charts sabse zyada impactful hote hain.
Python ki sabse purani aur powerful plotting library. Sab kuch customize kar sakte ho � colors, labels, sizes, layout.
Statistical visualization library jo Matplotlib pe built hai. Beautiful default themes aur complex plots asaan karta hai.
Interactive charts � hover karke data dekho, zoom karo, download karo. Dashboards ke liye best hai.
Kaunsa chart kab use karo � line for trends, bar for comparison, histogram for distribution, scatter for relationship.
LINE CHART: trends dikhao
Line chart tab use karo jab data mein koi trend ya pattern dikhana ho over time � sales, temperature, stock prices. Points ko line se connect karo aur direction samajh aa jaayegi.
import matplotlib.pyplot as plt
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
sales = [100, 120, 150, 130, 180]
plt.plot(months, sales, marker='o', color='steelblue', linewidth=2)
plt.title('Monthly Sales Trend')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.grid(True, alpha=0.3)
plt.show()BAR CHART: comparison karo
Bar chart tab use karo jab do cheezon ki comparison karni ho � courses ki popularity, departments ki performance, cities ki population. Lambi bars zyada value, chhoti bars kam value.
import matplotlib.pyplot as plt
courses = ['Python', 'SQL', 'ML', 'Data Science']
students = [150, 120, 80, 100]
plt.bar(courses, students, color=['gold', 'skyblue', 'coral', 'lightgreen'])
plt.title('Students per Course')
plt.xlabel('Course')
plt.ylabel('Students')
plt.show()plt.barh(). Labels easily padh jaayengi bina rotate kiye.HISTOGRAM: distribution samjho
Histogram tab use karo jab dekhna ho ki data kaise distributed hai � kitne logon ke 70-80 marks aaye, kitne ke 80-90. Bins data ko groups mein divide karte hain.
import matplotlib.pyplot as plt
import numpy as np
scores = np.random.normal(75, 10, 1000)
plt.hist(scores, bins=20, edgecolor='black', color='steelblue', alpha=0.7)
plt.title('Score Distribution')
plt.xlabel('Score')
plt.ylabel('Frequency')
plt.axvline(x=75, color='red', linestyle='--', label='Mean')
plt.legend()
plt.show()SCATTER PLOT: relationship dikhao
Scatter plot tab use karo jab do variables ki relationship samajhni ho � bill aur tip ka correlation, age aur salary ka relation. Points kitne close hain, usse pattern pata chalta hai.
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(42)
x = np.random.rand(50) * 100
y = x * 0.8 + np.random.randn(50) * 10
plt.scatter(x, y, alpha=0.6, c='coral', edgecolors='black')
plt.title('X vs Y Relationship')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()SEABORN: statistical visualization
Seaborn Matplotlib pe built hai lekin default styles aur colors bahut achhe hain. Complex statistical plots � heatmap, boxplot, pairplot � Seaborn se bahut easy ho jaate hain.
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset('tips')
# Scatter with hue � gender wise
sns.scatterplot(data=tips, x='total_bill', y='tip', hue='sex', size='size')
plt.title('Bill vs Tip')
plt.show()
# Boxplot � distribution by day
sns.boxplot(data=tips, x='day', y='total_bill')
plt.title('Bill Distribution by Day')
plt.show()HEATMAP: correlation dikhao
Heatmap se variables ka correlation matrix dikh jaata hai � positive correlation (1) red, negative (-1) blue, zero (0) white. ML mein feature selection ke liye bahut useful hai.
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame({
'math': [85, 90, 78, 92, 88],
'science': [82, 88, 75, 90, 85],
'english': [80, 85, 80, 88, 82]
})
corr = df.corr()
sns.heatmap(corr, annot=True, cmap='coolwarm', center=0)
plt.title('Subject Correlation')
plt.show()SUBPLOTS: ek saath multiple charts
Ek figure mein multiple charts rakh sakte ho � side by side ya grid mein. Comparison karna ho toh subplots best hain.
import matplotlib.pyplot as plt
import numpy as np
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
# Line chart
axes[0].plot(['A','B','C','D'], [10,20,15,25], marker='o')
axes[0].set_title('Line Chart')
# Bar chart
axes[1].bar(['A','B','C','D'], [10,20,15,25], color='coral')
axes[1].set_title('Bar Chart')
plt.tight_layout()
plt.show()CHART GUIDE: kaunsa chart kab use karo
Har chart type ka apna use case hai. Galat chart choose karo toh message clear nahi jaayega.
Trends over time � sales, temperature, stock prices. Time series data ke liye best.
Comparison between categories � courses, departments, cities. Discrete values ke liye.
Distribution of continuous data � marks, salaries, ages. Bins mein data divide hota hai.
Relationship between two variables � correlation, causation. Points ka pattern dikhata hai.
Try it: charts banao
Editor mein apna code likho aur "Run Python" dabao. Different chart types try karo aur dekho kaunsa kaise dikhta hai.
Quick check
4 courses ke liye bar chart banao � courses aur students variables already hain. Sirf 2 lines mein chart banao.
plt.bar() use karo pehle, phir plt.show() se chart dikhega.
Common beginner mistakes
- plt.show() bhoolna: Bina
plt.show()ke chart print nahi hota. Hamesha last mein lagao. - Labels na dena: Chart bana diya lekin axis labels aur title nahi daale � kaun samjhega kya dikh raha hai?
- Sab charts mein bar use karna: Har data ke liye bar chart sahi nahi hai. Trend ke liye line, distribution ke liye histogram.
- Colors na customize karna: Default colors boring lag sakte hain.
colorparameter se chart professional dikhta hai.
Ab Feature Engineering par chalo � naye features banao aur models ko powerful banao.