符合中小企业对网站设计、功能常规化式的企业展示型网站建设
本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...
商城网站建设因基本功能的需求不同费用上面也有很大的差别...
手机微信网站开发、微信官网、微信商城网站...
本文实例讲述了Python实现的逻辑回归算法。分享给大家供大家参考,具体如下:
创新互联成立10余年来,这条路我们正越走越好,积累了技术与客户资源,形成了良好的口碑。为客户提供成都网站建设、成都做网站、网站策划、网页设计、域名注册、网络营销、VI设计、网站改版、漏洞修补等服务。网站是否美观、功能强大、用户体验好、性价比高、打开快等等,这些对于网站建设都非常重要,创新互联通过对建站技术性的掌握、对创意设计的研究为客户提供一站式互联网解决方案,携手广大客户,共同发展进步。使用python实现逻辑回归
Using Python to Implement Logistic Regression Algorithm
菜鸟写的逻辑回归,记录一下学习过程
代码:
#encoding:utf-8 """ Author: njulpy Version: 1.0 Data: 2018/04/10 Project: Using Python to Implement LogisticRegression Algorithm """ import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split #建立sigmoid函数 def sigmoid(x): x = x.astype(float) return 1./(1+np.exp(-x)) #训练模型,采用梯度下降算法 def train(x_train,y_train,num,alpha,m,n): beta = np.ones(n) for i in range(num): h=sigmoid(np.dot(x_train,beta)) #计算预测值 error = h-y_train.T #计算预测值与训练集的差值 delt=alpha*(np.dot(error,x_train))/m #计算参数的梯度变化值 beta = beta - delt #print('error',error) return beta def predict(x_test,beta): y_predict=np.zeros(len(y_test))+0.5 s=sigmoid(np.dot(beta,x_test.T)) y_predict[s < 0.34] = 0 y_predict[s > 0.67] = 1 return y_predict def accurancy(y_predict,y_test): acc=1-np.sum(np.absolute(y_predict-y_test))/len(y_test) return acc if __name__ == "__main__": data = pd.read_csv('iris.csv') x = data.iloc[:,1:5] y = data.iloc[:,5].copy() y.loc[y== 'setosa'] = 0 y.loc[y== 'versicolor'] = 0.5 y.loc[y== 'virginica'] = 1 x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.3,random_state=15) m,n=np.shape(x_train) alpha = 0.01 beta=train(x_train,y_train,1000,alpha,m,n) pre=predict(x_test,beta) t = np.arange(len(x_test)) plt.figure() p1 = plt.plot(t,pre) p2 = plt.plot(t,y_test,label='test') label = ['prediction', 'true'] plt.legend(label, loc=1) plt.show() acc=accurancy(pre,y_test) print('The predicted value is ',pre) print('The true value is ',np.array(y_test)) print('The accuracy rate is ',acc)