当前位置:   article > 正文

20191208_神经网络交叉验证_神经网络1000 500 50 2

神经网络1000 500 50 2

这个单子难点在神经网络交叉验证,之前只会机器学习的交叉验证,借鉴一个微信文章才做出来的,文档链接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')
  • 1
  • 2
  • 3
  • 4
  • 5
test=pd.read_csv('CMP3751M_CMP9772M_ML_Assignment 2-dataset-nuclear_plants_final.csv')
test.head()
  • 1
  • 2
StatusPower_range_sensor_1Power_range_sensor_2Power_range_sensor_3Power_range_sensor_4Pressure _sensor_1Pressure _sensor_2Pressure _sensor_3Pressure _sensor_4Vibration_sensor_1Vibration_sensor_2Vibration_sensor_3Vibration_sensor_4
0Normal4.50440.74436.34001.905229.53150.86472.20446.048014.465921.648015.34291.2186
1Normal4.42840.90735.64331.623227.50321.47041.99295.985620.83560.064614.88137.3483
2Normal4.52911.01996.11301.056526.42711.92471.94206.71625.335811.077925.09149.2408
3Normal5.17271.00077.85890.276525.15762.60902.92346.74851.90171.846328.66404.0157
4Normal5.22580.61257.95040.154724.07653.21134.45635.84110.50779.370034.812213.4966
test.describe()
  • 1
Power_range_sensor_1Power_range_sensor_2Power_range_sensor_3Power_range_sensor_4Pressure _sensor_1Pressure _sensor_2Pressure _sensor_3Pressure _sensor_4Vibration_sensor_1Vibration_sensor_2Vibration_sensor_3Vibration_sensor_4
count996.000000996.000000996.000000996.000000996.000000996.000000996.000000996.000000996.000000996.000000996.000000996.000000
mean4.9995746.3792739.2281127.35527214.1991273.0779585.7492344.9970028.16456310.00159315.1879829.933591
std2.7648562.3125692.5321734.35477811.6800452.1260912.5261364.1654906.1732617.33623312.1596257.282383
min0.0082000.0403002.5839660.0623000.0248000.0082620.0012240.0058000.0000000.0185000.0646000.009200
25%2.8921204.9317507.5114003.4381415.0148751.4158004.0228001.5816253.1902924.0042005.5089003.842675
50%4.8811006.4705009.3480007.07155011.7168022.6724005.7413573.8592006.7529008.79305012.1856508.853050
75%6.7945578.10450011.04680010.91740020.2802504.5025007.5035787.59990011.25330014.68405521.83500014.357400
max12.12980011.92840015.75990017.23585867.97940010.24273812.64750016.55562036.18643834.86760053.23840043.231400
test.isnull().sum()
  • 1
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
def function(a):
	if 'Normal'in a :
		return 1
	else:
		return 0
test['Status'] = test.apply(lambda x: function(x['Status']), axis = 1)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
from sklearn.preprocessing import StandardScaler
x = test.drop(['Status'],axis=1)
y=test['Status']
X_scaler = StandardScaler()
x = X_scaler.fit_transform(x)
  • 1
  • 2
  • 3
  • 4
  • 5
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)
  • 1
  • 2
  • 3
  • 4
data shape: (996, 12); no. positive: 498; no. negative: 498
  • 1
import keras
from keras.models import Sequential
from keras.layers import Dense
classifier = Sequential()
  • 1
  • 2
  • 3
  • 4
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.
  • 1
  • 2
  • 3
  • 4
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))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
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 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score
  • 1
  • 2
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
classiifier = KerasClassifier(build_fn = make_classifier,
                            batch_size=100, nb_epoch=300)
  • 1
  • 2
accuracies = cross_val_score(estimator = classiifier,X = X_train,y = y_train,cv = 10,n_jobs = -1)
  • 1
mean = accuracies.mean()
  • 1
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))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
---------------------------------------------------------------------------

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/314027
推荐阅读
相关标签
  

闽ICP备14008679号