[python] THIS IS GREAT

Viewer

copydownloadembedprintName: THIS IS GREAT
  1. import time
  2. import pandas as pd
  3. from sklearn.ensemble import RandomForestRegressor
  4. from sklearn.model_selection import train_test_split
  5. from sklearn.metrics import mean_squared_error, mean_absolute_error
  6.  
  7. # Load the stock market data from the CSV file
  8. data = pd.read_csv('stock_market_data.csv')
  9.  
  10. # Extract relevant features (Open price and Volume) and target variable (Close price)
  11. features = data[['Open', 'Volume']]
  12. target = data['Close']
  13.  
  14. # Split the data into training and testing sets
  15. X_train, X_test, y_train, y_test = train_test_split(
  16.     features, target, test_size=0.2, random_state=42
  17. )
  18.  
  19. # Initialize the Random Forest model
  20. rf_model = RandomForestRegressor()
  21.  
  22. # Record the start time for training
  23. start_time = time.time()
  24.  
  25. # Train the Random Forest model
  26. rf_model.fit(X_train, y_train)
  27.  
  28. # Calculate the training time
  29. training_time = time.time() - start_time
  30.  
  31. # Make predictions on the testing set
  32. rf_predictions = rf_model.predict(X_test)
  33.  
  34. # Evaluate the model
  35. mse = mean_squared_error(y_test, rf_predictions)
  36. mae = mean_absolute_error(y_test, rf_predictions)
  37. rmse = mean_squared_error(y_test, rf_predictions, squared=False)  # Calculate RMSE
  38.  
  39. # Display the results
  40. print("Random Forest Model Metrics:")
  41. print(f"Mean Squared Error (MSE): {mse}")
  42. print(f"Mean Absolute Error (MAE): {mae}")
  43. print(f"Root Mean Squared Error (RMSE): {rmse}")
  44. print(f"Training Time: {training_time} seconds")
  45.  

Editor

You can edit this paste and save as new:


File Description
  • THIS IS GREAT
  • Paste Code
  • 28 Feb-2024
  • 1.41 Kb
You can Share it: