zoukankan      html  css  js  c++  java
  • 《机器学习》(3)

    from sklearn.linear_model import LinearRegression, SGDRegressor, Ridge, LogisticRegression
    from sklearn.datasets import load_boston
    from sklearn.model_selection import train_test_split
    from sklearn.preprocessing import StandardScaler
    from sklearn.metrics import mean_squared_error
    from sklearn.externals import joblib
    from sklearn.metrics import r2_score
    from sklearn.neural_network import MLPRegressor
    
    import pandas as pd
    import numpy as np
    # 读取数据
    lb = load_boston()
    # 标准化数据
    x_train, x_test, y_train, y_test = train_test_split(lb.data, lb.target, test_size=0.2)
    
    
    # 为数据增加一个维度,相当于把[1, 5, 10] 变成 [[1, 5, 10],]
    y_train = y_train.reshape(-1, 1)
    y_test = y_test.reshape(-1, 1)
    
    # 进行标准化
    std_x = StandardScaler()
    x_train = std_x.fit_transform(x_train)
    x_test = std_x.transform(x_test)
    
    std_y = StandardScaler()
    y_train = std_y.fit_transform(y_train)
    y_test = std_y.transform(y_test)
    
    # 正规方程预测
    # 实例化线性回归估计器
    lr = LinearRegression()
    lr.fit(x_train, y_train)
    print("r2 score of Linear regression is",r2_score(y_test,lr.predict(x_test)))
    
    # 岭回归
    from sklearn.linear_model import RidgeCV
    # 使用RidgeCV来建立参数
    cv = RidgeCV(alphas=np.logspace(-3, 2, 100))
    cv.fit (x_train , y_train)
    print("r2 score of Linear regression is",r2_score(y_test,cv.predict(x_test)))
    
    # fit():就是求得训练集X的均值啊,方差啊,最大值啊,最小值啊这些训练集X固有的属性。可以理解为一个训练过程
    # transform():在Fit的基础上,进行标准化,降维,归一化等操作(看具体用的是哪个工具,如PCA,StandardScaler等)
    # fit_transform():fit_transform是fit和transform的组合,既包括了训练又包含了转换
    
    
    from keras.models import Sequential
    from keras.layers import Dense
    
    
    
    
    #基准NN
    #使用标准化后的数据
    seq = Sequential()
    #构建神经网络模型
    #input_dim来隐含的指定输入数据shape
    seq.add(Dense(64, activation='relu',input_dim=lb.data.shape[1]))
    seq.add(Dense(64, activation='relu'))
    seq.add(Dense(1, activation='relu'))
    seq.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])
    seq.fit(x_train, y_train,  epochs=300, batch_size = 16, shuffle = False)
    score = seq.evaluate(x_test, y_test,batch_size=16) #loss value & metrics values
    print("score:",score)
    print('r2 score:',r2_score(y_test, seq.predict(x_test)))
    

      

  • 相关阅读:
    PHP7革新与性能优化
    git团队开发
    PHP获取机器mac代码
    js复制文字
    jquery datatable客户端分页保持
    自已编写C# DLL 绑定到unity进程进行单步调试
    ‘xxfile' Missing the class attribute 'ExtensionOfNativeClass'
    关于热更的杂记
    关于unityengine.dll, unityengine.coremodule.dll
    学到了关于服务器磁盘阵列
  • 原文地址:https://www.cnblogs.com/ywqtro/p/14342348.html
Copyright © 2011-2022 走看看