子臣的个人博客

0%

python1

  1. 数据交互
    pandas读取数据:

    1
    2
    3
    input = pd.read_excel("E:/File/pythonfile/NeuralNetwork/data1.xls",sheet_name=0)
    output = pd.read_excel("data1.xls",sheet_name=1)
    x = input.iloc[:,0] #选取特定位置数据

    保存数据:

    1
    2
    B = pd.DataFrame(A)  #数据需要先转化成pandas格式
    B.to_csv('A.csv') # B.to_excel
  2. numpy
    生成数据:

    1
    2
    3
    4
    A = np.array([0,0,0,0])  # 一列数据
    A = np.array([[0,0,0,0]]) # 一行数据

    a = np.linspace(7,10,31) # 等差数列,np.linspace(最小数,最大数,个数)

    插入数据:

    1
    np.r_[A,[[i,j,k,h]]] #np.r_插入行。np.c_插入列
  3. 画图:

    1
    2
    3
    4
    5
    6
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(x, y, z, c=a, s=6) # c是散点颜色,可以是具体颜色,也可以是数列,s是散点大小
    ax.view_init(15, 145) #三维图视角
    plt.colorbar(ax) # colorbar没有试验成功
    plt.show()
  4. 神经网络

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    from sklearn.neural_network import MLPRegressor  
    from sklearn.preprocessing import StandardScaler
    from sklearn.model_selection import train_test_split
    #from sklearn.cross_validation import train_test_split
    import pandas as pd

    input = pd.read_excel("data1.xls",sheet_name=0)
    output = pd.read_excel("data1.xls",sheet_name=1)
    x_train, x_test, y_train, y_test = train_test_split(input,output,test_size=0.2, random_state=0)
    X = x_train
    y = y_train
    scaler = StandardScaler() # 标准化转换
    scaler.fit(X) # 训练标准化对象
    X = scaler.transform(X) # 转换数据集
    #(多层感知器对特征的缩放是敏感的,所以需要归一化你的数据。 例如,将输入向量 X 的每个属性放缩到到 [0, 1] 或 [-1,+1] ,或者将其标准化使它具有 0 均值和方差 1。
    #为了得到有意义的结果,必须对测试集也应用 相同的尺度缩放。 可以使用 StandardScaler 进行标准化。)
    # solver=‘sgd', MLP的求解方法:L-BFGS 在小数据上表现较好,Adam 较为鲁棒,SGD在参数调整较优时会有最佳表现(分类效果与迭代次数);SGD标识随机梯度下降。
    # alpha:L2的参数:MLP是可以支持正则化的,默认为L2,具体参数需要调整
    # hidden_layer_sizes=(2, 1) hidden层2层,第一层2个神经元,第二层1个神经元),2层隐藏层,也就有3层神经网络
    clf = MLPRegressor(solver='sgd', alpha=1e-3,hidden_layer_sizes=(5,5), random_state=1)
    clf.fit(X, y)
    print('预测结果:', clf.predict([[10,3,6,2.95]])) # 预测某个输入对象
    cengindex = 0
    for wi in clf.coefs_:
    cengindex += 1 # 表示底第几层神经网络。
    print('第%d层网络层:' % cengindex)
    print('权重矩阵维度:',wi.shape)
    print('系数矩阵:\n',wi)