• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
华东 博客
17年国科大博士毕业,曾就职于三星电子,清华博后,目前在某大模型创业公司工作,研究方向大模型、智能体 新浪博客: http://blog.sina.com.cn/u/2463286753
博客园    首页    新随笔    联系   管理    订阅  订阅
数据标准化+网格搜索+交叉验证+预测(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))

 

posted on 2017-02-09 03:14  华东博客  阅读(6087)  评论(2)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3