机器学习——监督分类MLP 二分类的不确定估计
代码:
1 # -- coding: gbk -- 2 from sklearn.datasets import load_breast_cancer 3 from sklearn.model_selection import train_test_split 4 import pandas as pd 5 import mglearn 6 from sklearn.neural_network import MLPClassifier 7 from sklearn.datasets import make_moons 8 from sklearn.ensemble import GradientBoostingClassifier 9 from sklearn.datasets import make_circles 10 from pylab import * 11 from sklearn.datasets import load_iris 12 13 def MLP初理解(): 14 X, y = make_moons(n_samples=100, noise=0.25, random_state=3) 15 print(X.shape) 16 print(y.shape) 17 X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=42) 18 19 mlp = MLPClassifier(solver='lbfgs', random_state=0).fit(X_train, y_train) 20 '''降低隐节点''' 21 #mlp = MLPClassifier(solver='lbfgs', random_state=0, hidden_layer_sizes=[10]) 22 # 使用2个隐层,每个包含10个单元 23 #mlp = MLPClassifier(solver='lbfgs', random_state=0,hidden_layer_sizes=[10, 10]) 24 25 # 使用2个隐层,每个包含10个单元,这次使用tanh非线性 26 #mlp = MLPClassifier(solver='lbfgs', activation='tanh', random_state=0, hidden_layer_sizes=[10, 10]) 27 28 def MLP理解(): 29 cancer=load_breast_cancer() 30 X_train, X_test, y_train, y_test = train_test_split(cancer.data, cancer.target, random_state=0) 31 mlp = MLPClassifier(random_state=42) 32 mlp.fit(X_train, y_train) 33 y_pred=mlp.predict(X_test) 34 print(np.mean(y_pred==y_test)) 35 36 '''消除数据缩放''' 37 '''计算每个特征平均值''' 38 mean_on_train = X_train.mean(axis=0) 39 '''每个特征标准差''' 40 std_on_train = X_train.std(axis=0) 41 X_train_scaled = (X_train - mean_on_train) / std_on_train 42 X_test_scaled = (X_test - mean_on_train) / std_on_train 43 mlp = MLPClassifier(max_iter=1000,alpha=1,random_state=0) 44 mlp.fit(X_train_scaled, y_train) 45 print("Accuracy on training set: {:.3f}".format(mlp.score(X_train_scaled, y_train))) 46 print("Accuracy on test set: {:.3f}".format(mlp.score(X_test_scaled, y_test))) 47 48 def 二分类的不确定估计(): 49 X, y = make_circles(noise=0.25, factor=0.5, random_state=1) 50 y_named = np.array(["blue", "red"])[y] 51 X_train, X_test, y_train_named, y_test_named, y_train, y_test = \ 52 train_test_split(X, y_named, y, random_state=0) 53 gbrt = GradientBoostingClassifier(random_state=0) 54 gbrt.fit(X_train, y_train_named) 55 y_pred=gbrt.predict(X_test) 56 print(y_pred) 57 '''估计概率''' 58 y_pred = gbrt.predict_proba(X_test) 59 print(y_pred) 60 print("Predicted probabilities:\n{}".format(gbrt.predict_proba(X_test[:6]))) 61 if __name__ =='__main__': 62 iris = load_iris() 63 X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=42) 64 gbrt = GradientBoostingClassifier(learning_rate=0.01, random_state=0) 65 gbrt.fit(X_train, y_train) 66 print("Decision function shape: {}".format(gbrt.decision_function(X_test).shape))