Мы с сыном пытаемся выяснить, как выполнить логистическую классификацию, но только с учетом перехвата.Я придумал следующее: классификация выполняется с использованием х1-х4, а затем с использованием только модели перехвата.Код запускается и, кажется, делает то, что я хочу.Но является ли их более экономный подход к выполнению классификации только для перехвата?
import numpy as np
import pandas as pd
from sklearn import preprocessing
from sklearn.linear_model import LogisticRegression
from sklearn.cross_validation import train_test_split
from sklearn.metrics import confusion_matrix
################################################
### this just generates an example dataframe ###
################################################
dataf = pd.DataFrame(np.random.randn(10000, 4), columns = ['x1', 'x2', 'x3', 'x4']).round(2)
### this creates a Y variables of 0's and 1's based on logistic model ###
c = 8
dataf['linf'] = 0.5 + c*dataf['x1'] + c*dataf['x2'] + c*dataf['x3'] + c*dataf['x4']
dataf['explinf'] = np.exp(dataf['linf'])
dataf['prob'] = dataf['explinf']/(1 + dataf['explinf'])
dataf['y'] = np.random.binomial(1, dataf['prob'])
dataf = dataf.drop(['linf', 'explinf', 'prob'], axis = 1)
########################################################################
### the dataframe has features x1, x2, x3, and x4 and 0/1 variable y ###
########################################################################
print()
print("Partial listing of data with features x1, x2, x3, and x4")
print()
print(dataf[0:4])
######################################################
### split the data into test and training datasets ###
######################################################
X = dataf.iloc[:, 0:3]
y = dataf.iloc[:, 4]
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state = 1234)
###########################################
### fit logistic regression classifier ###
### using all features (x1, x2, x3, x4) ###
###########################################
classifier = LogisticRegression(random_state = 1234)
classifier.fit(X_train, y_train)
#######################################################
### check accuracy of classifier using all features ###
#######################################################
y_pred = classifier.predict(X_test)
confusion_matrix = confusion_matrix(y_test, y_pred)
print()
print('Accuracy of classification: {:.2f}'.format(classifier.score(X_test, y_test)))
##########################################################################
##########################################################################
### the following section fits a base model without using any features ###
##########################################################################
##########################################################################
###############################################
### create a column of 1's in the dataframe ###
###############################################
dataf['constant'] = 1
print()
print("Partial listing of data with features x1, x2, x3, and x4")
print("With addition of column of all 1's")
print()
print(dataf[0:4])
######################################################
### split the data into test and training datasets ###
### without using any features ###
######################################################
X = dataf.iloc[:, 5]
y = dataf.iloc[:, 4]
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state = 1234)
X_test = X_test.values.ravel()
X_train = X_train.values.ravel()
y_test = y_test.values.ravel()
y_train = y_train.values.ravel()
###########################################
### fit logistic regression classifier ###
### without using any features ###
###########################################
classifier = LogisticRegression(random_state = 1234, fit_intercept = False)
X_test = X_test.reshape(-1, 1)
y_test = y_test.reshape(-1, 1)
X_train = X_train.reshape(-1, 1)
y_train = y_train.reshape(-1, 1)
y_train = np.ravel(y_train)
classifier.fit(X_train, y_train)
#########################################################
### check accuracy of classifier without any features ###
#########################################################
y_pred = classifier.predict(X_test)
print()
print('Accuracy of classification: {:.2f}'.format(classifier.score(X_test, y_test)))