Programming for Data Science,Lec 14: Classification in Machine Learning using scikit-learn (sklearn)

Поделиться
HTML-код
  • Опубликовано: 3 окт 2024
  • #machinelearning #datascience #logisticregression
    First, you need to import the `LogisticRegression` class from the `linear_model` module in scikit-learn.
    ```
    from sklearn.linear_model import LogisticRegression
    ```
    Next, you initialize a `LogisticRegression` object. This step allows you to specify various parameters, such as the regularization strength (C), the solver to use for optimization, and the maximum number of iterations the solver performs.
    ```
    model = LogisticRegression()
    ```
    Fitting the model involves adjusting the weights to minimize the cost function using the training data. This is done using the `.fit()` method.
    ```
    model.fit(X_train, y_train)
    ```
    After the model has been fitted, you can use it to make predictions on new data using the `.predict()` method for class labels, or `.predict_proba()` for probabilities of each class.
    ```
    predictions = model.predict(X_test)
    probabilities = model.predict_proba(X_test)
    ```
    You can evaluate the performance of your model using various metrics such as accuracy, precision, and recall.
    ```
    from sklearn.metrics import accuracy_score, precision_score, recall_score
    accuracy = accuracy_score(y_test, predictions)
    precision = precision_score(y_test, predictions)
    recall = recall_score(y_test, predictions)
    ```

Комментарии •