python MLP 神经网络使用 MinMaxScaler 没有 StandardScaler效果好

本文展示了使用MLP64,2模型在不同预处理方法下的性能评估结果,包括标准化与归一化处理后的精确度、召回率等指标,并通过混淆矩阵详细对比了模型在训练集、验证集及未知数据上的表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MLP 64,2  preprocessing.MinMaxScaler().fit(X)
                               test confusion_matrix:
[[129293   2734]
 [   958  23375]]
             precision    recall  f1-score   support

          0       0.99      0.98      0.99    132027
          1       0.90      0.96      0.93     24333

avg / total       0.98      0.98      0.98    156360

all confusion_matrix:
[[646945  13384]
 [  4455 117015]]
             precision    recall  f1-score   support

          0       0.99      0.98      0.99    660329
          1       0.90      0.96      0.93    121470

avg / total       0.98      0.98      0.98    781799

black verify confusion_matrix:
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0
 0 0 0 0 0]
/root/anaconda2/lib/python2.7/site-packages/sklearn/metrics/classification.py:1137: UndefinedMetricWarning: Recall and F-score are ill-defined and being set to 0.0 in labels with no true samples.
  'recall', 'true', average, warn_for)
             precision    recall  f1-score   support

          0       0.00      0.00      0.00         0
          1       1.00      0.07      0.13        42

avg / total       1.00      0.07      0.13        42

white verify confusion_matrix:
[1 1 1 1 1 1 0]
             precision    recall  f1-score   support

          0       1.00      0.14      0.25         7
          1       0.00      0.00      0.00         0

avg / total       1.00      0.14      0.25         7

unknown_verify:
[1 0 0 1 1 0 0 0 1 1 0 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 0 0 1 1 1 1
 0 1 1 1 1 0 1 0 0 1 0 1 0 1 0 0 1 0 0 1 1 0 0 1 0 0 0 1 0 1 1 0 0 1 0 0 0]

 
 MLP 64,2 使用preprocessing.StandardScaler().fit(X)
 [[131850    180]
 [   230  24100]]
             precision    recall  f1-score   support

          0       1.00      1.00      1.00    132030
          1       0.99      0.99      0.99     24330

avg / total       1.00      1.00      1.00    156360

all confusion_matrix:
[[659500    829]
 [  1195 120275]]
             precision    recall  f1-score   support

          0       1.00      1.00      1.00    660329
          1       0.99      0.99      0.99    121470

avg / total       1.00      1.00      1.00    781799

black verify confusion_matrix:
[0 1 1 0 0 0 0 1 1 1 0 1 1 1 1 1 1 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 1 1 1 1
 0 0 0 1 1]
/root/anaconda2/lib/python2.7/site-packages/sklearn/metrics/classification.py:1137: UndefinedMetricWarning: Recall and F-score are ill-defined and being set to 0.0 in labels with no true samples.
  'recall', 'true', average, warn_for)
             precision    recall  f1-score   support

          0       0.00      0.00      0.00         0
          1       1.00      0.62      0.76        42

avg / total       1.00      0.62      0.76        42

white verify confusion_matrix:
[0 0 1 0 1 1 0]
             precision    recall  f1-score   support

          0       1.00      0.57      0.73         7
          1       0.00      0.00      0.00         0

avg / total       1.00      0.57      0.73         7

unknown_verify:
[1 0 0 0 1 0 1 1 0 0 1 0 1 1 0 1 0 1 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 1 0 0
 0 1 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0]

 

