1、分类
from sklearn.neural_network import MLPClassifier
X = [[0., 0.], [1., 1.]]
y = [0, 1]
#solver : {‘lbfgs’, ‘sgd’, ‘adam’}, default ‘adam’
clf = MLPClassifier(solver='lbfgs', alpha=1e-5,
hidden_layer_sizes=(5, 2), random_state=1)
clf.fit(X, y)
MLPClassifier(activation='relu', alpha=1e-05, batch_size='auto', beta_1=0.9,
beta_2=0.999, early_stopping=False, epsilon=1e-08,
hidden_layer_sizes=(5, 2), learning_rate='constant',
learning_rate_init=0.001, max_iter=200, momentum=0.9,
nesterovs_momentum=True, power_t=0.5, random_state=1, shuffle=True,
solver='lbfgs', tol=0.0001, validation_fraction=0.1, verbose=False,
warm_start=False)
clf.predict([[2., 2.], [-1., -2.]])
array([1, 0])
[coef.shape for coef in clf.coefs_]
[(2, 5), (5, 2), (2, 1)]
#打印参数
clf.coefs_
[array([[-0.14196276, -0.02104562, -0.85522848, -3.51355396, -0.60434709],
[-0.69744683, -0.9347486 , -0.26422217, -3.35199017, 0.06640954]]),
array([[ 0.29164405, -0.14147894],
[ 2.39665167, -0.6152434 ],
[-0.51650256, 0.51452834],
[ 4.0186541 , -0.31920293],
[ 0.32903482, 0.64394475]]),
array([[-4.53025854],
[-0.86285329]])]
#打印概率
clf.predict_proba([[2., 2.], [1., 2.]])
array([[1.96718015e-04, 9.99803282e-01],
[1.96718015e-04, 9.99803282e-01]])
2、多分类
X = [[0., 0.], [1., 1.]]
y = [[0, 1], [1, 1]]
clf = MLPClassifier(solver='lbfgs', alpha=1e-5,
hidden_layer_sizes=(15,), random_state=1)
clf.fit(X, y)
MLPClassifier(activation='relu', alpha=1e-05, batch_size='auto', beta_1=0.9,
beta_2=0.999, early_stopping=False, epsilon=1e-08,
hidden_layer_sizes=(15,), learning_rate='constant',
learning_rate_init=0.001, max_iter=200, momentum=0.9,
nesterovs_momentum=True, power_t=0.5, random_state=1, shuffle=True,
solver='lbfgs', tol=0.0001, validation_fraction=0.1, verbose=False,
warm_start=False)
[coef.shape for coef in clf.coefs_]
[(2, 15), (15, 2)]
clf.predict([[1., 2.]])
array([[1, 1]])
clf.predict([[0., 0.]])
array([[0, 1]])
3、回归(输出层中没有激活功能)
from sklearn.neural_network import MLPRegressor
X = [[0., 0.], [1., 1.],[2., 2.],[3,3]]
y = [0, 1,2,3]
#solver : {‘lbfgs’, ‘sgd’, ‘adam’}, default ‘adam’
clf = MLPRegressor(solver='lbfgs', alpha=1e-5,
hidden_layer_sizes=(5, 2), random_state=1)
clf.fit(X, y)
MLPRegressor(activation='relu', alpha=1e-05, batch_size='auto', beta_1=0.9,
beta_2=0.999, early_stopping=False, epsilon=1e-08,
hidden_layer_sizes=(5, 2), learning_rate='constant',
learning_rate_init=0.001, max_iter=200, momentum=0.9,
nesterovs_momentum=True, power_t=0.5, random_state=1, shuffle=True,
solver='lbfgs', tol=0.0001, validation_fraction=0.1, verbose=False,
warm_start=False)
clf.predict([[1., 1.5], [-1., 0.]])
array([ 1.36478416, -0.03321067])
4、正则化——alpha
5、实用的提示
多层感知器对特征缩放很敏感,因此需要处理数据。例如,将输入向量X上的每个属性缩放
为[0,1]或[-1,+ 1],或将其标准化为均值0和方差1.注意,必须将相同的缩放应用于测试集。