這樣問:
Please give a complete Python code for this one: 學生可以通過使用機器學習模型(如線性回歸、支持向量機、神經網絡等)來分析曆史股票數據,並預測未來的股價。 Use NVDA as a example.
得到答案:
Certainly! Below is a complete Python code using Machine Learning to analyze historical stock data for NVDA (NVIDIA) and predict future stock prices using Linear Regression. This is a simple example, and you can apply other machine learning models (such as Support Vector Machines, Neural Networks) similarly, but for this example, we'll start with linear regression.
You'll need libraries such as yfinance
, pandas
, numpy
, sklearn
, and matplotlib
to fetch the stock data, process it, and build the machine learning model.
Here is the complete code:
Install required libraries:
pip install yfinance scikit-learn matplotlib pandas numpy
Code:
import yfinance as yf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
# Download historical data for NVDA (NVIDIA)
symbol = 'NVDA'
data = yf.download(symbol, start='2010-01-01', end='2023-01-01')
# Display the first few rows of the dataset
print(data.head())
# Create the "Target" column, which is the stock price that we want to predict (Next day's Close price)
data['Target'] = data['Close'].shift(-1)
# Remove the last row because it doesn't have a "Target"
data = data[:-1]
# Select features (independent variables) to predict the Target (stock price)
features = ['Open', 'High', 'Low', 'Close', 'Volume']
X = data[features]
y = data['Target']
# Split the data into training and testing sets (80% training, 20% testing)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Initialize the Linear Regression model
model = LinearRegression()
# Train the model using the training data
model.fit(X_train, y_train)
# Make predictions on the testing data
y_pred = model.predict(X_test)
# Evaluate the model by calculating the Mean Squared Error
mse = mean_squared_error(y_test, y_pred)
print(f"Mean Squared Error (MSE): {mse}")
# Plot the predicted values vs actual values
plt.figure(figsize=(10, 6))
plt.plot(y_test.values, label='Actual Stock Price')
plt.plot(y_pred, label='Predicted Stock Price')
plt.title(f"NVDA Stock Price Prediction (Actual vs Predicted)")
plt.xlabel('Time')
plt.ylabel('Stock Price')
plt.legend()
plt.show()
# Predict the next day's stock price using the last row of data (most recent data)
latest_data = data[features].iloc[-1:].values
next_day_prediction = model.predict(latest_data)
print(f"Predicted next day's stock price for NVDA: ${next_day_prediction[0]:.2f}")
Explanation:
- Data Download: We use
yfinance
to download historical stock data for NVDA from 2010 to 2023. - Target Creation: The
Target
is the next day's closing price, which we shift theClose
column by -1 day. - Feature Selection: We use the stock's Open, High, Low, Close, and Volume as features to predict the Target (next day's closing price).
- Model: We train a Linear Regression model on the training data and use it to predict the stock price on the testing data.
- Evaluation: We evaluate the model's performance using Mean Squared Error (MSE) and visualize the actual vs. predicted stock prices on a graph.
- Prediction: Finally, we predict the next day's stock price using the most recent data available.
Notes:
- Feature Engineering: You can improve the model by adding more features (like technical indicators such as Moving Averages, RSI, MACD, etc.).
- Model Complexity: You can use more sophisticated models like Support Vector Machines (SVM) or Neural Networks (e.g., LSTM for time series forecasting).
- Data Period: Make sure you adjust the
start
andend
dates as per your needs.
Example Output:
- Mean Squared Error (MSE) of the model, which indicates how well the model is predicting stock prices (lower MSE = better performance).
- A graph comparing Actual vs Predicted Stock Prices.
- The Predicted next day's stock price for NVDA.
Enhancements:
- Technical Indicators: You can add technical indicators such as Moving Averages, RSI, MACD, etc., as features to make your model more sophisticated.
- Advanced Models: You can also experiment with more advanced models like SVR (Support Vector Regression) or even Deep Learning models (e.g., LSTM for sequential data).
This example gives a starting point, and you can further enhance it based on your specific needs and exploration.