[python] THIS IS GREAT
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- import time
- import pandas as pd
- from sklearn.ensemble import RandomForestRegressor
- from sklearn.model_selection import train_test_split
- from sklearn.metrics import mean_squared_error, mean_absolute_error
- # Load the stock market data from the CSV file
- data = pd.read_csv('stock_market_data.csv')
- # Extract relevant features (Open price and Volume) and target variable (Close price)
- features = data[['Open', 'Volume']]
- target = data['Close']
- # Split the data into training and testing sets
- X_train, X_test, y_train, y_test = train_test_split(
- features, target, test_size=0.2, random_state=42
- )
- # Initialize the Random Forest model
- rf_model = RandomForestRegressor()
- # Record the start time for training
- start_time = time.time()
- # Train the Random Forest model
- rf_model.fit(X_train, y_train)
- # Calculate the training time
- training_time = time.time() - start_time
- # Make predictions on the testing set
- rf_predictions = rf_model.predict(X_test)
- # Evaluate the model
- mse = mean_squared_error(y_test, rf_predictions)
- mae = mean_absolute_error(y_test, rf_predictions)
- rmse = mean_squared_error(y_test, rf_predictions, squared=False) # Calculate RMSE
- # Display the results
- print("Random Forest Model Metrics:")
- print(f"Mean Squared Error (MSE): {mse}")
- print(f"Mean Absolute Error (MAE): {mae}")
- print(f"Root Mean Squared Error (RMSE): {rmse}")
- print(f"Training Time: {training_time} seconds")
Editor
You can edit this paste and save as new: