zoukankan      html  css  js  c++  java
  • 数据标准化+网格搜索+交叉验证+预测(Python)

    Download datasets iris_training.csv from: https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/tutorials/monitors

    Method: SVR

    # -*- coding: utf-8 -*-
    
    import pandas as pd
    from sklearn.grid_search import GridSearchCV
    from sklearn import svm, datasets
    from sklearn.model_selection import train_test_split
    from sklearn.preprocessing import StandardScaler
    from sklearn.utils import shuffle
    import numpy as np
    from sklearn import metrics
    df = pd.read_csv('iris_training.csv', header=0)
    parameters = {'kernel':['rbf'], 'gamma':np.logspace(-5, 0, num=6, base=2.0),'C':np.logspace(-5, 5, num=11, base=2.0)}
    grid_search = GridSearchCV(svm.SVR(), parameters, cv=10, n_jobs=4, scoring='mean_squared_error')
    
    X = df[df.columns.drop('virginica')]
    y = df['virginica']
    
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=0.3, random_state=42)
    
    random_seed = 13
    X_train, y_train = shuffle(X_train, y_train, random_state=random_seed)
    X_scaler = StandardScaler()
    X_train = X_scaler.fit_transform(X_train)
    X_test = X_scaler.transform(X_test)
    
    grid_search.fit(X_train,y_train)
    y_pred = grid_search.predict(X_test) 
    
    print 'mean_squared_error:'+str(metrics.mean_squared_error(y_test,y_pred)),
     'r2_score:'+str(metrics.r2_score(y_test,y_pred))

    Neural Network:

    # -*- coding: utf-8 -*-
    
    import pandas as pd
    from sklearn.grid_search import GridSearchCV
    from sklearn.model_selection import train_test_split
    from sklearn.preprocessing import StandardScaler
    from sklearn.utils import shuffle
    import numpy as np
    from sklearn import metrics
    from sklearn.neural_network import MLPRegressor
    df = pd.read_csv('iris_training.csv', header=0)
    
    #neural networks for regresion
    parameters = {'hidden_layer_sizes':[200,250,300,400,500,600], 'activation':['relu']}
    grid_search = GridSearchCV(MLPRegressor(), parameters, cv=10, n_jobs=4, scoring='mean_squared_error')
    
    X = df[df.columns.drop('virginica')]
    y = df['virginica']
    
    X_train, X_test, y_train, y_test = train_test_split(
    	X, y, test_size=0.3, random_state=42)
    
    random_seed = 13
    X_train, y_train = shuffle(X_train, y_train, random_state=random_seed)
    X_scaler = StandardScaler()
    X_train = X_scaler.fit_transform(X_train)
    X_test = X_scaler.transform(X_test)
    
    grid_search.fit(X_train,y_train)
    y_pred = grid_search.predict(X_test) 
    
    print 'mean_squared_error:'+str(metrics.mean_squared_error(y_test,y_pred)),
     'r2_score:'+str(metrics.r2_score(y_test,y_pred))
    
  • 相关阅读:
    线性DP SGU167 I-country
    背包问题 POJ1742 Coins
    背包问题 codevs2210 数字组合
    dfs+贪心 BZOJ4027 [HEOI2015] 兔子与樱花
    spfa最短路+DP BZOJ1003 [ZJOI2006] 物流运输
    矩阵乘法 POJ3070 Fibonacci
    tarjan+spfa最短路 BZOJ1179 [Apio2009] Atm
    欧拉函数 BZOJ2190 [SDOI2008] 仪仗队
    矩阵乘法 洛谷 P3390【模板】矩阵快速幂
    裴蜀定理 BZOJ2299[HAOI2011] 向量
  • 原文地址:https://www.cnblogs.com/huadongw/p/6380482.html
Copyright © 2011-2022 走看看