91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 8.09MB
    文件類型: .zip
    金幣: 2
    下載: 0 次
    發布日期: 2023-10-10
  • 語言: Python
  • 標簽: 邏輯回歸??

資源簡介

內含有邏輯回歸的源代碼是數據集,使用Python3編碼,有封裝好的邏輯回歸所需要的各種函數,適合新手學習。

資源截圖

代碼片段和文件信息

#?coding:UTF-8
import?numpy?as?np


def?sig(x):
????“““
????#?sigmoid函數
????:param?x:
????:return:
????“““
????return?1.0?/?(1?+?np.exp(-x))


def?load_file(filename):
????“““
????input:filename(string)訓練數據的位置
????output:feature_data(mat)特征
????????????label_data(mat)標簽
????“““
????f?=?open(filename)??#?打開文件
????feature_data?=?[]
????label_data?=?[]
????for?line?in?f.readlines():
????????feature_tmp?=?[]
????????label_tmp?=?[]
????????lines?=?line.strip().split(‘\t‘)??#?strip移除末尾換行符???#通過指定分隔符對字符串進行切片
????????feature_tmp.append(1)??#?偏置項
????????for?i?in?range(len(lines)?-?1):
????????????feature_tmp.append(float(lines[i]))
????????label_tmp.append(float(lines[-1]))
????????feature_data.append(feature_tmp)
????????label_data.append(label_tmp)
????f.close()
????return?np.mat(feature_data)?np.mat(label_data)


def?error_rate(h?label):
????“““
????計算當前損失函數的值
????:param?h(mat):?預測值
????:param?label(mat)?:實際值
????:return:err/m(float):錯誤率
????“““
????m?=?np.shape(h)[0]

????sum_err?=?0.0
????for?i?in?range(m):
????????if?h[i?0]?>?0?and?(1-h[i?0])?>?0:
????????????sum_err?-=?(label[i?0]?*?np.log(h[i?0]))?+?\
???????????????????????(1?-?label[i?0])?*?np.log(1?-?h[i?0])
????????else:
????????????sum_err?-=?0
????return?sum_err?/?m

def?lr_train_bgd(feature?label?maxCycle?alpha):
????“““
????利用梯度下降法訓練LR模型
????:param?feature?(mat):特征
????:param?label(mat):標簽
????:param?maxCycle(int):最大迭代次數
????:param?alpha(float):學習速率
????:return:w(mat)權重
????“““
????n?=?np.shape(feature)[1]??#?特征個數,特征第一維的長度
????w?=?np.mat(np.ones((n?1)))???#?初始化權重
????i?=?0
????while?i?<=?maxCycle:
????????h?=?sig(feature?*?w)???#?計算Sigmoid值
????????err?=?label?-?h
????????if?i?%?100?==?0:
????????????print(“\t-----------iter?=?“?+?str(i)?+?\
??????????????????“??train?error?rate?=?“?+?str(error_rate(h?label)))
????????w?=?w?+?alpha?*?feature.T?*?err???#?權重修正
????????i?+=?1
????return?w

def?save_model(filename?w):
????“““
????保存最終的模型
????:param?filename(string):模型保存的文件名
????:param?w(mat):LR模型的權重
????:return:
????“““
????m?=?np.shape(w)[0]
????f_w?=?open(filename?‘w‘)
????w_array?=?[]
????for?i?in?range(m):
????????w_array.append(str(w[i?0]))
????f_w.write(‘t‘.join(w_array))
????f_w.close()

?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-10-29?15:56??邏輯回歸\
?????目錄???????????0??2018-10-29?18:40??邏輯回歸\.idea\
?????文件?????????637??2018-10-23?11:30??邏輯回歸\.idea\Case1.iml
?????目錄???????????0??2018-10-29?18:40??邏輯回歸\.idea\inspectionProfiles\
?????文件?????????294??2018-10-05?20:40??邏輯回歸\.idea\misc.xml
?????文件?????????269??2018-10-05?20:40??邏輯回歸\.idea\modules.xml
?????文件?????????239??2018-10-23?11:30??邏輯回歸\.idea\other.xml
?????文件???????26647??2018-10-29?18:40??邏輯回歸\.idea\workspace.xml
?????文件????????2567??2018-10-29?15:40??邏輯回歸\LR_functions.py
?????文件????????1515??2018-10-25?21:36??邏輯回歸\LR_train.py
?????目錄???????????0??2018-10-15?11:02??邏輯回歸\__pycache__\
?????文件????????2498??2018-10-15?11:02??邏輯回歸\__pycache__\LR_functions.cpython-36.pyc
?????文件????????5361??2018-10-14?22:28??邏輯回歸\data.txt
?????目錄???????????0??2018-10-13?21:45??邏輯回歸\venv\
?????目錄???????????0??2018-10-29?18:40??邏輯回歸\venv\Include\
?????目錄???????????0??2018-10-13?21:45??邏輯回歸\venv\Lib\
?????目錄???????????0??2018-10-13?21:45??邏輯回歸\venv\Lib\site-packages\
?????文件??????????55??2018-10-05?20:40??邏輯回歸\venv\Lib\site-packages\easy-install.pth
?????目錄???????????0??2018-10-13?21:45??邏輯回歸\venv\Lib\site-packages\pip-10.0.1-py3.6.egg\
?????目錄???????????0??2018-10-13?21:45??邏輯回歸\venv\Lib\site-packages\pip-10.0.1-py3.6.egg\EGG-INFO\
?????文件????????2972??2018-10-05?20:40??邏輯回歸\venv\Lib\site-packages\pip-10.0.1-py3.6.egg\EGG-INFO\PKG-INFO
?????文件???????12502??2018-10-05?20:40??邏輯回歸\venv\Lib\site-packages\pip-10.0.1-py3.6.egg\EGG-INFO\SOURCES.txt
?????文件???????????1??2018-10-05?20:40??邏輯回歸\venv\Lib\site-packages\pip-10.0.1-py3.6.egg\EGG-INFO\dependency_links.txt
?????文件??????????98??2018-10-05?20:40??邏輯回歸\venv\Lib\site-packages\pip-10.0.1-py3.6.egg\EGG-INFO\entry_points.txt
?????文件???????????2??2018-10-05?20:40??邏輯回歸\venv\Lib\site-packages\pip-10.0.1-py3.6.egg\EGG-INFO\not-zip-safe
?????文件??????????74??2018-10-05?20:40??邏輯回歸\venv\Lib\site-packages\pip-10.0.1-py3.6.egg\EGG-INFO\requires.txt
?????文件???????????4??2018-10-05?20:40??邏輯回歸\venv\Lib\site-packages\pip-10.0.1-py3.6.egg\EGG-INFO\top_level.txt
?????目錄???????????0??2018-10-13?21:45??邏輯回歸\venv\Lib\site-packages\pip-10.0.1-py3.6.egg\pip\
?????文件??????????24??2018-10-05?20:40??邏輯回歸\venv\Lib\site-packages\pip-10.0.1-py3.6.egg\pip\__init__.py
?????文件?????????629??2018-10-05?20:40??邏輯回歸\venv\Lib\site-packages\pip-10.0.1-py3.6.egg\pip\__main__.py
?????目錄???????????0??2018-10-13?21:45??邏輯回歸\venv\Lib\site-packages\pip-10.0.1-py3.6.egg\pip\_internal\
............此處省略377個文件信息

評論

共有 條評論