代码:

    from sklearn import preprocessing
    scaler = preprocessing.StandardScaler().fit(X)
    #scaler = preprocessing.MinMaxScaler().fit(X)
    X = scaler.transform(X)
    print("standard X sample:", X[:3])

    black_verify = scaler.transform(black_verify)
    print(black_verify)

    white_verify = scaler.transform(white_verify)
    print(white_verify)

    unknown_verify = scaler.transform(unknown_verify)
    print(unknown_verify)

    # ValueError: operands could not be broadcast together with shapes (756140,75) (42,75) (756140,75) 
    for i in range(20):
        X = np.concatenate((X, black_verify))
        y += black_verify_labels


    labels = ['white', 'CC']
    if True:
        # pdb.set_trace()
        ratio_of_train = 0.8
        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=(1 - ratio_of_train))
        # X_train=preprocessing.normalize(X_train)
        # X_test=preprocessing.normalize(X_test)
        clf = MLPClassifier(solver='sgd', batch_size=128, learning_rate='adaptive', max_iter=256,
                            hidden_layer_sizes=(64, 2), random_state=1)

        """
        clf = sklearn.ensemble.RandomForestClassifier(n_estimators=n_estimators, verbose=verbose, n_jobs=n_jobs,
                                                      random_state=random_state, oob_score=True)
        """

        clf.fit(X_train, y_train)
        print "test confusion_matrix:"
        # print clf.feature_importances_
        y_pred = clf.predict(X_test)
        print(sklearn.metrics.confusion_matrix(y_test, y_pred))
        print(classification_report(y_test, y_pred))
    else:
        #clf = pickle.loads(open("mpl-acc97-recall98.pkl", 'rb').read())
        clf = pickle.loads(open("mlp-add-topx10.model", 'rb').read())
        y_pred = clf.predict(X)
        print(sklearn.metrics.confusion_matrix(y, y_pred))
        print(classification_report(y, y_pred))
        import sys
        #sys.exit(0)


    print "all confusion_matrix:"
    y_pred = clf.predict(X)
    print(sklearn.metrics.confusion_matrix(y, y_pred))
    print(classification_report(y, y_pred))

 

### 船舶耐波性快速预报模型的实现 基于神经网络的船舶耐波性快速预报模型可以通过多种方法构建,其中最常见的是利用深度学习框架 TensorFlow 和 Keras 来完成。以下是一个简化版的示例代码,展示如何使用多层感知机 (MLP) 或循环神经网络 (RNN) 进行船舶耐波性的预测。 #### 数据预处理 在开始建模之前,数据预处理是非常重要的一步。假设输入特征包括海浪高度、风速、船体参数等,而目标变量是某种形式的耐波性能指标(如加速度响应谱)。以下是数据标准化的一个例子: ```python import numpy as np from sklearn.preprocessing import StandardScaler, MinMaxScaler # 假设 X_train 是训练集特征矩阵,y_train 是对应的标签向量 scaler_X = StandardScaler() X_train_scaled = scaler_X.fit_transform(X_train) scaler_y = StandardScaler() y_train_scaled = scaler_y.fit_transform(y_train.reshape(-1, 1)) ``` #### 构建 MLP 模型 对于静态输入输出关系,可以采用简单的全连接神经网络结构: ```python import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense model_mlp = Sequential([ Dense(64, activation='relu', input_shape=(X_train.shape[1],)), Dense(32, activation='relu'), Dense(1) # 输出层,单节点回归问题 ]) model_mlp.compile(optimizer=tf.optimizers.Adam(), loss='mse') model_mlp.summary() history = model_mlp.fit( X_train_scaled, y_train_scaled, epochs=50, batch_size=32, validation_split=0.2 ) ``` #### 构建 RNN/LSTM 模型 如果考虑时间序列特性,则可以选择 LSTM 或 GRU 单元来捕捉动态变化规律[^1]: ```python from tensorflow.keras.layers import LSTM # 假设输入被重新组织成形状为 (样本数, 时间步长, 特征维度) 的三维张量 X_train_reshaped = X_train_scaled.reshape((X_train_scaled.shape[0], time_steps, num_features)) model_lstm = Sequential([ LSTM(64, activation='tanh', return_sequences=True, input_shape=(time_steps, num_features)), LSTM(32, activation='tanh'), Dense(1) # 输出层 ]) model_lstm.compile(optimizer=tf.optimizers.Adam(), loss='mse') model_lstm.summary() history = model_lstm.fit( X_train_reshaped, y_train_scaled, epochs=50, batch_size=32, validation_split=0.2 ) ``` #### 结果评估与反归一化 为了更好地解释预测结果,需将标准化后的数值还原到原始范围: ```python predictions_scaled = model_lstm.predict(X_test_reshaped) predictions = scaler_y.inverse_transform(predictions_scaled) ``` --- ### YOLOv5 对于船舶图像的目标检测补充说明 虽然上述内容主要涉及耐波性预测,但如果需要结合视觉信息辅助分析,也可以引入目标检测技术。例如,使用 YOLOv5 训练一个用于识别船舶及其环境状态的模型[^2]: ```bash !python train.py --img 640 --batch 16 --epochs 50 --data ship_dataset.yaml --weights yolov5s.pt ``` 此部分适用于提取船舶运动轨迹或其他外部条件作为额外输入特征。 --- ### 总结 无论是选择传统的机器学习还是现代的深度学习方法,都需要根据具体应用场景调整模型架构和超参数配置。此外,在毕业设计阶段选取此类课题具有较高的实用价值和技术含量[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值