_1952 / _1952.py
antitheft159's picture
Upload _1952.py
b09b46b verified
# -*- coding: utf-8 -*-
""".1952
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1Gw-RJg5Bp__ayBzDb0HsHaj9uYYfQ_nI
"""
# Commented out IPython magic to ensure Python compatibility.
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
# %matplotlib inline
file_path = '/content/Key_Economic_Indicators.csv'
df = pd.read_csv(file_path)
df.head()
df.isnull().sum()
df.fillna(df.mean(), inplace=True)
df['Date'] = pd.to_datetime(df[['Year', 'Month']].assign(DAY=1))
df.drop(['Year', 'Month'], axis=1, inplace=True)
df.head()
plt.figure(figsize=(12, 6))
sns.lineplot(data=df, x='Date', y='Consumer Confidence Index TX', label='TX')
plt.title('Consumer Confidence Index Over Time')
plt.xlabel('Date')
plt.ylabel('Consumer Confidence Index')
plt.legend()
plt.show()
plt.figure(figsize=(12, 6))
sns.histplot(df['Unemployment TX'], kde=True, color='blue', label='TX')
sns.histplot(df['Unemployment U.S.'], kde=True, color='red', label='US')
plt.title('Distribution of Unemployment Rates')
plt.xlabel('Unemployment Rate')
plt.ylabel('Frequency')
plt.legend()
plt.show()
numeric_df = df.select_dtypes(include=[np.number])
plt.figure(figsize=(14, 10))
sns.heatmap(numeric_df.corr(), annot=True, fmt='.2f', cmap='coolwarm')
plt.title('Correlation Matrix of Economic Indicators')
plt.show()
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error
X = numeric_df.drop(columns=['Consumer Confidence Index TX'])
y = numeric_df['Consumer Confidence Index TX']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = RandomForestRegressor(random_state=42)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
rmse = np.sqrt(mse)
rmse