Skip to main content

a backward feature elimination followed with forwared features selection strategy.

Project description

out_and_in

** out_and_in ** is a backward feature elimination followed with forwared features selection strategy.

Basic Idea

This function will itinerary remove one feature and from features, if the metrics has beed optimized, we will add it to dropped-features. we will drop dropped-features after that repeat, and calculate a metrics based on remaining features, and add those dropped_fatures back to the remaining features, if the performance has been increased, we will remove that features from dropped features. and finially, it will return dropped features.

Instalation

pip intall out_and_in

Example

We will use the auto_mpg as demo for the selection process of out_and_in auto_mpg include 80 features and 1460 entries. those features include numerical features and categorical features

import pandas as pd
from main import outin
from sklearn.datasets import fetch_openml
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error
from math import sqrt

# Load dataset
auto_mpg = fetch_openml(data_id=42165)
X = auto_mpg.data
y = auto_mpg.target
print(f"the shape of X: {X.shape()}")
# encoded X
X_encoded = pd.get_dummies(X, drop_first=True)
# fill NA with mean
X_encoded = X_encoded.fillna(X_encoded.mean())


def model_train(X: pd.DataFrame,
                y: pd.DataFrame):
 """
 This function will train a basic model and return a metric for that model , we take random forest as a example here.
 :param X: pandas dataframe, it will include all features and the categorical features have been encoded.
 :param y: pandas datasrame, it will include prediction target
 :return: metric.
 """

 # Split data into training and testing sets
 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

 # Initialize the Random Forest model
 rf_model = RandomForestRegressor(n_estimators=100, random_state=42)

 # Train the model
 rf_model.fit(X_train, y_train)

 # Make predictions
 y_pred = rf_model.predict(X_test)

 # Evaluate the model
 rmse = sqrt(mean_squared_error(y_test, y_pred))

 return rmse


# select features
dropped_features = outin(X, y, model_train)
print(f"dropped features: {dropped_features}")

Project details


Release history Release notifications | RSS feed

This version

1.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

out_and_in-1.1.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

out_and_in-1.1-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file out_and_in-1.1.tar.gz.

File metadata

  • Download URL: out_and_in-1.1.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for out_and_in-1.1.tar.gz
Algorithm Hash digest
SHA256 ff7e511a633120f8bfde09128f2d59508da38596675cb0e292c6274ce77794a7
MD5 55b291b0d506be3e1ebea2739350393e
BLAKE2b-256 21815f29ef2acc0414b3d7874f10a0e6072863020139c3d9f17b74cb777c0568

See more details on using hashes here.

File details

Details for the file out_and_in-1.1-py3-none-any.whl.

File metadata

  • Download URL: out_and_in-1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for out_and_in-1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 23496e78f2f57a268c0ffa803658fb156b1dbe8b0cffc293c09ad3678380e2a4
MD5 9dcdb75c49861a3cfebc5763b1305a30
BLAKE2b-256 63bceac4540738f115788c68be5c833fd8400fa2960d58236394315c936fae70

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page