○ 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
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
・ ・ ・ ・ ・ ・ ・ ・ ・ ・
Four quantitative variables: Sepal.Length (sepal length), Sepal.Width (septal width), Petal.Length (petal length), Petal.Width (petal width) Species (seed, setosa, versicolor, virginica)
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.