这个单子难点在神经网络交叉验证,之前只会机器学习的交叉验证,借鉴一个微信文章才做出来的,文档链接https://mp.weixin.qq.com/s?__biz=MzA4OTg5NzY3NA==&mid=2649345834&idx=1&sn=3c748d2d3c0ac89395da25a07a75cefa&chksm=880e808fbf7909999e2775254dc6ac0b02fd4fc582977a4de640b6249d838725d6fedf00aead&mpshare=1&scene=23&srcid=1211ZPzaGxnuVALs2XUXGnMx&sharer_sharetime=1576057502530&sharer_shareid=d4f40f7a25def68e84cbad465c76535f#rd
import pandas as pd
import matplotlib. pyplot as plt
% matplotlib inline
import warnings
warnings. filterwarnings( 'ignore' )
test= pd. read_csv( 'CMP3751M_CMP9772M_ML_Assignment 2-dataset-nuclear_plants_final.csv' )
test. head( )
Status Power_range_sensor_1 Power_range_sensor_2 Power_range_sensor_3 Power_range_sensor_4 Pressure _sensor_1 Pressure _sensor_2 Pressure _sensor_3 Pressure _sensor_4 Vibration_sensor_1 Vibration_sensor_2 Vibration_sensor_3 Vibration_sensor_4 0 Normal 4.5044 0.7443 6.3400 1.9052 29.5315 0.8647 2.2044 6.0480 14.4659 21.6480 15.3429 1.2186 1 Normal 4.4284 0.9073 5.6433 1.6232 27.5032 1.4704 1.9929 5.9856 20.8356 0.0646 14.8813 7.3483 2 Normal 4.5291 1.0199 6.1130 1.0565 26.4271 1.9247 1.9420 6.7162 5.3358 11.0779 25.0914 9.2408 3 Normal 5.1727 1.0007 7.8589 0.2765 25.1576 2.6090 2.9234 6.7485 1.9017 1.8463 28.6640 4.0157 4 Normal 5.2258 0.6125 7.9504 0.1547 24.0765 3.2113 4.4563 5.8411 0.5077 9.3700 34.8122 13.4966
test. describe( )
Power_range_sensor_1 Power_range_sensor_2 Power_range_sensor_3 Power_range_sensor_4 Pressure _sensor_1 Pressure _sensor_2 Pressure _sensor_3 Pressure _sensor_4 Vibration_sensor_1 Vibration_sensor_2 Vibration_sensor_3 Vibration_sensor_4 count 996.000000 996.000000 996.000000 996.000000 996.000000 996.000000 996.000000 996.000000 996.000000 996.000000 996.000000 996.000000 mean 4.999574 6.379273 9.228112 7.355272 14.199127 3.077958 5.749234 4.997002 8.164563 10.001593 15.187982 9.933591 std 2.764856 2.312569 2.532173 4.354778 11.680045 2.126091 2.526136 4.165490 6.173261 7.336233 12.159625 7.282383 min 0.008200 0.040300 2.583966 0.062300 0.024800 0.008262 0.001224 0.005800 0.000000 0.018500 0.064600 0.009200 25% 2.892120 4.931750 7.511400 3.438141 5.014875 1.415800 4.022800 1.581625 3.190292 4.004200 5.508900 3.842675 50% 4.881100 6.470500 9.348000 7.071550 11.716802 2.672400 5.741357 3.859200 6.752900 8.793050 12.185650 8.853050 75% 6.794557 8.104500 11.046800 10.917400 20.280250 4.502500 7.503578 7.599900 11.253300 14.684055 21.835000 14.357400 max 12.129800 11.928400 15.759900 17.235858 67.979400 10.242738 12.647500 16.555620 36.186438 34.867600 53.238400 43.231400
test. isnull( ) . sum ( )
Status 0
Power_range_sensor_1 0
Power_range_sensor_2 0
Power_range_sensor_3 0
Power_range_sensor_4 0
Pressure _sensor_1 0
Pressure _sensor_2 0
Pressure _sensor_3 0
Pressure _sensor_4 0
Vibration_sensor_1 0
Vibration_sensor_2 0
Vibration_sensor_3 0
Vibration_sensor_4 0
dtype: int64
def function ( a) :
if 'Normal' in a :
return 1
else :
return 0
test[ 'Status' ] = test. apply ( lambda x: function( x[ 'Status' ] ) , axis = 1 )
from sklearn. preprocessing import StandardScaler
x = test. drop( [ 'Status' ] , axis= 1 )
y= test[ 'Status' ]
X_scaler = StandardScaler( )
x = X_scaler. fit_transform( x)
print ( 'data shape: {0}; no. positive: {1}; no. negative: {2}' . format (
x. shape, y[ y== 1 ] . shape[ 0 ] , y[ y== 0 ] . shape[ 0 ] ) )
from sklearn. model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split( x, y, test_size= 0.2 )
data shape: (996, 12); no. positive: 498; no. negative: 498
import keras
from keras. models import Sequential
from keras. layers import Dense
classifier = Sequential( )
Using Theano backend.
WARNING (theano.configdefaults): g++ not available, if using conda: `conda install m2w64-toolchain`
WARNING (theano.configdefaults): g++ not detected ! Theano will be unable to execute optimized C-implementations (for both CPU and GPU) and will default to Python implementations. Performance will be severely degraded. To remove this warning, set Theano flags cxx to an empty string.
WARNING (theano.tensor.blas): Using NumPy C-API based implementation for BLAS functions.
for i in [ 50 , 500 , 1000 ] :
classifier = Sequential( )
classifier. add( Dense( i, kernel_initializer = 'uniform' , activation = 'sigmoid' , input_dim= 12 ) )
classifier. add( Dense( 1 , kernel_initializer = 'uniform' , activation = 'sigmoid' ) )
classifier. compile ( optimizer= 'adam' , loss = 'binary_crossentropy' , metrics = [ 'accuracy' ] )
classifier. fit( X_train, y_train, batch_size = 700 , epochs = 1 )
print ( ' nerve cell{} accuracy rate ' . format ( i) )
Epoch 1/1
796/796 [==============================] - 2s 2ms/step - loss: 0.6935 - accuracy: 0.5113
nerve cell50 accuracy rate
Epoch 1/1
796/796 [==============================] - 14s 17ms/step - loss: 0.6934 - accuracy: 0.5113
nerve cell500 accuracy rate
Epoch 1/1
796/796 [==============================] - 28s 35ms/step - loss: 0.6924 - accuracy: 0.4975
nerve cell1000 accuracy rate
from keras. wrappers. scikit_learn import KerasClassifier
from sklearn. model_selection import cross_val_score
def make_classifier ( ) :
classifier = Sequential( )
classiifier. add( Dense( 3 , kernel_initializer = 'uniform' , activation = 'relu' , input_dim= 12 ) )
classiifier. add( Dense( 3 , kernel_initializer = 'uniform' , activation = 'relu' ) )
classifier. add( Dense( 1 , kernel_initializer = 'uniform' , activation = 'sigmoid' ) )
classifier. compile ( optimizer= 'adam' , loss = 'binary_crossentropy' , metrics = [ 'accuracy' ] )
return classifier
classiifier = KerasClassifier( build_fn = make_classifier,
batch_size= 100 , nb_epoch= 300 )
accuracies = cross_val_score( estimator = classiifier, X = X_train, y = y_train, cv = 10 , n_jobs = - 1 )
mean = accuracies. mean( )
import keras
from keras. wrappers. scikit_learn import KerasClassifier
from sklearn. model_selection import cross_val_score
from keras. models import Sequential
from keras. layers import Dense
for i in [ 50 , 500 , 1000 ] :
classifier = Sequential( )
classiifier. add( Dense( i, kernel_initializer = 'uniform' , activation = 'relu' , input_dim= 12 ) )
classifier. add( Dense( 1 , kernel_initializer = 'uniform' , activation = 'sigmoid' ) )
classifier. compile ( optimizer= 'adam' , loss = 'binary_crossentropy' , metrics = [ 'accuracy' ] )
classifier. fit( X_train, y_train, batch_size = 100 , epochs = 300 )
print ( ' nerve cell{} accuracy rate ' . format ( i) )
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-14-f1690d4285f8> in <module>()
6 for i in [50,500,1000]:
7 classifier = Sequential()
----> 8 classiifier.add(Dense(i,kernel_initializer = 'uniform', activation = 'relu', input_dim=12))
9 classifier.add(Dense(1, kernel_initializer = 'uniform', activation = 'sigmoid'))
10 classifier.compile(optimizer= 'adam',loss = 'binary_crossentropy',metrics = ['accuracy'])
NameError: name 'classiifier' is not defined