python做逻辑回归 怎么把导入的数据分成x,y mac如何通过python将大批excel数据导入mysql

\u600e\u6837\u5c06\u6570\u636e\u5c55\u793a\u4e0eexcel\u5bfc\u5165\u653e\u5230\u4e00\u8d77\u505a

\u611f\u89c9\u4f60\u7684\u9700\u6c42\u6709\u70b9\u7279\u6b8a\uff0c\u591a\u6570\u7684\u90fd\u662f\u5bfc\u5165\uff0c\u7136\u540e\u586b\u62a5\u7684\uff0c\u5efa\u8bae\u697c\u4e3b\u505a\u6210\u4e8c\u4e2a\u6a21\u677f\uff0c\u4e00\u4e2a\u5bfc\u5165\uff0c\u4e00\u4e2a\u663e\u793a\u6570\u636e\u5e93\u7684\u6570\u636e

\u901a\u8fc7python\u5c06\u5927\u6279excel\u6570\u636e\u5bfc\u5165mysql\uff1a
\u5229\u7528Python\u4eceEXCEL\u4e2d\u8bfb\u53d6\u4e24\u5217\u6570\u636e\u5b58\u50a8\u5728LIST\u4e2d\u7136\u540e\u8fde\u63a5\u6570\u636e\u5e93\u5229\u7528insert\u8bed\u53e5\u548cLIST \u5185\u5bb9\u7ed3\u5408\uff0c\u5bfc\u5165\u6570\u636e\u5e93\uff1b\u7136\u540e\u518d\u4ece\u6570\u636e\u5e93\u8bfb\u53d6\u8868\u7684\u5b57\u6bb5\u548c\u5185\u5bb9\uff0c\u5b58\u653e\u5230EXCEL\u65b0\u8868\u4e2d\u5373\u53ef\u3002

\u4ee5\u4e0b\u7684\u5bfc\u5165\u7684\u6838\u5fc3\u4ee3\u7801\uff1a
#\u6309\u7167\u8bf4\u660e\u5b89\u88c5\u5373\u53efimport win32com.client as win32import MySQLdbapp='Excel'#\u542f\u52a8EXCEL\u7a0b\u5e8fxl=win32.gencache.EnsureDispatch('%s.Application' % app)#\u6253\u5f00\u76f8\u5e94\u7684EXCEL\u6587\u4ef6xlbook=win32.Dispatch('Excel.Application').Workbooks.Open('D:\\pyd\\opsdata.xls')#\u8bfb\u53d6EXCEL\u5de5\u5177\u8868\uff0c\u6253\u5f00\u8981\u5bfc\u5165\u6570\u636e\u7684\u8868sh=xlbook.Worksheets('sheet2')#\u5efa\u7acb\u4e00\u4e2a\u7a7aLIST\u548c\u4e24\u4e2a\u5faa\u73af\u53c2\u6570dfun=[]jcount=2ncount=1#\u4ece\u7b2c\u4e00\u5217\u5f00\u59cb\u5411\u4e0b\u5faa\u73af\uff0c\u627e\u5230\u4e3a\u7a7a\u7684\u90a3\u4e00\u884c\u8fd4\u56de\u884c\u6570\uff0c\u5373\u4e3a\u7b2cncount\u884c\u4e3a\u7a7afor ncount in range(1,65566): if sh.Cells(ncount,1).Value==None: break else: continue#print ncount\uff0c\u6d4b\u8bd5\u4ee3\u7801\uff0c\u6d4b\u8bd5ncount\u503c#EXCEL\u7684\u6570\u636e\u5b89\u6392\u4e3a\u7b2c\u4e00\u884c\u662f\u5b57\u6bb5\uff0c\u7b2c\u4e8c\u884c\u5f00\u59cb\u662f\u6570\u636e\uff0c\u6545\u4ece\u7b2c2\u884c\u5f00\u59cb\u5faa\u73af\uff0c\u5c06\u4e24\u5217\u6570\u636e\u5408\u5e76\u5230\u4e00\u4e2aLIST\u4e2d#LIST\u7ed3\u6784\u4e3a[('a','b'),('c','d')]for jcount in range(2,ncount): dfun.append((sh.Cells(jcount,1).Value,sh.Cells(jcount,2).Value))#\u5173\u95edEXCEL\u7a0b\u5e8f#xl.Application.Quit()#print dfun\uff0c\u6d4b\u8bd5\u4ee3\u7801\u6d4b\u8bd5dfun\u7684\u6570\u636e#\u5efa\u7acb\u4e00\u4e2a\u7a7aLIST fo\u7528\u6765\u5b58\u653e\u5b57\u6bb5\uff0c\u8bfb\u53d6EXCEL\u4e2d\u7b2c\u4e00\u884c\u7684\u5b57\u6bb5\u5b58\u50a8\u5230fo\u4e2dfo=[]icount=1for icount in range(1,2): fo.append((sh.Cells(1,icount).Value,sh.Cells(1,icount+1).Value))#\u6d4b\u8bd5\u4ee3\u7801\uff0cprint fo#\u6253\u5f00MYSQL\u94fe\u63a5conn=MySQLdb.connect(host='localhost',user='root',passwd='0.789asd',db='python')#\u83b7\u53d6\u6e38\u6807\u64cd\u4f5ccursor=conn.cursor()#\u5efa\u7acb\u4e00\u4e2a\u7a7a\u8868cursor.execute("create table test("+fo[0][0]+" varchar(100),"+fo[0][1]+" varchar(100));")#\u5229\u7528executemany\u547d\u4ee4\uff0c\u5c06LIST\uff0cdfun\u4e2d\u7684\u6570\u636e\u901a\u8fc7insert\u8bed\u53e5\u5199\u5165\u6570\u636e\u5e93cursor.executemany("""insert into test values(%s,%s);""" ,dfun)#\u786e\u8ba4\u6570\u636e\u64cd\u4f5c\uff0c\u6ce8\u610f\u5982\u679c\u6ca1\u6709conn.commit()\u8fd9\u4e2a\u547d\u4ee4\uff0c\u6570\u636e\u65e0\u6cd5\u88ab\u63d2\u5165\u5230\u6570\u636e\u5e93\u4e2dconn.commit()#\u6267\u884c\u67e5\u8be2\u68c0\u67e5\u7ed3\u679ccount = cursor.execute('select * from test') print 'has %s record' % count#\u91cd\u7f6e\u6e38\u6807\u4f4d\u7f6ecursor.scroll(0,mode='absolute')#\u641c\u53d6\u6240\u6709\u7ed3\u679cresults = cursor.fetchall() #\u6d4b\u8bd5\u4ee3\u7801\uff0cprint results#\u83b7\u53d6MYSQL\u91cc\u7684\u6570\u636e\u5b57\u6bb5fields = cursor.description#\u5c06\u5b57\u6bb5\u5199\u5165\u5230EXCEL\u65b0\u8868\u7684\u7b2c\u4e00\u884csh2=xlbook.Worksheets('sheet3')#\u6e05\u7a7asheet3sh2.Cells.Clearfor ifs in range(1,len(fields)+1): sh2.Cells(1,ifs).Value=fields[ifs-1][0]#\u5c06\u8bfb\u53d6\u7684\u6570\u636e\u586b\u5165\u5230\u76f8\u5e94\u7684\u884c\u5217\u4e2dics=2jcs=1for ics in range(2,len(results)+2): for jcs in range(1,len(fields)+1): sh2.Cells(ics,jcs).Value=results[ics-2][jcs-1]#\u5173\u95edEXCEL\u7a0b\u5e8fxl.Application.Quit()#\u5173\u95ed\u6e38\u6807\u548c\u67e5\u8be2\u94fe\u63a5cursor.close()conn.close()

