The Machine Learning Guide for Predictive Accuracy: Interpolation and Extrapolation – Towards Data Science
class ModelFitterAndVisualizer: def __init__(self, X_train, y_train, y_truth, scaling=False, random_state=41): """ Initialize the ModelFitterAndVisualizer class with training and testing data.
Parameters: X_train (pd.DataFrame): Training data features y_train (pd.Series): Training data target y_truth (pd.Series): Ground truth for predictions scaling (bool): Flag to indicate if scaling should be applied random_state (int): Seed for random number generation """ self.X_train = X_train self.y_train = y_train self.y_truth = y_truth
self.initialize_models(random_state)
self.scaling = scaling
# Initialize models # ----------------------------------------------------------------- def initialize_models(self, random_state): """ Initialize the models to be used for fitting and prediction.
Parameters: random_state (int): Seed for random number generation """
# Define kernel for GPR kernel = 1.0 * RBF(length_scale=1.0) + WhiteKernel(noise_level=1.0)
# Define Ensemble Models Estimator # Decision Tree + Kernel Method estimators_rf_svr = [ ('rf', RandomForestRegressor(n_estimators=30, random_state=random_state)), ('svr', SVR(kernel='rbf')), ] estimators_rf_gpr = [ ('rf', RandomForestRegressor(n_estimators=30, random_state=random_state)), ('gpr', GaussianProcessRegressor(kernel=kernel, normalize_y=True, random_state=random_state)) ] # Decision Trees estimators_rf_xgb = [ ('rf', RandomForestRegressor(n_estimators=30, random_state=random_state)), ('xgb', xgb.XGBRegressor(random_state=random_state)), ]
self.models = [ SymbolicRegressor(random_state=random_state), SVR(kernel='rbf'), GaussianProcessRegressor(kernel=kernel, normalize_y=True, random_state=random_state), DecisionTreeRegressor(random_state=random_state), RandomForestRegressor(random_state=random_state), xgb.XGBRegressor(random_state=random_state), lgbm.LGBMRegressor(n_estimators=50, num_leaves=10, min_child_samples=3, random_state=random_state), VotingRegressor(estimators=estimators_rf_svr), StackingRegressor(estimators=estimators_rf_svr, final_estimator=RandomForestRegressor(random_state=random_state)), VotingRegressor(estimators=estimators_rf_gpr), StackingRegressor(estimators=estimators_rf_gpr, final_estimator=RandomForestRegressor(random_state=random_state)), VotingRegressor(estimators=estimators_rf_xgb), StackingRegressor(estimators=estimators_rf_xgb, final_estimator=RandomForestRegressor(random_state=random_state)), ]
# Define graph titles self.titles = [ "Ground Truth", "Training Points", "SymbolicRegressor", "SVR", "GPR", "DecisionTree", "RForest", "XGBoost", "LGBM", "Vote_rf_svr", "Stack_rf_svr__rf", "Vote_rf_gpr", "Stack_rf_gpr__rf", "Vote_rf_xgb", "Stack_rf_xgb__rf", ]
def fit_models(self): """ Fit the models to the training data.
Returns: self: Instance of the class with fitted models """ if self.scaling: scaler_X = MinMaxScaler() self.X_train_scaled = scaler_X.fit_transform(self.X_train) else: self.X_train_scaled = self.X_train.copy()
for model in self.models: model.fit(self.X_train_scaled, self.y_train) return self
def visualize_surface(self, x0, x1, width=400, height=500, num_panel_columns=5, vertical_spacing=0.06, horizontal_spacing=0, output=None, display=False, return_fig=False): """ Visualize the prediction surface for each model.
Parameters: x0 (np.ndarray): Meshgrid for feature 1 x1 (np.ndarray): Meshgrid for feature 2 width (int): Width of the plot height (int): Height of the plot output (str): File path to save the plot display (bool): Flag to display the plot """
num_plots = len(self.models) + 2 num_panel_rows = num_plots // num_panel_columns
whole_width = width * num_panel_columns whole_height = height * num_panel_rows
specs = [[{'type': 'surface'} for _ in range(num_panel_columns)] for _ in range(num_panel_rows)] fig = make_subplots(rows=num_panel_rows, cols=num_panel_columns, specs=specs, subplot_titles=self.titles, vertical_spacing=vertical_spacing, horizontal_spacing=horizontal_spacing)
for i, model in enumerate([None, None] + self.models): # Assign the subplot panels row = i // num_panel_columns + 1 col = i % num_panel_columns + 1
# Plot training points if i == 1: fig.add_trace(go.Scatter3d(x=self.X_train[:, 0], y=self.X_train[:, 1], z=self.y_train, mode='markers', marker=dict(size=2, color='darkslategray'), name='Training Data'), row=row, col=col)
surface = go.Surface(z=self.y_truth, x=x0, y=x1, showscale=False, opacity=.4) fig.add_trace(surface, row=row, col=col)
# Plot predicted surface for each model and ground truth else: y_pred = self.y_truth if model is None else model.predict(np.c_[x0.ravel(), x1.ravel()]).reshape(x0.shape) surface = go.Surface(z=y_pred, x=x0, y=x1, showscale=False) fig.add_trace(surface, row=row, col=col)
fig.update_scenes(dict( xaxis_title='x0', yaxis_title='x1', zaxis_title='y', ), row=row, col=col)
fig.update_layout(title='Model Predictions and Ground Truth', width=whole_width, height=whole_height)
# Change camera angle camera = dict( up=dict(x=0, y=0, z=1), center=dict(x=0, y=0, z=0), eye=dict(x=-1.25, y=-1.25, z=2) ) for i in range(num_plots): fig.update_layout(**{f'scene{i+1}_camera': camera})
if display: fig.show()
if output: fig.write_html(output)
if return_fig: return fig
Read this article:
The Machine Learning Guide for Predictive Accuracy: Interpolation and Extrapolation - Towards Data Science
- Predicting land suitability for wheat and barley crops using machine learning techniques - Nature - May 10th, 2025 [May 10th, 2025]
- AI and Machine Learning - Ribeiro Preto adopts Optibus to optimise public bus system - Smart Cities World - May 10th, 2025 [May 10th, 2025]
- Childrens Hospital Los Angeles Leads Development of First Machine Learning Tool to Predict Risk of Cisplatin-Induced Hearing Loss - Business Wire - May 10th, 2025 [May 10th, 2025]
- Google is using machine learning to help Android users avoid unwanted and dangerous notifications - BetaNews - May 10th, 2025 [May 10th, 2025]
- London School of Emerging Technology (LSET) Concludes International Workshop on Emerging AI & Machine Learning Innovation - Barchart.com - May 10th, 2025 [May 10th, 2025]
- Thermal performance, entropy generation, and machine learning insights of AlO-TiO hybrid nanofluids in turbulent flow - Nature - May 10th, 2025 [May 10th, 2025]
- Predicting the efficacy of bevacizumab on peritumoral edema based on imaging features and machine learning - Nature - May 10th, 2025 [May 10th, 2025]
- How AI and machine learning are supercharging video conferencing tools - European CEO - May 10th, 2025 [May 10th, 2025]
- The need for a risk-based approach to AI and machine learning in healthcare - Health Tech World - May 10th, 2025 [May 10th, 2025]
- Integrated bioinformatics, machine learning, and molecular docking reveal crosstalk genes and potential drugs between periodontitis and systemic lupus... - May 10th, 2025 [May 10th, 2025]
- Adversarial Machine Learning in Detecting Inauthentic Behavior on Social Platforms - AiThority - May 10th, 2025 [May 10th, 2025]
- Exploring crop health and its associations with fungal soil microbiome composition using machine learning applied to remote sensing data - Nature - May 10th, 2025 [May 10th, 2025]
- Trust-based model and machine learning improve forest fire detection system - International Fire & Safety Journal - May 10th, 2025 [May 10th, 2025]
- A machine learning engineer shares the rsums that landed her jobs at Meta and X and what she'd change if she applied again - Business Insider Africa - May 5th, 2025 [May 5th, 2025]
- Recentive Analytics v. Fox: The Federal Circuit Provides Analysis on the Patent Eligibility of Machine Learning Claims - Mintz - May 5th, 2025 [May 5th, 2025]
- A machine learning engineer shares the rsums that landed her jobs at Meta and X and what she'd change if she applied again - Business Insider - May 5th, 2025 [May 5th, 2025]
- Enhancing urban resilience through machine learning-supported flood risk assessment: integrating flood susceptibility with building function... - May 5th, 2025 [May 5th, 2025]
- MicroAlgo Inc. Develops Classifier Auto-Optimization Technology Based on Variational Quantum Algorithms, Accelerating the Advancement of Quantum... - May 5th, 2025 [May 5th, 2025]
- Enhanced metal ion adsorption using ZnO-MXene nanocomposites with machine learning-based performance prediction - Nature - May 5th, 2025 [May 5th, 2025]
- Integrating SHAP analysis with machine learning to predict postpartum hemorrhage in vaginal births - BMC Pregnancy and Childbirth - May 5th, 2025 [May 5th, 2025]
- Machine learning provide new insights into how the brain responds to heroin use - News-Medical - May 2nd, 2025 [May 2nd, 2025]
- Machine Learning and AI in Basic HIV Research: From Big Data Analysis to Large Language Models - UNC Gillings School of Global Public Health - May 2nd, 2025 [May 2nd, 2025]
- Machine learning brings new insights to cells role in addiction, relapse - University of Cincinnati - May 2nd, 2025 [May 2nd, 2025]
- UH/UC Researchers Use Machine Learning to Map Brain Changes from Heroin Addiction - University of Houston - May 2nd, 2025 [May 2nd, 2025]
- Machine Learning Algorithm Predicts Shiba Inu Price In May You Should See This - The Crypto Update - May 2nd, 2025 [May 2nd, 2025]
- Seerist partners with SOCOM to enhance AI and machine learning for special operations - Defence Industry Europe - May 2nd, 2025 [May 2nd, 2025]
- How machine learning can spark many discoveries in science and medicine - The Indian Express - April 30th, 2025 [April 30th, 2025]
- Machine learning frameworks to accurately estimate the adsorption of organic materials onto resin and biochar - Nature - April 30th, 2025 [April 30th, 2025]
- Gene Therapy Research Roundup: Gene Circuits and Controlling Capsids With Machine Learning - themedicinemaker.com - April 30th, 2025 [April 30th, 2025]
- Seerist and SOCOM Enter Five-Year CRADA to Advance AI and Machine Learning for Operations - PRWeb - April 30th, 2025 [April 30th, 2025]
- Machine learning models for estimating the overall oil recovery of waterflooding operations in heterogenous reservoirs - Nature - April 30th, 2025 [April 30th, 2025]
- Machine learning-based quantification and separation of emissions and meteorological effects on PM - Nature - April 30th, 2025 [April 30th, 2025]
- Protein interactions, network pharmacology, and machine learning work together to predict genes linked to mitochondrial dysfunction in hypertrophic... - April 30th, 2025 [April 30th, 2025]
- AQR Bets on Machine Learning as Asness Becomes AI Believer - Bloomberg.com - April 30th, 2025 [April 30th, 2025]
- Darktrace enhances Cyber AI Analyst with advanced machine learning for improved threat investigations - Industrial Cyber - April 21st, 2025 [April 21st, 2025]
- Infrared spectroscopy with machine learning detects early wood coating deterioration - Phys.org - April 21st, 2025 [April 21st, 2025]
- A simulation-driven computational framework for adaptive energy-efficient optimization in machine learning-based intrusion detection systems - Nature - April 21st, 2025 [April 21st, 2025]
- Machine learning model to predict the fitness of AAV capsids for gene therapy - EurekAlert! - April 21st, 2025 [April 21st, 2025]
- An integrated approach of feature selection and machine learning for early detection of breast cancer - Nature - April 21st, 2025 [April 21st, 2025]
- Predicting cerebral infarction and transient ischemic attack in healthy individuals and those with dysmetabolism: a machine learning approach combined... - April 21st, 2025 [April 21st, 2025]
- Autolomous CEO Discusses AI and Machine Learning Applications in Pharmaceutical Development and Manufacturing with Pharmaceutical Technology -... - April 21st, 2025 [April 21st, 2025]
- Machine Learning Interpretation of Optical Spectroscopy Using Peak-Sensitive Logistic Regression - ACS Publications - April 21st, 2025 [April 21st, 2025]
- Estimated glucose disposal rate outperforms other insulin resistance surrogates in predicting incident cardiovascular diseases in... - April 21st, 2025 [April 21st, 2025]
- Machine learning-based differentiation of schizophrenia and bipolar disorder using multiscale fuzzy entropy and relative power from resting-state EEG... - April 12th, 2025 [April 12th, 2025]
- Increasing load factor in logistics and evaluating shipment performance with machine learning methods: A case from the automotive industry - Nature - April 12th, 2025 [April 12th, 2025]
- Machine learning-based prediction of the thermal conductivity of filling material incorporating steelmaking slag in a ground heat exchanger system -... - April 12th, 2025 [April 12th, 2025]
- Do LLMs Know Internally When They Follow Instructions? - Apple Machine Learning Research - April 12th, 2025 [April 12th, 2025]
- Leveraging machine learning in precision medicine to unveil organochlorine pesticides as predictive biomarkers for thyroid dysfunction - Nature - April 12th, 2025 [April 12th, 2025]
- Analysis and validation of hub genes for atherosclerosis and AIDS and immune infiltration characteristics based on bioinformatics and machine learning... - April 12th, 2025 [April 12th, 2025]
- AI and Machine Learning - Bentley and Google partner to improve asset analytics - Smart Cities World - April 12th, 2025 [April 12th, 2025]
- Where to find the next Earth: Machine learning accelerates the search for habitable planets - Phys.org - April 10th, 2025 [April 10th, 2025]
- Concurrent spin squeezing and field tracking with machine learning - Nature - April 10th, 2025 [April 10th, 2025]
- This AI Paper Introduces a Machine Learning Framework to Estimate the Inference Budget for Self-Consistency and GenRMs (Generative Reward Models) -... - April 10th, 2025 [April 10th, 2025]
- UCI researchers study use of machine learning to improve stroke diagnosis, access to timely treatment - UCI Health - April 10th, 2025 [April 10th, 2025]
- Assessing dengue forecasting methods: a comparative study of statistical models and machine learning techniques in Rio de Janeiro, Brazil - Tropical... - April 10th, 2025 [April 10th, 2025]
- Machine learning integration of multimodal data identifies key features of circulating NT-proBNP in people without cardiovascular diseases - Nature - April 10th, 2025 [April 10th, 2025]
- How AI, Data Science, And Machine Learning Are Shaping The Future - Forbes - April 10th, 2025 [April 10th, 2025]
- Development and validation of interpretable machine learning models to predict distant metastasis and prognosis of muscle-invasive bladder cancer... - April 10th, 2025 [April 10th, 2025]
- From fax machines to machine learning: The fight for efficiency - HME News - April 10th, 2025 [April 10th, 2025]
- Carbon market and emission reduction: evidence from evolutionary game and machine learning - Nature - April 10th, 2025 [April 10th, 2025]
- Infleqtion Unveils Contextual Machine Learning (CML) at GTC 2025, Powering AI Breakthroughs with NVIDIA CUDA-Q and Quantum-Inspired Algorithms - Yahoo... - March 22nd, 2025 [March 22nd, 2025]
- Karlie Kloss' coding nonprofit offering free AI and machine learning workshop this weekend - KSDK.com - March 22nd, 2025 [March 22nd, 2025]
- Machine learning reveals distinct neuroanatomical signatures of cardiovascular and metabolic diseases in cognitively unimpaired individuals -... - March 22nd, 2025 [March 22nd, 2025]
- Machine learning analysis of cardiovascular risk factors and their associations with hearing loss - Nature.com - March 22nd, 2025 [March 22nd, 2025]
- Weekly Recap: Dual-Cure Inks, AI And Machine Learning Top This Weeks Stories - Ink World Magazine - March 22nd, 2025 [March 22nd, 2025]
- Network-based predictive models for artificial intelligence: an interpretable application of machine learning techniques in the assessment of... - March 22nd, 2025 [March 22nd, 2025]
- Machine learning aids in detection of 'brain tsunamis' - University of Cincinnati - March 22nd, 2025 [March 22nd, 2025]
- AI & Machine Learning in Database Management: Studying Trends and Applications with Nithin Gadicharla - Tech Times - March 22nd, 2025 [March 22nd, 2025]
- MicroRNA Biomarkers and Machine Learning for Hypertension Subtyping - Physician's Weekly - March 22nd, 2025 [March 22nd, 2025]
- Machine Learning Pioneer Ramin Hasani Joins Info-Tech's "Digital Disruption" Podcast to Explore the Future of AI and Liquid Neural Networks... - March 22nd, 2025 [March 22nd, 2025]
- Predicting HIV treatment nonadherence in adolescents with machine learning - News-Medical.Net - March 22nd, 2025 [March 22nd, 2025]
- AI And Machine Learning In Ink And Coatings Formulation - Ink World Magazine - March 22nd, 2025 [March 22nd, 2025]
- Counting whales by eavesdropping on their chatter, with help from machine learning - Mongabay.com - March 22nd, 2025 [March 22nd, 2025]
- Associate Professor - Artificial Intelligence and Machine Learning job with GALGOTIAS UNIVERSITY | 390348 - Times Higher Education - March 22nd, 2025 [March 22nd, 2025]
- Innovative Machine Learning Tool Reveals Secrets Of Marine Microbial Proteins - Evrim Aac - March 22nd, 2025 [March 22nd, 2025]
- Exploring the role of breastfeeding, antibiotics, and indoor environments in preschool children atopic dermatitis through machine learning and hygiene... - March 22nd, 2025 [March 22nd, 2025]
- Applying machine learning algorithms to explore the impact of combined noise and dust on hearing loss in occupationally exposed populations -... - March 22nd, 2025 [March 22nd, 2025]
- 'We want them to be the creators': Karlie Kloss' coding nonprofit offering free AI and machine learning workshop this weekend - KSDK.com - March 22nd, 2025 [March 22nd, 2025]
- New headset reads minds and uses AR, AI and machine learning to help people with locked-in-syndrome communicate with loved ones again - PC Gamer - March 22nd, 2025 [March 22nd, 2025]
- Enhancing cybersecurity through script development using machine and deep learning for advanced threat mitigation - Nature.com - March 11th, 2025 [March 11th, 2025]