python学习:两个py文件间的函数调用
本例子是测试一些数据分析模型的R值,R值越接近1,表明该模型越适合分析该数据集.
本例子是在集成开发环境Aptana Studio 3 中创建 一个dataAnaly ,然后创建modelTest.py调用modelChose.py中的函数;在modelTest.py中需要import modelChose
格式:from 模块名 import 函数名1,函数名2....
'''
Created on 2015-1-19 @author: xuzhengzhu
'''
#input files
import xlrd,openpyxl
import pandas as pd
from sklearn import cross_validation
from dataAnaly import modelChose
from sklearn.metrics import r2_score
import numpy as np file=pd.ExcelFile('e:\\report.xlsx')
data=file.parse('Sheet1')
n=len(data)
#init data
x=data[['myjg','tjg']]
y=data['byjg']
models=['linear_model.SGDRegressor','GradientBoostingRegressor','RandomForestRegressor','AdaBoostRegressor','BaggingRegressor','linear_model.LinearRegression','linear_model.LogisticRegression','svm.svr','svm.NuSVR']
m=len(models)
k=10
R2=np.zeros(k)
z=2
count=0
modelCount=0
#lookup get model object
for modelCount in range(m-1):
clf=modelChose.modelChose(models[modelCount])
R2=np.zeros(k)
count=0
#lookup folds
for train_index,test_index in cross_validation.KFold(n-z,n_folds=k):
x_train,x_test=x.ix[train_index],x.ix[test_index]
y_train,y_test=y[train_index],y[test_index]
clf.fit(x_train,y_train)
y_predict=clf.predict(x_test);
r2=r2_score(y_test,y_predict)
#print 'computed %d time(s) and R square is:%f ' %(count+1,r2)
R2[count]=r2
count+=1 print 'model choose is :',models[modelCount],'the mean of R2 is :',np.mean(R2)
y_validation = clf.predict(x.ix[(n-z):n])
r2_val=r2_score(y.ix[(n-z):n],y_validation)
print 'model choose is :',models[modelCount],'the validation ser R square is :%f ',r2_val
#print pd.DataFrame({'y_true':y.ix[(n-z):n,],'y_validation':y_validation})
modelCount+=1
modelTest.py
'''
Created on 2015-1-19
@author: xuzhengzhu
'''
from sklearn.ensemble import BaggingRegressor
from sklearn.ensemble import AdaBoostRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import GradientBoostingRegressor
from sklearn import linear_model
from sklearn.svm import SVR
from sklearn.svm import NuSVR def modelChose(modelName):
if(cmp(modelName,'linear_model.SGDRegressor')==0):
#print modelName
clf = linear_model.SGDRegressor()
return clf
elif (cmp(modelName,'GradientBoostingRegressor')==0):
#print modelName
clf = GradientBoostingRegressor()
return clf
elif (cmp(modelName,'RandomForestRegressor')==0):
#print modelName
clf = RandomForestRegressor()
return clf
elif (cmp(modelName,'AdaBoostRegressor')==0):
#print modelName
clf = AdaBoostRegressor()
return clf
elif (cmp(modelName,'BaggingRegressor')==0):
#print modelName
clf = BaggingRegressor()
return clf
elif (cmp(modelName,'linear_model.LinearRegression')==0):
#print modelName
clf = linear_model.LinearRegression()
return clf
elif (cmp(modelName,'linear_model.LogisticRegression')==0):
#print modelName
clf = linear_model.LogisticRegression()
return clf
elif (cmp(modelName,'svm.svr')==0):
#print modelName
clf = SVR()
return clf
elif (cmp(modelName,'svm.NuSVR')==0):
#print modelName
clf = NuSVR()
return clf
else:
#print modelName,count,'dddd',models[count]
return 1
modelChose.py
测试结果:
model choose is : linear_model.SGDRegressor the mean of R2 is : -4.40149514377e+158
model choose is : linear_model.SGDRegressor the validation ser R square is :%f -1.69950873171e+175
model choose is : GradientBoostingRegressor the mean of R2 is : 0.06842532769
model choose is : GradientBoostingRegressor the validation ser R square is :%f -0.706828939678
model choose is : RandomForestRegressor the mean of R2 is : 0.0656454293629
model choose is : RandomForestRegressor the validation ser R square is :%f -1.62440546968
model choose is : AdaBoostRegressor the mean of R2 is : 0.0678670360111
model choose is : AdaBoostRegressor the validation ser R square is :%f -0.743162901308
model choose is : BaggingRegressor the mean of R2 is : 0.0913739612188
model choose is : BaggingRegressor the validation ser R square is :%f -1.11141498216
model choose is : linear_model.LinearRegression the mean of R2 is : 0.0976952970181
model choose is : linear_model.LinearRegression the validation ser R square is :%f -15.3631379961
model choose is : linear_model.LogisticRegression the mean of R2 is : -0.224099722992
model choose is : linear_model.LogisticRegression the validation ser R square is :%f 0.588585017836
model choose is : svm.svr the mean of R2 is : -0.243679440381
model choose is : svm.svr the validation ser R square is :%f -1.21033155027

