[PYTHON] Machine learning logistic regression

○ The main points of this article Note that I learned logistic regression

Logistic regression: ・ Algorithm for predicting classification problems (It's called regression, but it's used in classification) ・ Classification is performed by calculating the probability that the data belongs to each class. (Outputs such as class 1: 0.3, class 2: 0.5, class 3: 0.2) ・ Supervised learning

○ Source code (Python)

Logistic regression model


#Logistic regression
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score

#Load iris data
data = load_iris()
X = pd.DataFrame(data.data, columns=data.feature_names)
y = pd.DataFrame(data.target, columns=["Species"])
df #Display iris data

image.png ・ ・ ・ ・ ・ ・ ・ ・ ・ ・ image.png

Four quantitative variables: Sepal.Length (sepal length), Sepal.Width (septal width), Petal.Length (petal length), Petal.Width (petal width) Species (seed, setosa, versicolor, virginica)

○ Source code (Python)

Logistic regression model (continued)


#Creating, learning, and predicting logistic regression models
model = LogisticRegression()
model.fit(X, y)
y_pred = model.predict(X)
print(y_pred) #Predicted results for 150 iris data

#Model evaluation
print(mean_squared_error(y, y_pred)) #Mean squared error. The smaller the better
print(r2_score(y, y_pred)) #Coefficient of determination. The larger the value between 0 and 1, the better.

Execution result [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2] 0.02666666666666667 0.96

・ I think it is the easiest model to understand in the classification problem. ・ About the result The place where 1 is lined up occasionally and the place where 1 and 2 are lined up occasionally appear where 1 is the correct answer and the prediction does not match.

Recommended Posts

Machine learning logistic regression
Machine learning algorithm (logistic regression)
Machine learning linear regression
<Course> Machine Learning Chapter 3: Logistic Regression Model
Machine Learning: Supervised --Linear Regression
Logistic regression
Logistic regression
Understand machine learning ~ ridge regression ~.
Machine learning
Supervised machine learning (classification / regression)
Machine learning stacking template (regression)
Coursera Machine Learning Challenges in Python: ex2 (Logistic Regression)
Machine learning beginners try linear regression
Machine learning algorithm (multiple regression analysis)
Machine learning algorithm (simple regression analysis)
Classification and regression in machine learning
[Memo] Machine learning
Machine learning classification
[Machine learning] Understanding logistic regression from both scikit-learn and mathematics
Machine Learning sample
Machine learning algorithm (generalization of linear regression)
Machine learning with python (2) Simple regression analysis
<Course> Machine Learning Chapter 1: Linear Regression Model
<Course> Machine Learning Chapter 2: Nonlinear Regression Model
Stock price forecast using machine learning (regression)
Machine learning algorithm (linear regression summary & regularization)
[Machine learning] Regression analysis using scikit learn
Machine learning tutorial summary
Machine learning ⑤ AdaBoost Summary
Machine Learning: Supervised --AdaBoost
Machine learning support vector machine
EV3 x Python Machine Learning Part 2 Linear Regression
Studying Machine Learning ~ matplotlib ~
Machine learning course memo
Machine learning library dlib
Machine learning (TensorFlow) + Lotto 6
[Python3] Let's analyze data using machine learning! (Regression)
Somehow learn machine learning
Supervised learning (regression) 1 Basics
Python: Supervised Learning (Regression)
Machine learning library Shogun
Machine learning rabbit challenge
Introduction to machine learning
Machine Learning: k-Nearest Neighbors
What is machine learning?
Coursera Machine Learning Challenges in Python: ex3 (Handwritten Number Recognition with Logistic Regression)
Python Scikit-learn Linear Regression Analysis Nonlinear Simple Regression Analysis Machine Learning
Coursera Machine Learning Challenges in Python: ex1 (Linear Regression)
Machine learning model considering maintainability
Japanese preprocessing for machine learning
Machine learning in Delemas (practice)
An introduction to machine learning
What is Logistic Regression Analysis?
Machine learning / classification related techniques
Basics of Machine Learning (Notes)
Machine learning beginners tried RBM
[Machine learning] Understanding random forest
Machine learning with Python! Preparation
Machine Learning Study Resource Notepad
Machine learning ② Naive Bayes Summary
Machine learning article summary (self-authored)