Stock Price Prediction using Machine Learning in Python
- Predicting House Prices using Machine Learning - April 10, 2025
- 10 Data Visualization Project Ideas with Source Code - April 9, 2025
- Music Recommendation System using Python – Full Project - April 7, 2025

Stock Price Prediction using Machine Learning in Python:
Introduction:
Stock price prediction is one of the most challenging yet rewarding applications of machine learning. This project focuses on Stock Price Prediction using Machine Learning in Python, where we analyze historical stock data and apply various machine learning models to forecast future prices.
Objective:
- Collect and preprocess Tesla stock market data.
- Perform Exploratory Data Analysis (EDA) to understand trends.
- Apply feature engineering techniques for better predictions.
- Implement machine learning models for stock price prediction.
- Evaluate model performance and visualize results.
Tools & Libraries:
We will use the following Python libraries:
pandas
for data manipulation.numpy
for numerical computations.matplotlib
andseaborn
for data visualization.scikit-learn
for machine learning models.
Data Set Link: https://www.kaggle.com/datasets/amohankumar/tesla-stock-price-prediction-dataset/data
1. Importing Required Libraries & Loading Data
import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns from sklearn.model_selection import train_test_split from sklearn.preprocessing import MinMaxScaler from sklearn.linear_model import LinearRegression from sklearn.svm import SVR from sklearn.metrics import mean_absolute_error, mean_squared_error
# Load Tesla stock market data stock_data = pd.read_csv('/path/to/tesla_stock_data.csv') # Replace with actual dataset path # Display first few rows stock_data.head()
Explanation:
- We import essential libraries for data analysis, visualization, and machine learning.
- Load the Tesla dataset and inspect the first few rows.
2. Exploratory Data Analysis (EDA)
Checking Data Information
# Summary of dataset stock_data.info() stock_data.describe()
Handling Missing Values
# Check for missing values stock_data.isnull().sum() # Fill missing values using forward fill method stock_data.fillna(method='ffill', inplace=True)
Visualizing Stock Price Trends
plt.figure(figsize=(12,6)) plt.plot(stock_data['Date'], stock_data['CloseAdj'], label='Adjusted Closing Price', color='blue') plt.xlabel('Date') plt.ylabel('Adjusted Closing Price') plt.title('Tesla Stock Price Trend') plt.legend() plt.show()
Explanation:
- We analyze dataset structure, check for missing values, and use forward fill to handle them.
- A line plot is used to visualize Tesla’s adjusted closing stock price trend over time.
3. Feature Engineering
Creating New Features
stock_data['Daily Return'] = stock_data['CloseAdj'].pct_change() stock_data['Moving Average'] = stock_data['CloseAdj'].rolling(window=10).mean()
Scaling Data
scaler = MinMaxScaler() scaled_features = scaler.fit_transform(stock_data[['CloseAdj']])
Explanation:
- We create Daily Return and Moving Average to capture stock market trends.
- The data is scaled using MinMaxScaler to improve model performance.
4. Splitting Data & Training Models
Splitting Data
X = stock_data[['Moving Average']].dropna() y = stock_data['CloseAdj'][X.index] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
Implementing Linear Regression
lr_model = LinearRegression() lr_model.fit(X_train, y_train) y_pred_lr = lr_model.predict(X_test)
Implementing Support Vector Regression
svr_model = SVR(kernel='rbf') svr_model.fit(X_train, y_train) y_pred_svr = svr_model.predict(X_test)
Explanation:
- We split data into training and testing sets.
- Implement Linear Regression and Support Vector Regression models for prediction.
5. Evaluating Model Performance
# Evaluate Linear Regression mae_lr = mean_absolute_error(y_test, y_pred_lr) mse_lr = mean_squared_error(y_test, y_pred_lr) # Evaluate SVR mae_svr = mean_absolute_error(y_test, y_pred_svr) mse_svr = mean_squared_error(y_test, y_pred_svr) print(f'Linear Regression - MAE: {mae_lr}, MSE: {mse_lr}') print(f'SVR - MAE: {mae_svr}, MSE: {mse_svr}')
Explanation:
- Mean Absolute Error (MAE) and Mean Squared Error (MSE) are used to evaluate models.
- Lower values indicate better predictive performance.
6. Visualizing Predictions
plt.figure(figsize=(10,6)) plt.plot(y_test.index, y_test, label='Actual', color='blue') plt.plot(y_test.index, y_pred_lr, label='Linear Regression', color='red') plt.plot(y_test.index, y_pred_svr, label='SVR', color='green') plt.xlabel('Date') plt.ylabel('Adjusted Closing Price') plt.title('Tesla Stock Price Prediction Comparison') plt.legend() plt.show()
Explanation:
- We compare actual vs predicted Tesla stock prices using Line plots for both models.
7. Conclusion & Future Improvements
- Linear Regression and Support Vector Regression provide basic stock price predictions.
- Further improvements could include LSTM models or Random Forest Regression for better accuracy.
- Adding more features like trading volume, volatility, and macroeconomic indicators could enhance performance.
Stock Price Prediction using Machine Learning in Python, Tesla stock prediction, stock market forecasting, ML stock price prediction, time series analysis, financial data modeling.