更新时间: 试题数量: 购买人数: 提供作者:

有效期: 个月

章节介绍: 共有个章节

收藏
搜索
题库预览
下面以使用 scikit-learn 库中的 breast cancer 数据集为例,展示如何使用逻辑回归算法进 行分类,请分析下面代码,说说各部分实现的功能。 Import numpy as np From sklearn import datasets From sklearn.linear_model import LogisticRegression Import matplotlib.pyplot as plt 以上代码的作用:() 。# 加载乳腺癌数据集 Breast_cancer = datasets.load_breast_cancer() Index = breast_cancer.target < 2 X = breast_cancer.data[index,:2] Y = breast_cancer.target[index] 以上代码的作用:() 。 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15, random_state=40) 以上代码的作用:() 。 Classifier = LogisticRegression() 以上代码的作用:() 。 Classifier.fit(X_train, y_train) 以上代码的作用:() 。 # 计算分类正确率 Y_predit = classifier.predict(X_test) Error = np.mean(np.abs(y_test - y_predit)) Print(error) 以上代码的作用:() 。 Print(classifier.coef_) Print(classifier.intercept_) 以上代码的作用:() 。 Xx,yy = np.meshgrid( Np.arange(X[:,0].min()-0.5 ,X[:,0].max()+0.5 ,0.1), Np.arange(X[:,1].min()-0.5 ,X[:,1].max()+0.5 ,0.1) ) Input = np.concatenate( (xx.reshape((-1,1)) ,yy.reshape((-1,1))), axis=1 ) Z = classifier.predict(input) Z = z.reshape(xx.shape) Plt.figure(figsize=(4,3)) Plt.pcolormesh(xx,yy,z,cmap=plt.cm.Paired) For i in range(2): Index = Y == i Mark = 'kx' if i == 0 else 'k+'plt.plot(X[index,0], X[index,1],mark) Plt.show() 以上代码的作用:()
1