python学习:两个py文件间的函数调用的更多相关文章
- Python包中 __init__.py文件的作用
原创连接 https://www.cnblogs.com/AlwinXu/p/5598543.html Python包中 __init__.py文件的作用 在创建python包的过程中,IDE都会在包 ...
- Python_架构、同一台电脑上两个py文件通信、两台电脑如何通信、几十台电脑如何通信、更多电脑之间的通信、库、端口号
1.架构 C/S架构(鼻祖) C:client 客户端 S:server 服务器 早期使用的一种架构,目前的各种app使用的就是这种架构,它的表现形式就是拥有专门的app. B/S架构(隶属于C/ ...
- python包中__init__.py文件的作用
python包中__init__.py文件的作用 __init__.py文件最常用的作用是标识一个文件夹是一个 python包. __init__.py文件的另一个作用是定义模糊导入时要导入的内容. ...
- [python IO学习篇] 补充.py文件是中文, .ini文件内容是中文
python 代码文件的编码.py文件默认是ASCII编码,中文在显示时会做一个ASCII到系统默认编码的转换,这时就会出错:SyntaxError: Non-ASCII character.需要在代 ...
- python学习两月总结_汇总大牛们的思想_值得收藏
下面是我汇总的我学习两个月python(version:3.3.2)的所有笔记 你可以访问:http://www.python.org获取更多信息 你也可以访问:http://www.cnblogs. ...
- python 学习笔记(十二) 文件和序列化
python 文件读写和序列化学习.## python文件读写`1 打开并且读取文件` f = open('openfile.txt','r') print(f.read()) f.close() ` ...
- Python学习(15)文件/IO
目录 Python 文件I/O 打印到屏幕 读取键盘输入 打开和关闭文件 File对象属性 文件定位 重命名和删除文件 Python的目录 Python 文件I/O 本章只讲述所有基本的的I/O函数, ...
- 编写python程序和运行.py文件的方法步骤
前提:已安装好 Subliume Test 3 且已经添加好python编译系统,已安装好python3.7 一.新建一个文本文档,将后缀名改为.py 二.使用 Subliume Test 3 打开该 ...
- python调用另一个.py文件中的类和函数
同一文件夹下的调用 1.调用函数 A.py文件如下:def add(x,y): print('和为:%d'%(x+y)) 在B.py文件中调用A.py的add函数如下: import AA.ad ...
随机推荐
- www.verycd.com
#encoding=utf-8 import urllib import urllib2 postdate = urllib.urlencode({'continueURL':'http://www. ...
- LeetCode解题报告—— Jump Game & Merge Intervals & Permutation Sequence
1. Jump Game Given an array of non-negative integers, you are initially positioned at the first inde ...
- Apache配置基于IP的虚拟主机 Apache virtual host configuration is based on the IP
Step 1: 检查是否开启 httpd-vhosts.conf apache/conf/httpd.conf文件 # Virtual hosts Include conf/extra/httpd-v ...
- CentOS7下Samba的安装与配置
一.Samba简介 Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Server Messages Block,信息服务块)是一种在局域网上共享 ...
- 洛谷P4145 上帝造题的七分钟2/花神游历各国 [树状数组,并查集]
题目传送门 题目背景 XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. 题目描述 "第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分钟,L说,要能修改,于是 ...
- Nginx开启跨域访问
CORS on Nginx The following Nginx configuration enables CORS, with support for preflight requests. # ...
- Linux命令之chattr
chattr [-RVf] [-v version] [mode] files… chattr修改文件在Linux第二扩展文件系统(E2fs)上的特有属性.符号模式(mode)有+-=[aAcCdDe ...
- 【POJ 2154】 Color (置换、burnside引理)
Color Description Beads of N colors are connected together into a circular necklace of N beads (N< ...
- Bomb Enemy -- LeetCode
Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return ...
- JZYZOJ1535 [haoi2014]穿越封锁线
http://172.20.6.3/Problem_Show.asp?id=1535 整体来说是道水题,但是穿过点的判定把我坑得wa了两次,考场上这可是40分的水分啊啊啊. 开始的错误想法:排序后向上 ...