-
大小: 16KB文件類型: .zip金幣: 2下載: 0 次發(fā)布日期: 2021-06-16
- 語言: Python
- 標(biāo)簽: 機(jī)器學(xué)習(xí)??邏輯回歸??Softmax??regr??
資源簡介
邏輯回歸一般只能解決二分問題,但是進(jìn)行擴(kuò)展之后可以解決多線性分類問題。這是一個(gè)完整的Softmax regression解決多線性分類的源代碼,python3編碼,可直接運(yùn)行,有輸入數(shù)據(jù)和預(yù)測(cè)數(shù)據(jù)的可視化編程。還訓(xùn)練部分和測(cè)試部分的源代碼進(jìn)行了封裝,可直接運(yùn)行。

代碼片段和文件信息
import?numpy?as?np
import?matplotlib.pyplot?as?plt
import?random?as?rd
#???模型訓(xùn)練函數(shù)
def?gradientAscent(feature_data?label_data?k?maxCycle?alpha):
????“““
????利用梯度下降法訓(xùn)練Softmax模型
????:param?feature_data:(mat)特征
????:param?label_data:(mat)標(biāo)簽
????:param?k:(int)類別的個(gè)數(shù)
????:param?maxCycle:(int)最大迭代個(gè)數(shù)
????:param?alpha:?(float)學(xué)習(xí)速率
????:return:weights(mat)權(quán)重
????“““
????m?n?=?np.shape(feature_data)
????weights?=?np.mat(np.ones((n?k)))
????i?=?0
????while?i?<=?maxCycle:
????????err?=?np.exp(feature_data?*?weights)
????????if?i?%?1000?==?0:
????????????print(“\t-----iter:“?i?“cost:“?cost(err?label_data))
????????row_sum?=?-err.sum(axis=1)
????????row_sum?=?row_sum.repeat(k?axis=1)
????????err?=?err?/?row_sum
????????for?x?in?range(m):
????????????err[x?label_data[x?0]]?+=?1
????????weights?=?weights?+?(alpha?/?m)?*?feature_data.T?*?err
????????i?+=?1
????return?weights
def?cost(err?label_data):
????“““
????計(jì)算損失函數(shù)值
????:param?err:(mat)exp值
????:param?label_data:()mat標(biāo)簽的值
????:return:sum_cost/m??(float):損失函數(shù)的值
????“““
????m?=?np.shape(err)[0]
????sum_cost?=?0.0
????for?i?in?range(m):
????????if?err[i?label_data[i?0]]?/?np.sum(err[i?:])?>?0:
????????????sum_cost?-=?np.log(err[i?label_data[i?0]]?/?np.sum(err[i?:]))
????????else:
????????????sum_cost?-=?0
????return?sum_cost?/?m
def?load_data(inputfile):
????“““
????:param?inputfile:訓(xùn)練樣本的文件名
????:return:feature_data(mat)特征
????????????label_data(mat)標(biāo)簽
????????????k(int)類別的個(gè)數(shù)
????“““
????f?=?open(inputfile)
????feature_data?=?[]
????label_data?=?[]
????for?line?in?f.readlines():
????????feature_temp?=?[]
????????feature_temp.append(1)
????????lines?=?line.strip().split(“\t“)
????????for?i?in?range(len(lines)?-?1):
????????????feature_temp.append(float(lines[i]))
????????label_data.append(int(lines[-1]))
????????feature_data.append(feature_temp)
????f.close()
????#?print(feature_data)
????#?feature_data?=?np.mat(feature_data)
????#?print(feature_data)
????return?np.mat(feature_data)?np.mat(label_data).T?len(set(label_data))
def?save_model(filename?weights):
????“““
????保存最終的模型
????:param?filename:(string)文件名
????:param?w:?(mat)SR?模型的權(quán)重
????:return:
????“““
????f_w?=?open(filename?“w“)
????m?n?=?np.shape(weights)
????for?i?in?range(m):
????????w_tmp?=?[]
????????for?j?in?range(n):
????????????w_tmp.append(str(weights[i?j]))
????????f_w.write(“\t“.join(w_tmp)?+?“\n“)
????f_w.close()
def?figurePlot(feature?label):
????“““
????根據(jù)標(biāo)簽畫出各個(gè)類別的點(diǎn)圖
????:param?feature:(mat)特征
????:param?label:(mat)標(biāo)簽
????:return:
????“““
????point_red?=?[]
????point_blue?=?[]
????point_yellow?=?[]
????point_green?=?[]
????r?=?np.shape(feature)[0]
????for?index?in?range(r):
????????temp?=?-2??#?從右到左第二個(gè)數(shù)
????????if?label[index]?==?0:
????????????point_red.append(feature[index?temp])
????????????point_red.append(feature[index?temp?+?1])??#?如果一次放兩個(gè)元素feature[index?1:3],會(huì)被當(dāng)做一個(gè)整體存放在list中
????????elif?la
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-11-03?12:54??Softmax?Regression(邏輯回歸多線性分類)\
?????目錄???????????0??2018-11-03?12:57??Softmax?Regression(邏輯回歸多線性分類)\.idea\
?????文件?????????408??2018-11-01?20:38??Softmax?Regression(邏輯回歸多線性分類)\.idea\Softmax?Regression.iml
?????目錄???????????0??2018-11-03?12:58??Softmax?Regression(邏輯回歸多線性分類)\.idea\inspectionProfiles\
?????文件?????????294??2018-11-01?20:38??Softmax?Regression(邏輯回歸多線性分類)\.idea\misc.xm
?????文件?????????295??2018-11-01?20:38??Softmax?Regression(邏輯回歸多線性分類)\.idea\modules.xm
?????文件???????23028??2018-11-03?12:57??Softmax?Regression(邏輯回歸多線性分類)\.idea\workspace.xm
?????文件????????6363??2018-11-03?12:52??Softmax?Regression(邏輯回歸多線性分類)\SR_functions.py
?????文件?????????488??2018-11-03?12:54??Softmax?Regression(邏輯回歸多線性分類)\SR_main.py
?????文件?????????576??2018-11-03?11:40??Softmax?Regression(邏輯回歸多線性分類)\SR_test.py
?????文件????????3129??2018-10-31?18:21??Softmax?Regression(邏輯回歸多線性分類)\SoftInput
?????目錄???????????0??2018-11-03?12:54??Softmax?Regression(邏輯回歸多線性分類)\__pycache__\
?????文件????????5508??2018-11-03?12:54??Softmax?Regression(邏輯回歸多線性分類)\__pycache__\SR_functions.cpython-36.pyc
?????文件???????12000??2018-11-03?12:54??Softmax?Regression(邏輯回歸多線性分類)\test_result
?????文件?????????228??2018-11-03?12:54??Softmax?Regression(邏輯回歸多線性分類)\weights
評(píng)論
共有 條評(píng)論