简介
本例子是通过对一组逻辑回归映射进行输出,使得网络的权重和偏置达到最理想状态,最后再进行预测。其中,使用GD算法对参数进行更新,损耗函数采取交叉商来表示,一共训练10000次。
2.python代码
#!/usr/bin/python

import numpy
import theano
import theano.tensor as T
rng=numpy.random

N=400
feats=784
# D[0]:generate rand numbers of size N,element between (0,1)
# D[1]:generate rand int number of size N,0 or 1
D=(rng.randn(N,feats),rng.randint(size=N,low=0,high=2))
training_steps=10000

# declare symbolic variables
x=T.matrix('x')
y=T.vector('y')
w=theano.shared(rng.randn(feats),name='w') # w is shared for every input
b=theano.shared(0.,name='b') # b is shared too.

print('Initial model:')
print(w.get_value())
print(b.get_value())

# construct theano expressions,symbolic
p_1=1/(1+T.exp(-T.dot(x,w)-b)) # sigmoid function,probability of target being 1
prediction=p_1>0.5
xent=-y*T.log(p_1)-(1-y)*T.log(1-p_1) # cross entropy
cost=xent.mean()+0.01*(w**2).sum() # cost function to update parameters
gw,gb=T.grad(cost,[w,b]) # stochastic gradient descending algorithm

#compile
train=theano.function(inputs=[x,y],outputs=[prediction,xent],updates=((w,w-0.1*gw),(b,b-0.1*gb)))
predict=theano.function(inputs=[x],outputs=prediction)

# train
for i in range(training_steps):
pred,err=train(D[0],D[1])

print('Final model:')
print(w.get_value())
print(b.get_value())
print('target values for D:')
print(D[1])
print('prediction on D:')
print(predict(D[0]))

print('newly generated data for test:')
test_input=rng.randn(30,feats)
print('result:')
print(predict(test_input))

3.程序解读
如上面所示,首先导入所需的库,theano是一个用于科学计算的库。然后这里我们随机产生一个输入矩阵,大小为400*784的随机数,随机产生一个输出向量大小为400,输出向量为二值的。因此,称为逻辑回归。
然后初始化权重和偏置,它们均为共享变量(shared),其中权重初始化为较小的数,偏置初始化为0,并且打印它们。
这里我们只构建一层网络结构,使用的激活函数为logistic sigmoid function,对输入量乘以权重并考虑偏置以后就可以算出输入的激活值,该值在(0,1)之间,以0.5为界限进行二值化,然后算出交叉商和损耗函数,其中交叉商是代表了我们的激活值与实际理论值的偏离程度。接着我们使用cost分别对w,b进行求解偏导,以上均为符号表达式运算。
接着我们使用theano.function进行编译优化,提高计算效率。得到train函数和predict函数,分别进行训练和预测。
接着,我们对数据进行10000次的训练,每次训练都会按照GD算法进行更新参数,最后我们得到了想要的模型,产生一组新的输入,即可进行预测。

扩展阅读:python培训班学费一般多少 ... python基础代码大全 ... python初学编程必背 ... python逻辑回归结果检验 ... c++和python先学哪个 ... python逻辑回归可视化 ... python实现逻辑回归 ... pvq和p∧q逻辑关系 ... 在python中逻辑量有true ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网