当前位置:网站首页>Simple example of logistic regression for machine learning

Simple example of logistic regression for machine learning

2022-06-11 22:06:00 Chrn morning

logistic Is a linear classifier , For the linear separable problem . utilize logistic The main idea of regression classification is : According to the existing data, the regression formula is established for the classification boundary line , This is used for classification . there “ Return to ” The term comes from best fit , Means to find the best fitting parameter set , therefore ,logistic The way to train the classifier is to find the best fitting parameter , The optimization method is used .

for example : In the case of two classes , Output function 0 or 1, This function is a binary classifier sigmoid function ;

                                                                                                                 g(x)=\frac{1}{1+e^{-z}}

As illustrated x by 0 when ,sigmoid The value of is 0.5, With x The increase of , Corresponding sigmoid Function valued approximation 1, With x Reduction of ,sigmoid The value of a function approximates 0

So in order to implement a logistic Regression classifier , You can multiply each feature by a regression coefficient , Then add up all the values , Substitute this sum into sigmoid Function to get a 0-1 The number of ranges , Any more than 0.5 The data is classified as 1 class , Then less than 0.5 The data is classified as 0 class ,logistic It can be regarded as a kind of probability estimation .

 

for example : In function f(x)=a*x+b in , To compress the entire target value into (0,1) in , introduce logistic function , So there is g(f(x))=\frac{1}{1+e^{-(a*x+b)}}

 

logistic The general steps of regression : collecting data , Prepare the data , Analyze the data , Training algorithm , The test algorithm , Usage algorithm .

 

The following is used Python The code is good / Cancer prediction practice .

The original data download address is :https://archive.ics.uci.edu/ml/machine-learing-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data

 



import pandas as pd
import numpy as np
# Create a feature list 
column_names=['Sample code number','Clump Thickness','Unigormity og Cell Size',
              'Uniformity of Cell Shape','Marginal Adhesion','Single Epithlital CellSize',
              'Bare Nuclei','Bland Chromation','Normal Nucleoli','Mitoses','Class']

data=pd.read_csv('https://archive.ics.uci.edu/ml/machine-learing-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data',names=column_names)
data.describle()

# Data preprocessing 
# use no Replace with standard missing value 
data=data.replace(to_replace='no',value=np.nan)
# Delete data with missing values 
data=data.dropna()
# Descriptive analysis of data 
data.describle()

# use 25% As a parameter set ,75% As a training set 
from sklearn.cross_validation import train_test_split
x_train,x_test,y_train,y_test=train_test_split(data[column_names[1:10]],data[column_names[10]],
                                               test_size=0.25,random_state=33)
# Query the number and category of training samples 
y_train.value_counts()

# Query the number and category of test samples 
y_test.value_counts()

# use Loistic Regression training on the above data 
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression

# Standardize data 
#StandardScaler function : Make each column of data in the dataset ( That is, each characteristic data ) Are unified and standardized 
s=StandardScaler()
x_train=s.fit_transform(x_train)
x_test=s.fit_transform(x_test)

# Initializing the regressor SGDClassifier
lr=LogisticRegression()

# Training models 
lr.fit(x_train,y_train)

# forecast 
lr_pred=lr.predict(x_test)

# Model performance evaluation 
from sklearn.metrics import classification_report
#classification_report The function of is to display the text report of the main classification indicators 
# Use the built-in scoring function 
print('Accuracy of LR Classifier:',lr.score(x_test,y_test))

 

原网站

版权声明
本文为[Chrn morning]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206112152200467.html