我的儿子和我正在试图弄清楚如何进行后勤分类,但只包括一个拦截 . 我想出了以下内容,它使用x1到x4执行分类,然后仅使用拦截模型 . 代码运行并且看起来像我想要的那样 . 但他们是否采用更简约的方法来执行仅限拦截分类?

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)))