#! /usr/bin/env python3 # def ram_regression_decision ( ): #*****************************************************************************80 # ## ram_regression_decision() uses a decision tree for RAM price regression. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 19 July 2023 # # Author: # # Andreas Mueller, Sarah Guido. # Modifications by John Burkardt. # # Reference: # # Andreas Mueller, Sarah Guido, # Introduction to Machine Learning with Python, # OReilly, 2017, # ISBN: 978-1-449-36941-5 # from sklearn.tree import export_graphviz import graphviz import matplotlib.pyplot as plt import mglearn import numpy as np import os import pandas as pd import platform import sklearn print ( '' ) print ( 'ram_regression_decision():' ) print ( ' Python version: ' + platform.python_version ( ) ) print ( ' scikit-learn version: '+ sklearn.__version__ ) # # Read the dataset. # print ( '' ) print ( ' Retrieve the RAM price dataset, (X, y).' ) ram_prices = pd.read_csv ( 'ram_price.csv' ) # # Plot the data. # plt.clf ( ) plt.semilogy ( ram_prices.date, ram_prices.price ) plt.grid ( True ) plt.xlabel ( 'Year' ) plt.ylabel ( 'Price in $/Mbyte' ) plt.title ( 'Price for RAM Megabyte, semilog plot.' ) filename = 'ram_data.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) data_train = ram_prices[ram_prices.date < 2000] data_test = ram_prices[ram_prices.date >= 2000] # # Predict prices. # X_train = np.array ( data_train.date ) X_train = X_train[:,np.newaxis] y_train = np.log ( data_train.price ) from sklearn.tree import DecisionTreeRegressor tree = DecisionTreeRegressor ( max_depth = 3 ).fit ( X_train, y_train ) X_all = np.array ( ram_prices.date ) X_all = X_all[:,np.newaxis] pred = tree.predict ( X_all ) price_tree = np.exp ( pred ) plt.clf ( ) plt.semilogy ( data_train.date, data_train.price, label = 'Training data' ) plt.semilogy ( data_test.date, data_test.price, label = 'Test data' ) plt.semilogy ( ram_prices.date, price_tree, label = 'Decision tree prediction' ) plt.grid ( True ) plt.xlabel ( 'Year' ) plt.ylabel ( 'Price in $/Mbyte' ) plt.title ( 'Price for RAM Megabyte, semilog plot.' ) plt.legend ( ) filename = 'ram_prediction.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) # # Terminate. # print ( '' ) print ( 'ram_regression_decision():' ) print ( ' Normal end of execution.' ) return def timestamp ( ): #*****************************************************************************80 # ## timestamp() prints the date as a timestamp. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 21 August 2019 # # Author: # # John Burkardt # import time t = time.time ( ) print ( time.ctime ( t ) ) return if ( __name__ == '__main__' ): timestamp ( ) ram_regression_decision ( ) timestamp ( )