0% found this document useful (0 votes)
4 views2 pages

Data Analysis with Clustering & Regression

The document outlines a data analysis workflow using Python, focusing on a dataset that includes economic indicators. It includes steps for data standardization, K-Means clustering, feature importance analysis using Random Forest, and various visualizations such as correlation matrices and scatterplots. The analysis aims to explore relationships between personal consumption expenditure, unemployment, and other economic metrics.

Uploaded by

Ramesh Vankara
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Data Analysis with Clustering & Regression

The document outlines a data analysis workflow using Python, focusing on a dataset that includes economic indicators. It includes steps for data standardization, K-Means clustering, feature importance analysis using Random Forest, and various visualizations such as correlation matrices and scatterplots. The analysis aims to explore relationships between personal consumption expenditure, unemployment, and other economic metrics.

Uploaded by

Ramesh Vankara
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

# Import necessary libraries

import pandas as pd
import numpy as np
import [Link] as plt
import seaborn as sns
from [Link] import KMeans
from [Link] import RandomForestRegressor
from sklearn.model_selection import train_test_split
from [Link] import mean_squared_error, r2_score
from [Link] import StandardScaler
import warnings

[Link]("ignore", category=FutureWarning)

# Sample Data (Replace with your dataset)


data = {
'date': pd.date_range(start='1967-07-31', periods=10, freq='M'),
'pce': [Link](10) * 1000, # Personal Consumption Expenditure
'pop': [Link](100000, 500000, 10), # Population
'psavert': [Link](10) * 10, # Personal Saving Rate
'uempmed': [Link](10) * 5, # Median Duration of Unemployment
'unemploy': [Link](2000, 5000, 10), # Unemployed people
'contributors': [Link](50, 500, 10), # Contributor Activity
'article_density': [Link](10) * 100, # Number of articles per capita
'gdp': [Link](50000, 200000, 10) # GDP
}
df = [Link](data)

# Standardizing numerical columns


scaler = StandardScaler()
df[['pce', 'pop', 'psavert', 'uempmed', 'unemploy', 'contributors',
'article_density', 'gdp']] = \
scaler.fit_transform(df[['pce', 'pop', 'psavert', 'uempmed', 'unemploy',
'contributors', 'article_density', 'gdp']])

# **DISPLAY TABLE DATA FIRST**


# Display first few rows of the dataset
print("🔹 First 5 Rows of the Dataset:")
print([Link]())

# Show summary statistics of numerical columns


print("\n🔹 Summary Statistics:")
print([Link]())

# Display correlation matrix as a table (useful before heatmap)


print("\n🔹 Correlation Matrix Table:")
print([Link](columns=['date']).corr())

# **1. K-Means Clustering Plot**


kmeans = KMeans(n_clusters=3, random_state=42)
df['cluster'] = kmeans.fit_predict(df[['pce', 'pop', 'psavert', 'uempmed',
'unemploy']])
[Link](figsize=(8, 6))
[Link](x='pce', y='unemploy', hue='cluster', data=df, palette='Set2',
s=100)
[Link]('K-Means Clustering: PCE vs Unemployment')
[Link]()

# **2. Feature Importance (Random Forest)**


X = df[['pce', 'pop', 'psavert', 'uempmed']]
y = df['unemploy']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
rf_model = RandomForestRegressor(random_state=42)
rf_model.fit(X_train, y_train)
importance = rf_model.feature_importances_
[Link](figsize=(8, 6))
[Link](x=importance, y=[Link], color='skyblue')
[Link]('Feature Importance (Random Forest)')
[Link]()

# **3. Correlation Matrix Heatmap**


[Link](figsize=(8, 6))
[Link]([Link](columns=['date', 'cluster']).corr(), annot=True,
cmap='coolwarm', fmt=".2f", linewidths=0.5)
[Link]('Correlation Matrix Heatmap')
[Link]()

# **4. Box-and-Whisker Plot for Engagement Metrics**


[Link](figsize=(8, 6))
[Link](data=df[['pce', 'pop', 'psavert', 'uempmed', 'unemploy']],
palette="Set3")
[Link]('Box-and-Whisker Plot for Engagement Metrics')
[Link](rotation=45)
[Link]()

# **5. Histogram of Contributor Activity**


[Link](figsize=(8, 6))
[Link](df['contributors'], bins=10, kde=True, color='purple')
[Link]('Histogram of Contributor Activity')
[Link]('Contributor Activity')
[Link]('Frequency')
[Link]()

# **6. Scatterplot: Article Density vs. GDP**


[Link](figsize=(8, 6))
[Link](x='article_density', y='gdp', data=df, color='red')
[Link]('Scatterplot: Article Density vs. GDP')
[Link]('Article Density')
[Link]('GDP')
[Link]()

You might also like