Skip to content

Latest commit

 

History

History
109 lines (89 loc) · 3.66 KB

scikit_learn.md

File metadata and controls

109 lines (89 loc) · 3.66 KB

Scikit-learn is a powerful and widely-used machine learning library in Python. It provides simple and efficient tools for data analysis and modeling. Here are the basics you need to know:

1. Installation

You can install scikit-learn using pip:

pip install scikit-learn

2. Key Components

Scikit-learn has several key components that make it easy to implement various machine learning algorithms:

  1. Datasets: Built-in datasets and tools for loading external datasets.
  2. Preprocessing: Tools for preprocessing data such as scaling, normalization, and encoding.
  3. Models: A wide variety of machine learning algorithms for classification, regression, clustering, and more.
  4. Model Selection: Tools for evaluating and selecting models, such as cross-validation and hyperparameter tuning.
  5. Metrics: Functions for evaluating model performance using various metrics.

3. Basic Workflow

The typical workflow in scikit-learn involves the following steps:

  1. Loading Data:

    • Use built-in datasets or load your own data.
    from sklearn.datasets import load_iris
    data = load_iris()
    X, y = data.data, data.target
  2. Splitting Data:

    • Split the data into training and testing sets.
    from sklearn.model_selection import train_test_split
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
  3. Preprocessing:

    • Preprocess the data as needed (e.g., scaling, encoding).
    from sklearn.preprocessing import StandardScaler
    scaler = StandardScaler()
    X_train = scaler.fit_transform(X_train)
    X_test = scaler.transform(X_test)
  4. Training a Model:

    • Choose a model and train it on the training data.
    from sklearn.ensemble import RandomForestClassifier
    model = RandomForestClassifier()
    model.fit(X_train, y_train)
  5. Making Predictions:

    • Use the trained model to make predictions on the test data.
    y_pred = model.predict(X_test)
  6. Evaluating the Model:

    • Evaluate the model's performance using appropriate metrics.
    from sklearn.metrics import accuracy_score
    accuracy = accuracy_score(y_test, y_pred)
    print(f'Accuracy: {accuracy}')

4. Example: Iris Dataset

Let's put it all together with an example using the Iris dataset:

# 1. Load Data
from sklearn.datasets import load_iris
data = load_iris()
X, y = data.data, data.target

# 2. Split Data
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 3. Preprocess Data
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

# 4. Train Model
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier()
model.fit(X_train, y_train)

# 5. Make Predictions
y_pred = model.predict(X_test)

# 6. Evaluate Model
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy}')

Key Concepts and Functions

  • Estimator: Any object that learns from data (e.g., RandomForestClassifier).
  • fit(): Method to train the model.
  • predict(): Method to make predictions.
  • transform(): Method to apply preprocessing transformations.
  • fit_transform(): Method to fit and transform the data in one step.
  • score(): Method to evaluate the model's performance (sometimes available).

Scikit-learn's consistent API design makes it easy to swap out different models and preprocessing steps, allowing for flexible and rapid experimentation.