求解回归系数可以这样子,我们仍然以该数据为例,计算逻辑回归的回归系数
# -*- coding: cp936 -*- import pandas as pd import numpy as np column_names=['Sample code number','Clump Thickness','Uniformity of Cell Size','Uniformity of Cell Shape','Marginal Adhesion','Single Epithelial Cell Size','Bare Nuclei','Bland Chromatin','Normal Nucleoli','Mitoses','Class'] data=pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data",names=column_names) data=data.replace(to_replace='?',value=np.nan) data=data.dropna(how='any') #data.shape 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.5,random_state=33) import sklearn.linear_model as sk_linear model = sk_linear.LogisticRegression(penalty="l2", dual=False, tol=0.0001, C=1.0, fit_intercept=True, intercept_scaling=1, class_weight=None, random_state=None, solver="liblinear", max_iter=100, multi_class="ovr", verbose=0, warm_start=False, n_jobs=1) model.fit(X_train,y_train) acc=model.score(X_test,y_test) #返回预测的确定系数R2 print '逻辑回归:' print '截距:',model.intercept_ #输出截距 print'系数:',model.coef_ #输出系数 print'逻辑回归模型评价:',acc输出
======================== RESTART: C:/Python27/laji.py ======================== Warning (from warnings module): File "C:\Python27\lib\site-packages\sklearn\cross_validation.py", line 41 "This module will be removed in 0.20.", DeprecationWarning) DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20. 逻辑回归: 截距: [-5.321301] 系数: [[ 0.23229245 0.19913981 0.24170318 0.1652501 0.03457521 0.26294431 0.04651961 0.11158346 0.14945944]] 逻辑回归模型评价: 0.979532163743值得一提的是,model=sklearn.linear_model.LogisticRegression(fit_intercept=True,n_jobs=1)
model.get_params()
这个get_params是获取各个model的参数,并不是求解回归系数,第一次搞混乱了,一定要注意!
