ArcGIS Pro 3.1.0: What is the python code for the Geographically Weighted Regression (GWR)?
In the screenshot below, I couldn’t figure out how to obtain the python code for the Geographically Weighted Regression (GWR) by which the dependent variable (y) can be expected based on the independent variables (x1, x2, x3, x4) considering the location of each observations where u column represents the x – coordinate and v column represents the y – coordinate
C:\... install folder ...\Resources\ArcToolBox\toolboxes\Spatial Statistics Tools.tbx\toolbox.module.py
line 3619 : contains the class GeographicallyWeightedRegression description
line 3864: conts the class GWR
you can follow the various imports fromtthat module
Not sure how can I can take the advantage of this python code to stick it in JupyterLab in order to apply the analysis
For JupyterLab?
you would have to provide all the input variables into this expression
ARCPY.GeographicallyWeightedRegression_analysis(
in_features, dependent_field, explanatory_field, out_feature,
kernel_type, band_width, distance, nn, weight_field, crw,
anaCellSize, in_pred, pred_field, out_pred)
I don't think that is what you want
I used the ChatGPT in order to get the python code for the GWR as per the request below
---
Write a python code to read the table saved here:
"C:\D\H3.xlsx"
Write a python code to predict the value of the dependent variable based on the geographically Weighted Regression (GWR) displaying how the weights of neighbouring observations are used to calculate the prediction of the focal observation
The first 4 columns are the independent variables
The fifth column is the dependent variable
The u column represents the x-coordinate
The v column represents the y coordinate
Use the Gaussian as the kernel function
Assume the bandwidth to be 400 meters
-------
The ChatGPT provided the code below:
--------
import pandas as pd
import numpy as np
from spreg import GWR
from spreg.utils import read_spatialdata
# Load the data from the Excel file
file_path = r'C:\D\H3.xlsx'
data = pd.read_excel(file_path)
# Define the independent variables (X1, X2, X3, X4)
X = data[['X1', 'X2', 'X3', 'X4']].values
# Define the dependent variable (y)
y = data['y'].values
# Define the coordinates (u and v)
u = data['u'].values
v = data['v'].values
# Define the bandwidth for the Gaussian kernel
bandwidth = 400 # meters
# Create a spatial weights matrix based on Gaussian kernel
distances = np.sqrt((u[:, np.newaxis] - u)**2 + (v[:, np.newaxis] - v)**2)
kernel_weights = np.exp(-distances**2 / (2 * bandwidth**2))
# Create a GWR model
model = GWR(y, X, kernel_weights)
# Fit the GWR model
results = model.fit()
# Predict the values of the dependent variable
predicted_values = results.predict(X)
# You can now use predicted_values for your predictions
print(predicted_values)
-------
However, running this code on the JupyterLab ends up with an error as below
spreg isn't an available package
You can do the same if you examine the code I referenced earlier
I have little knowledge in coding. All what I wanted is a python code for the Geographically Weighted Regression (GWR) that can work in Jupyter or Spyder
https://developers.arcgis.com/geoanalytics/tools/gwr/
https://github.com/geeksnome/machine-learning-made-easy/blob/master/locally_weighted_regression.py