This code has two classes
- Preprocess - For preprocessing and normalizing the data
- Descent - For performing the gradient descent
Descent Class
Constructor
def __init__(self,X:list,y:list,epoch:int,method='linear',theta=None,alpha=0.01,decimals=5) -> None:
# Usage
# Goal - to construct a linear regression for specified hyperparameters
gd = Descent(X=X_norm, y=y_norm, epoch=5000,alpha=0.01)
Constructs the Descent instance with the specified hyperparameters
Parameters:
- X (list): The independent variables
- y (list): The dependent variable
- epoch (int): The number of iterations to be performed during regression
- Method (str, optional): The method by which you would like to solve the gradient descent problem. Defaults to 'linear'
- theta (list): The initialized weights/thetas for regression. Defaults to None
- alpha (float): The learning rate for the regression. Defaults to 0.01
- decimals (int, optional): The number of decimal places till which the early stopping can be implpemented. Defaults to 5.
Fit function
def fit(self)->tuple:
# Goal - to calibrate the linear regression model
pastCost,pastTheta,stopEpoch = gd.fit()
Calibrates the coefficient for gradient descent
Returns:
- pastCost - The history of costs incurred by the model while performing the iterations
- pastTheta - The history of thetas calculated by the model while performing the iterations
- stopEpoch - The epoch at which the gradient descent model converged for the specified decimal places
Predict function
def predict(self, values:list)->tuple:
# Goal - to predict the coefficients for the test set
bestTheta, preds = gd.predict(X_test)
Predicts the output for the specified values
Parameters
- values - The values for which you would like to perform the predictions
Returns
- bestTheta - The optimised value for theta after gradient descent
- predictions - The predictions for those values
Attributes
@property
def summary(self)->pd.DataFrame:
Prints the summary of the regression along with the necessary descriptive statistics
Returns the following metrics
- Skewness of residuals
- Kurtosis of residuals
- Jarque Bera Coefficient
- Jarque Bera p-value
If the model is linear, it also returns
- R Squared
- Adjusted R Squared
- Mean Squared Error
If the model is logistic, it also returns
- Precision
- Recall
- F1 Score
Preprocess class
Constructor
def __init__(self,data):
# Goal - to create a preprocess instance for x
X_scaler = Preprocess(X)
Stores the mean and standard deviation of the data for future transformations
transform function
def transform(self,values=None,add_ones=False)->np.array:
# Goal - to normalize the data and concatenate ones in the begining
X1 = X_scaler.transform(add_ones=True)
Normalizes the inputs by using the formula x_norm = (x-mean(x))/std(x)
Arguments:
- values - The values on which you want to apply the transformation, if not given then it transforms the data passed to it in the constructor
- add_ones (bool, optional): Whether you want to add ones for intercept or not. Defaults to True.
Returns the normalized data
inverse_transform function
def inverse_transform(self,inp):
#goal - to invert the transformation on the data
x_rescaled = X_scaler.inverse_transform()
Reverses the normalization by using the formula x = (x_norm*std(x))+mean(x)
Arguments
- inp (np.array or pd.Series or pd.DataFrame): The normalized data which you would like to convert to original data
Returns the converted data
Metadata
Release files for GradientDescent 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| GradientDescent-0.0.2.tar.gz | 19.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| GradientDescent-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 37.5 kB
Release files / GradientDescent-0.0.2.tar.gz
| Download URL | GradientDescent-0.0.2.tar.gz |
|---|---|
| Size | 19.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
32163f85e6b930d15b08880260dbf77206aed060b5d7c1c858e0f635dc217878
|
|
BLAKE2b-256 checksum How to use checksums |
10a1ab6108c31a09ad0f359947555e38864be17ee1da30386dd9a7961da8d450
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6
|
Release files / GradientDescent-0.0.2-py3-none-any.whl
| Download URL | GradientDescent-0.0.2-py3-none-any.whl |
|---|---|
| Size | 18.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c7b741a3baeecfef1c44b168d918178538ada2dcdbd7638c2d00a3b11bb0eb46
|
|
BLAKE2b-256 checksum How to use checksums |
fbee4c17ff3b53f8abc16a671a77856fded9ada67bf3ed50fc305c5ddfed1263
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6
|
Security for GradientDescent 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.