This project focuses on predicting the closing price of Microsoft (MSFT) stock using historical stock data. The dataset includes columns such as date, open, high, low, close, adjusted close, and volume. For this project, the 'Close' column is used as the target variable (y). The project involves data visualization, model selection, training, and evaluation using a Decision Tree Regression model.
Ensure you have the following libraries installed:
You can install these libraries using pip:
pip install pandas numpy matplotlib seaborn scikit-learn
git clone https://github.com/yourusername/Station-Project-1-The-Prediction.git
cd microsoft_stock_price_prediction.py
microsoft_stock_price_prediction.py
) in your preferred IDE or text editor.microsoft_stock_price_prediction.py
project directory).Visualized the relationships between different features using:
Example output:
import seaborn as sns
import matplotlib.pyplot as plt
# Pair plot
sns.pairplot(df)
plt.show()
# Heatmap
plt.figure(figsize=(10, 6))
sns.heatmap(df.corr(), annot=True, cmap='coolwarm')
plt.show()
Split the data into training and testing sets, then selected Decision Tree Regression for model training.
Example:
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeRegressor
# Splitting the data
X = df.drop(['Close'], axis=1)
y = df['Close']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Model selection
model = DecisionTreeRegressor()
model.fit(X_train, y_train)
Calculated Mean Squared Error (MSE), Mean Absolute Error (MAE), Root Mean Squared Error (RMSE), and R-squared (R²) metrics to evaluate the model's performance.
Example:
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
# Predictions
y_pred = model.predict(X_test)
# Metrics
mse = mean_squared_error(y_test, y_pred)
mae = mean_absolute_error(y_test, y_pred)
rmse = np.sqrt(mse)
r2 = r2_score(y_test, y_pred)
print(f"MSE: {mse}")
print(f"MAE: {mae}")
print(f"RMSE: {rmse}")
print(f"R²: {r2}")
The model's evaluation metrics are as follows:
These results demonstrate the accuracy and prediction capabilities of the Decision Tree Regression model for Microsoft stock price prediction.
This project showcases the process of predicting stock prices using historical data and machine learning techniques. By following the steps provided, you can run and test the model, visualize the data, and evaluate the model's performance.