您的位置:首页 > 健康 > 养生 > 服务平台app_微信平台开发_营销渠道的三个类型_网络推广要求

服务平台app_微信平台开发_营销渠道的三个类型_网络推广要求

2025/3/14 12:25:45 来源:https://blog.csdn.net/m0_63137850/article/details/146209344  浏览:    关键词:服务平台app_微信平台开发_营销渠道的三个类型_网络推广要求
服务平台app_微信平台开发_营销渠道的三个类型_网络推广要求

svm.py

import numpy as npclass SVM:def __init__(self,C=1.0,lr=0.01,batch_size=32,epochs=100):self.C=Cself.lr=lrself.batch_size=batch_sizeself.epochs=epochsself.w=Noneself.b=0.0self.epoch=0#计算最高得分和对应w,bdef fit(self,X,y,X_val=None,y_val=None):sample,feature=X.shapeself.w=np.zeros(feature)self.b=0.0best_score=-np.inf#best_w=self.w  错误best_w=self.w.copy()best_b=self.bbest_epoch=0for epoch in range(self.epochs):#打乱顺序shu_index=np.random.permutation(sample)shu_X=X[shu_index]shu_y=y[shu_index]for i in range(0,sample,self.batch_size):end=i+self.batch_size#第x个批量x_batch=shu_X[i:end]y_batch=shu_y[i:end]dw,db=self.com_gradient(x_batch,y_batch)self.w-=self.lr*dwself.b-=self.lr*dbif X_val is not None and y_val is not None:y_pred=self.predict(X_val)#np.mean(x,y)错误score=np.mean(y_pred==y_val)if score>best_score:best_score=scorebest_w=self.w.copy()best_b=self.b#best_epoch=self.epoch 错误best_epoch=epochprint(f"第{epoch+1}轮训练,准确率为:{score:.4f}")if X_val is not None and y_val is not None:self.w=best_wself.b=best_bself.epoch=best_epochdef com_gradient(self,X_batch,y_batch):n=X_batch.shape[0]dw_hinge=np.zeros_like(self.w)db_hinge=0.0for i in range(n):xi=X_batch[i]yi=y_batch[i]#margin=yi*np.dot(xi,self.w)+self.b 注意是ximargin=yi*np.dot(xi,self.w)+self.bif margin<1:dw_hinge+=-yi*xidb_hinge+=-yi#注意 是计算完n个样本的dw_hinge才算dwdw=self.w+(self.C/n)*dw_hingedb=(self.C/n)*db_hingereturn dw,dbdef predict(self,X):linear=np.dot(X,self.w)+self.breturn np.sign(linear)def evaluate(self,X,y):y_true=yy_pre=self.predict(X)#注意是标签是-1和1,而非0,1tp=np.sum((y_pre==1)&(y_true==1))fp=np.sum((y_pre==1)&(y_true==-1))tn=np.sum((y_pre==-1)&(y_true==-1))fn=np.sum((y_pre==-1)&(y_true==1))accuracy=(tp+tn)/(tp+tn+fp+fn)precision=tp/(tp+fp) if tp+fp!=0 else 0recall=tp/(tp+fn) if tp+fn!=0 else 0f1=(2*precision*recall)/(precision+recall) if precision+recall!=0 else 0#注意字典的键值对xx:xxreturn{'accuracy':accuracy,'precision':precision,'recall':recall,'f1':f1}def save_weight(self,filename):#注意w和b要保存进文件np.savez(filename,w=self.w,b=self.b,epoch=self.epoch,C=self.C,lr=self.lr,batch_size=self.batch_size,epochs=self.epochs)@classmethoddef load_weight(cls,filename):data=np.load(filename)svm=cls(C=data['C'],lr=data['lr'],batch_size=data['batch_size'],epochs=data['epochs'])svm.w=data['w']svm.b=data['b']svm.epoch=data['epoch']return svm

train.py

import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from joblib import dump
from svm import SVMdata=datasets.load_breast_cancer()
X=data.data
y=data.target
y=np.where(y==0,-1,1)X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=42)
X_train,X_val,y_train,y_val=train_test_split(X_train,y_train,test_size=0.25,random_state=42)scaler=StandardScaler()
X_train=scaler.fit_transform(X_train)
X_val=scaler.transform(X_val)
X_test=scaler.transform(X_test)
dump(scaler,'scaler.joblib')#最佳准确率以及最佳模型
best_accu=-np.inf
best_model=NoneC_values=[0.1,1,10,100]for C in C_values:print(f"开始C:{C}")model=SVM(C=C,lr=0.01,batch_size=32,epochs=100)model.fit(X_train,y_train,X_val,y_val)#注意要评估X_val,y_val的得分,传参m_metrics=model.evaluate(X_val,y_val)if m_metrics['accuracy']>best_accu:#注意m_metrics['accuracy']传参best_accu=m_metrics['accuracy']best_model=model
best_model.save_weight("best_weight.npz")
print(f"最优C:{best_model.C}") 
print(f"最优C对应的epoch:{best_model.epoch+1}")   

test.py

import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScalerfrom svm import SVM
from joblib import loaddata=datasets.load_breast_cancer()
X=data.data
y=data.target
y=np.where(y==0,-1,1)_,X_test,_,y_test=train_test_split(X,y,test_size=0.2,random_state=42)scaler=load('scaler.joblib')
X_test=scaler.transform(X_test)model=SVM.load_weight('best_weight.npz')
print(f"C:{model.C}")
print(f"最优C的epoch:{model.epoch+1}")
t_metrics=model.evaluate(X_test,y_test)print(f"Accuracy:{t_metrics['accuracy']:.4f},Precision:{t_metrics['precision']:.4f},Recall:{t_metrics['recall']:.4f},f1分数:{t_metrics['f1']:.4f}")

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com