你需要一个神经网络(keras)来使用分类器(sklearn)[关闭]

Posted

技术标签:

【中文标题】你需要一个神经网络(keras)来使用分类器(sklearn)[关闭]【英文标题】:Do you need a neural network (keras) to use a classifier (sklearn) [closed] 【发布时间】:2019-12-16 11:56:57 【问题描述】:

我有一个关于机器学习的问题。我在 keras 中编写了一个神经网络,并在神经网络完成后使用了一些 sklearn 分类器。我的问题是它们是否以某种方式相关?

神经网络会直接影响分类器的结果吗?如果我调整它会影响我的 %s 吗?

import pandas as pd
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense
import matplotlib.pyplot as plt
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import roc_curve,confusion_matrix, classification_report,auc
import sklearn.metrics as metrics
import seaborn as sns
import warnings
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
import tensorflow as tf
import pickle

from sklearn.metrics import roc_auc_score

warnings.filterwarnings("ignore")

#Normalize Data
heart_data = pd.read_csv('data1.csv')

heart_data.head()
y = heart_data.target.values
x_data = heart_data.drop(['target'], axis = 1)
x = (x_data - np.min(x_data)) / (np.max(x_data) - np.min(x_data)).values
n_cols = x.shape[1]

#Splitting Data
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)




def regression_model():
    # create model
    model = Sequential()
    #inputs
    model.add(Dense(50, activation='sigmoid', input_shape=(n_cols,)))
    model.add(Dense(50, activation='sigmoid')) # activation function
    model.add(Dense(1))

    # compile model
    model.compile(optimizer='adam', loss='mean_squared_error')
    #loss measures the results and figures out how bad it did. Optimizer generates next guess.
    return model


# build the model
model = regression_model()
print (model)
# fit the model
history=model.fit(x_train, y_train, validation_data=(x_test,y_test), epochs=100, batch_size=10)



#K Nearest Neighbor

neigh = KNeighborsClassifier(n_neighbors=6)
neigh.fit(x_test, y_test)
y_test_pred = neigh.predict(x_test)
print("Test Accuracy of KNN Algorithm: :.2f%".format(neigh.score(x_test,y_test)*100))
print('KNN Teacher Classification report \n',classification_report(y_test, y_test_pred))
print ("The KNN AUC Score is: ",roc_auc_score(y_test, y_test_pred))

with open('knearest_teacher', 'wb') as k:
    pickle.dump(neigh, k)

#Support Vector Machine

svm = SVC(random_state = 1)
svm.fit(x_test, y_test)
y_test_pred2 = svm.predict(x_test)
print("Test Accuracy of SVM Alg orithm: :.2f%".format(svm.score(x_test,y_test)*100))
print('SVM Teacher Classification report \n',classification_report(y_test, y_test_pred2))
print ("The SVM AUC Score is: ",roc_auc_score(y_test, y_test_pred2))

with open('supportvector_teacher', 'wb') as s:
    pickle.dump(svm, s)

#Random Forest

rf = RandomForestClassifier(n_estimators = 1000, random_state = 1)
rf.fit(x_train, y_train)
y_test_pred3 = rf.predict(x_test)
print("Random Forest Algorithm Accuracy Score : :.2f%".format(rf.score(x_test,y_test)*100))
print('Random Teacher Forest Classification report \n',classification_report(y_test, y_test_pred3))
print ("The Random Forest AUC Score is: ",roc_auc_score(y_test, y_test_pred3))

with open('randomforest_teacher', 'wb') as f:
    pickle.dump(rf, f)

#Naive Bayes

nb = GaussianNB()
nb.fit(x_train, y_train)
y_test_pred4 = nb.predict(x_test)
print("Naive Bayes Algorithm Accuracy Score : :.2f%".format(nb.score(x_test,y_test)*100))
print('Naive Bayes Teacher Classification report \n',classification_report(y_test, y_test_pred4))
print ("The Naive Bayes AUC Score is: ",roc_auc_score(y_test, y_test_pred4))

with open('naive_teacher', 'wb') as n:
    pickle.dump(nb, n)

如果不是很明显,我是机器学习的新手。

【问题讨论】:

【参考方案1】:

简单来说,神经网络只是机器学习模型的一个子集。正如您在提供的代码中所示,每个都可以独立训练和使用。请注意,在您的代码中,您不会在任何其他模型中使用 keras 模型的输出,所以不,它与其他模型无关

【讨论】:

【参考方案2】:

简而言之,没有。您的神经网络基于 Keras。您的分类器基于 Sklearn。就像 python 相关的 Java Script 一样。它们都是代码,但它们是并发的。

它们是完全独立的系统,你可以删除一个,另一个仍然可以工作,除非第二个的数据基于第一个的结果。

希望这对您有所帮助。

【讨论】:

以上是关于你需要一个神经网络(keras)来使用分类器(sklearn)[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

对比学习用 Keras 搭建 CNN RNN 等常用神经网络

使用keras构建LSTM分类器

LOSS 在非常简单的 KERAS 二元分类器中没有改变

Keras训练神经网络进行分类并进行交叉验证(Cross Validation)

Keras 自动编码器分类

Keras - 分类器未从预训练模型的转移值中学习