標(biāo)題: 邏輯回歸算法 Python源程序 [打印本頁(yè)]

作者: 體驗(yàn)地球生活    時(shí)間: 2020-12-18 11:03
標(biāo)題: 邏輯回歸算法 Python源程序
Python源程序如下:
  1. import pandas as pd
  2. import numpy as np
  3. test = pd.read_table('horseColicTest.txt',header=None)
  4. train = pd.read_table('horseColicTraining.txt',header=None)
  5. train.head()
  6. train.shape
  7. train.info()
  8. print(train.head())
  9. # print(test)
  10. # print(train)
  11. #
  12. # def sigmoid(inX):
  13. #     s = 1/(1+np.exp(-inX))
  14. #     return s
  15. # def classify(inX,weight):
  16. #     p = sigmoid(sum(inX*weight))
  17. #     if p <0.5:
  18. #         return 0
  19. #     else:
  20. #         return 1
  21. # def regularize(xMat):
  22. #     inMat = xMat.copy()
  23. #     inMeans = np.mean(inMat,axis = 0)
  24. #     inVar = np.std(inMat,axis = 0)
  25. #     inMat = (inMat-inMeans)/inVar
  26. #     return inMat
  27. # def SGD_LR(dataSet,alpha=0.001,maxCycles=500):
  28. #     dataSet = dataSet.sample(maxCycles,replace=True)
  29. #     dataSet.index = range(dataSet.shape[0])
  30. #     xMat = np.mat(dataSet.iloc[:,:-1].values)
  31. #     yMat = np.mat(dataSet.iloc[:,-1].values).T
  32. #     xMat = regularize(xMat)
  33. #     m,n = xMat.shape
  34. #     weights = np.zeros((n,1))
  35. #     for i in range(m):
  36. #         grad = xMat[i].T*(xMat[i]*weights-yMat[i])
  37. #         weights = weights-alpha*grad
  38. #     return weights
  39. #
  40. # def get_acc(train,test,alpha=0.001,maxCycles=5000):
  41. #     weights = SGD_LR(train,alpha,maxCycles=maxCycles)
  42. #     xMat = np.mat(test.iloc[:,:-1].values)
  43. #     xMat = regularize(xMat)
  44. #     result = []
  45. #     for inX in xMat:
  46. #         label = classify(inX,weights)
  47. #         result = []
  48. #         for inX in xMat:
  49. #             label = classify(inX,weights)
  50. #             result.append(label)
  51. #         retest = test.copy()
  52. #         retest['predict']=result
  53. #         acc= (retest.iloc[:,-1]==retest.iloc[:,-2]).mean()
  54. #         print(f'模型準(zhǔn)確率為:{acc}')
  55. #         return retest
  56. # print(get_acc(train,test,alpha=0.001,maxCycles=5000))
  57. #
復(fù)制代碼
全部資料51hei下載地址:
邏輯回歸算法.zip (15.02 KB, 下載次數(shù): 12)






歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1