国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

Python實(shí)現(xiàn)的邏輯回歸算法示例【附測(cè)試csv文件下載】

2020-02-16 00:22:05
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了Python實(shí)現(xiàn)的邏輯回歸算法。分享給大家供大家參考,具體如下:

使用python實(shí)現(xiàn)邏輯回歸
Using Python to Implement Logistic Regression Algorithm

菜鳥(niǎo)寫(xiě)的邏輯回歸,記錄一下學(xué)習(xí)過(guò)程

代碼:

#encoding:utf-8""" Author:  njulpy Version:  1.0 Data:  2018/04/10 Project: Using Python to Implement LogisticRegression Algorithm"""import numpy as npimport pandas as pdimport matplotlib.pyplot as pltfrom sklearn.model_selection import train_test_split#建立sigmoid函數(shù)def sigmoid(x): x = x.astype(float) return 1./(1+np.exp(-x))#訓(xùn)練模型,采用梯度下降算法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)) #計(jì)算預(yù)測(cè)值  error = h-y_train.T    #計(jì)算預(yù)測(cè)值與訓(xùn)練集的差值  delt=alpha*(np.dot(error,x_train))/m #計(jì)算參數(shù)的梯度變化值  beta = beta - delt  #print('error',error) return betadef 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_predictdef accurancy(y_predict,y_test): acc=1-np.sum(np.absolute(y_predict-y_test))/len(y_test) return accif __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)

輸出結(jié)果:

The predicted value is  [ 0.   0.5  1.   0.   0.   1.   1.   0.5  1.   1.   1.   0.5  0.5  0.5  1.
  0.   0.5  1.   0.   1.   0.5  0.   0.5  0.5  0.   0.   1.   1.   1.   1.
  0.   1.   1.   1.   0.   0.   1.   0.   0.   0.5  1.   0.   0.   0.5  1. ]
The true value is  [0 0.5 0.5 0 0 0.5 1 0.5 0.5 1 1 0.5 0.5 0.5 1 0 0.5 1 0 1 0.5 0 0.5 0.5 0
 0 1 1 1 0.5 0 1 0.5 1 0 0 1 0 0 0.5 1 0 0 0.5 1]
The accuracy rate is  0.9444444444444444

附:上述示例中的iris.csv文件點(diǎn)擊此處本站下載。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 江阴市| 东乌珠穆沁旗| 建德市| 化德县| 环江| 仙居县| 新田县| 太谷县| 自治县| 四子王旗| 永川市| 凌海市| 全椒县| 于都县| 台前县| 蓝山县| 深泽县| 沁水县| 尼勒克县| 西畴县| 宿州市| 大英县| 凤阳县| 当雄县| 二连浩特市| 喀喇| 托里县| 云和县| 安平县| 应城市| 西充县| 清镇市| 永靖县| 察隅县| 阜康市| 达日县| 崇义县| 七台河市| 牟定县| 南乐县| 罗江县|