sklearn逻辑回归库函数直接拟合数据

from sklearn import model_selection
from sklearn.linear_model import LogisticRegression
from sklearn import metrics


# generalization of test and train set
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.5, random_state=0)

# model training
log_model = LogisticRegression() 
log_model.fit(X_train, y_train) 

# model testing
y_pred = log_model.predict(X_test)

# summarize the accuracy of fitting
print(metrics.confusion_matrix(y_test, y_pred))
print(metrics.classification_report(y_test, y_pred))

第一步,划分元素训练集和测试集,用model_selection。train_test_split指定分类数据集,答案,测试大小。

第二部,使用logisticRegression。fit函数来训练x_train,y-train.

第三步,测试,用logisticregression。predict(x_test)来使用测试集。

第四步,输出。

猜你喜欢

转载自www.cnblogs.com/baochen/p/9036280.html