Unit 5 (Python) - Colab
Unit 5 (Python) - Colab
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
sales = [120, 135, 150, 145, 160, 175, 180, 170, 165, 180, 195, 210]
plt.figure(figsize=(10, 6))
plt.plot(months, sales, marker='o', linestyle='-')
plt.xlabel('Month')
plt.ylabel('Sales Amount')
plt.title('Product Sales Trend Over the Last 12 Months')
plt.grid(True)
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.cluster import KMeans
import numpy as np
np.random.seed(42)
data = {
'Age': np.random.randint(18, 65, 100),
'Income': np.random.randint(30000, 120000, 100),
'CustomerID': range(1, 101)
}
df = pd.DataFrame(data)
print("Sample Data:")
print(df.head())
plt.figure(figsize=(8, 6))
sns.histplot(df['Age'], bins=10, kde=True)
plt.title('Distribution of Customer Age')
plt.xlabel('Age')
plt.ylabel('Frequency')
plt.show()
plt.figure(figsize=(8, 6))
https://colab.research.google.com/drive/1T6zPX0dM3l7uZw5HfQdL4f06_DzkQwrX#scrollTo=y-K1EGx5o4fT 1/11
08/06/2025, 23:39 unit 5 (python) - Colab
sns.histplot(df['Income'], bins=10, kde=True)
plt.title('Distribution of Customer Income')
plt.xlabel('Income')
plt.ylabel('Frequency')
plt.show()
plt.figure(figsize=(10, 8))
sns.scatterplot(x='Age', y='Income', data=df)
plt.title('Customer Age vs. Income')
plt.xlabel('Age')
plt.ylabel('Income')
plt.show()
X = df[['Age', 'Income']]
kmeans = KMeans(n_clusters=3, random_state=42, n_init=10)
kmeans.fit(X)
df['Cluster'] = kmeans.labels_
plt.figure(figsize=(10, 8))
sns.scatterplot(x='Age', y='Income', hue='Cluster', data=df, palette='viridis', s=50)
plt.title('Customer Segmentation: Age vs. Income with Clusters')
plt.xlabel('Age')
plt.ylabel('Income')
plt.legend(title='Cluster')
plt.show()
https://colab.research.google.com/drive/1T6zPX0dM3l7uZw5HfQdL4f06_DzkQwrX#scrollTo=y-K1EGx5o4fT 2/11
08/06/2025, 23:39 unit 5 (python) - Colab
<ipython-input-2-c3ba236435c0>:17: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in
67414.4 88284.8 77771.2 68656. 41839.2 75235.2 58352.8 41467.2 38437.6
84612.8 77473.6 70442.4 76254.4 42931.2 77737.6 45388.8]' has dtype incompatible with int64, please explicitly cast to a compati
df.loc[df['Age'] < 30, 'Income'] = df.loc[df['Age'] < 30, 'Income'] * 0.8
Sample Data:
Age Income CustomerID
0 56 39234.0 1
1 46 78190.0 2
2 32 35258.0 3
3 60 141045.6 4
4 25 55603.2 5
https://colab.research.google.com/drive/1T6zPX0dM3l7uZw5HfQdL4f06_DzkQwrX#scrollTo=y-K1EGx5o4fT 3/11
08/06/2025, 23:39 unit 5 (python) - Colab
https://colab.research.google.com/drive/1T6zPX0dM3l7uZw5HfQdL4f06_DzkQwrX#scrollTo=y-K1EGx5o4fT 4/11
08/06/2025, 23:39 unit 5 (python) - Colab
stock_data = pd.DataFrame({
'Open': open_prices,
'High': high_prices,
'Low': low_prices,
'Close': close_prices,
'Volume': volume
}, index=dates)
plt.figure(figsize=(12, 6))
plt.plot(stock_data.index, stock_data['Close'])
plt.title('Stock Closing Price Over Time (Line Chart)')
plt.xlabel('Date')
plt.ylabel('Closing Price')
plt.grid(True)
plt.show()
https://colab.research.google.com/drive/1T6zPX0dM3l7uZw5HfQdL4f06_DzkQwrX#scrollTo=y-K1EGx5o4fT 5/11
08/06/2025, 23:39 unit 5 (python) - Colab
https://colab.research.google.com/drive/1T6zPX0dM3l7uZw5HfQdL4f06_DzkQwrX#scrollTo=y-K1EGx5o4fT 6/11
08/06/2025, 23:39 unit 5 (python) - Colab
highest_sales_region = sales_df.loc[sales_df['Sales'].idxmax()]
lowest_sales_region = sales_df.loc[sales_df['Sales'].idxmin()]
plt.figure(figsize=(10, 6))
highest_index = sales_df['Sales'].idxmax()
lowest_index = sales_df['Sales'].idxmin()
colors[highest_index] = 'lightgreen'
colors[lowest_index] = 'salmon'
plt.figure(figsize=(8, 8))
plt.pie(sales_df['Sales'], labels=sales_df['Region'], autopct='%1.1f%%', startangle=90, colors=colors)
plt.title('Sales Proportion by Region')
plt.axis('equal')
plt.show()
https://colab.research.google.com/drive/1T6zPX0dM3l7uZw5HfQdL4f06_DzkQwrX#scrollTo=y-K1EGx5o4fT 7/11
08/06/2025, 23:39 unit 5 (python) - Colab
https://colab.research.google.com/drive/1T6zPX0dM3l7uZw5HfQdL4f06_DzkQwrX#scrollTo=y-K1EGx5o4fT 8/11
08/06/2025, 23:39 unit 5 (python) - Colab
metrics = metrics + [metrics[0]]
employee_scores = employee_scores + [employee_scores[0]]
plt.figure(figsize=(8, 8))
ax = plt.subplot(111, polar=True)
ax.plot(angles, employee_scores, 'o-', linewidth=2)
ax.fill(angles, employee_scores, alpha=0.25)
ax.set_ylim(0, 5)
plt.show()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-8-96f9bd57a8bf> in <cell line: 0>()
22
23
---> 24 ax.set_thetagrids(angles * 180/np.pi, metrics)
25
26
np.random.seed(42)
data = {
'Revenue': np.random.randint(100000, 500000, 50),
'Expenses': np.random.randint(50000, 300000, 50),
'Profit': np.random.randint(20000, 250000, 50),
https://colab.research.google.com/drive/1T6zPX0dM3l7uZw5HfQdL4f06_DzkQwrX#scrollTo=y-K1EGx5o4fT 9/11
08/06/2025, 23:39 unit 5 (python) - Colab
'Marketing Spend': np.random.randint(5000, 50000, 50),
'Employee Count': np.random.randint(10, 100, 50)
}
financial_df = pd.DataFrame(data)
correlation_matrix = financial_df.corr()
plt.figure(figsize=(10, 8))
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', fmt=".2f", linewidths=.5)
plt.title('Correlation Heatmap of Financial Variables')
plt.show()
average_sales = combined_df.groupby('Period')['Sales'].mean().reset_index()
plt.figure(figsize=(8, 6))
plt.bar(average_sales['Period'], average_sales['Sales'], color=['skyblue', 'lightgreen'])
plt.ylabel('Average Daily Sales')
plt.title('Average Sales Performance Before and After Marketing Campaign')
plt.ylim(0, max(average_sales['Sales']) * 1.2)
plt.show()
https://colab.research.google.com/drive/1T6zPX0dM3l7uZw5HfQdL4f06_DzkQwrX#scrollTo=y-K1EGx5o4fT 10/11
08/06/2025, 23:39 unit 5 (python) - Colab
plt.figure(figsize=(12, 6))
plt.plot(df_before['Date'], df_before['Sales'], label='Before Campaign', marker='o')
plt.plot(df_after['Date'], df_after['Sales'], label='After Campaign', marker='o')
plt.xlabel('Date')
plt.ylabel('Daily Sales')
plt.title('Daily Sales Trend Before and After Marketing Campaign')
plt.legend()
plt.grid(True)
plt.show()
https://colab.research.google.com/drive/1T6zPX0dM3l7uZw5HfQdL4f06_DzkQwrX#scrollTo=y-K1EGx5o4fT 11/11