diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..f960933b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "cSpell.words": [ + "numpy", + "statsmodels" + ] +} \ No newline at end of file diff --git a/contrib/advanced-python/Stepwise_Regression.md b/contrib/advanced-python/Stepwise_Regression.md new file mode 100644 index 00000000..dbb657af --- /dev/null +++ b/contrib/advanced-python/Stepwise_Regression.md @@ -0,0 +1,73 @@ +## Stepwise Regression + + +# Definition +Stepwise Regression is a method of fitting regression models in which the choice of predictive variables is carried out by an automatic procedure. This technique is useful for identifying a subset of variables that have the most significant impact on the dependent variable. + +# Purpose +The main goal of Stepwise Regression is to simplify the model by including only the most significant variables, thereby improving model interoperability and performance. + +# Types +Forward Selection: Starts with no variables in the model, testing each variable one by one, and adding them if they improve the model significantly. +Backward Elimination: Starts with all candidate variables and removes the least significant variables one at a time. +Bidirectional Elimination: A combination of forward selection and backward elimination. +# Steps Involved + +## Forward Selection: + +Begin with an empty model. +Add the predictor that improves the model the most (lowest p-value). +Continue adding predictors one by one until no further significant improvement is made. +Backward Elimination: + +Start with all candidate predictors. +Remove the predictor with the highest p-value (least significant). +Continue removing predictors until all remaining predictors are statistically significant. +Bidirectional Elimination: + +Combine forward selection and backward elimination steps iteratively. +Add significant predictors and remove non-significant ones in each step until the model stabilizes. +Practical Example in Python +Here's an example using the statsmodels library: + +# code +import statsmodels.api as sm +import pandas as pd +import numpy as np + +# Sample Data +np.random.seed(0) +X = pd.DataFrame({ + 'X1': np.random.randn(100), + 'X2': np.random.randn(100), + 'X3': np.random.randn(100) +}) +y = 1 + 2*X['X1'] + 0.5*X['X2'] + np.random.randn(100) + +# Forward Selection +def forward_selection(X, y): + initial_features = [] + remaining_features = list(X.columns) + best_features = [] + while remaining_features: + scores_with_candidates = [] + for candidate in remaining_features: + model = sm.OLS(y, sm.add_constant(pd.DataFrame(X[initial_features + [candidate]]))).fit() + score = model.aic + scores_with_candidates.append((score, candidate)) + scores_with_candidates.sort() + best_score, best_candidate = scores_with_candidates[0] + initial_features.append(best_candidate) + remaining_features.remove(best_candidate) + best_features.append(best_candidate) + return best_features + +# Applying Forward Selection +selected_features = forward_selection(X, y) +print("Selected features:", selected_features) + +# Building final model with selected features +final_model = sm.OLS(y, sm.add_constant(X[selected_features])).fit() +print(final_model.summary()) + +This code demonstrates forward selection by iteratively adding features to the model and selecting the ones that improve the model's AIC (Akaike Information Criterion) the most. The final model is then built using the selected features. \ No newline at end of file diff --git a/contrib/advanced-python/magic_methods.md b/contrib/advanced-python/magic_methods.md new file mode 100644 index 00000000..fe5ce6a5 --- /dev/null +++ b/contrib/advanced-python/magic_methods.md @@ -0,0 +1,88 @@ +## Introduction to Magic Methods + +Magic methods in Python, also known as dunder (double underscore) methods, are special methods that begin and end with double underscores (__). +They allow you to define the behavior of your objects in specific contexts, such as when they are created, represented as strings, compared, or used in arithmetic operations. These methods make classes more powerful and flexible. + +## List of Common Magic Methods + +__init__(self, ...): Constructor method, called when an instance is created. +__str__(self): Defines the string representation of an object. +__repr__(self): Defines the "official" string representation of an object. +__len__(self): Returns the length of the object. +__getitem__(self, key): Allows indexing using square brackets. +__setitem__(self, key, value): Assigns a value to the specified key. +__delitem__(self, key): Deletes the specified key-value pair. +__iter__(self): Returns an iterator for the object. +__next__(self): Returns the next item from an iterator. +__call__(self, ...): Makes an instance callable like a function. +__eq__(self, other): Defines behavior for the equality operator (==). +__add__(self, other): Defines behavior for the addition operator (+). + + +## Examples + +__init__ and __str__ + +## code + +class Person: + def __init__(self, name, age): + self.name = name + self.age = age + + def __str__(self): + return f"{self.name}, {self.age} years old" + +p = Person("Alice", 30) +print(p) # Output: Alice, 30 years old +__repr__ + +## code +class Person: + def __init__(self, name, age): + self.name = name + self.age = age + + def __repr__(self): + return f"Person(name={self.name!r}, age={self.age})" + +p = Person("Alice", 30) +print(repr(p)) # Output: Person(name='Alice', age=30) +__getitem__ and __setitem__ + +## code +class CustomList: + def __init__(self, elements): + self.elements = elements + + def __getitem__(self, index): + return self.elements[index] + + def __setitem__(self, index, value): + self.elements[index] = value + +clist = CustomList([1, 2, 3]) +print(clist[1]) + +# Output: 2 + +clist[1] = 42 +print(clist[1]) +# Output: 42 +__len__ + + +# code + +class CustomList: + def __init__(self, elements): + self.elements = elements + + def __len__(self): + return len(self.elements) + +clist = CustomList([1, 2, 3]) +print(len(clist)) +# Output: 3 + +By implementing these magic methods, you can customize how your classes and objects interact with built-in Python functions and operators, making them more intuitive and versatile. \ No newline at end of file

AltStyle によって変換されたページ (->オリジナル) /