train_noseparability.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # -*- encoding:utf-8 -*-
  2. from sklearn import datasets
  3. from sklearn.model_selection import train_test_split
  4. from sklearn.linear_model import LogisticRegression
  5. from sklearn.model_selection import cross_val_predict
  6. from numpy import shape
  7. from sklearn import metrics
  8. import numpy as np
  9. import random
  10. '''
  11. 线性不可分
  12. '''
  13. def curve(x_train,w,w0):
  14. results=x_train.tolist()
  15. results=[x[0:2] for x in results]
  16. step=0.0001
  17. for i in np.arange(-0.2,1.2,step):
  18. x1=i+step
  19. x2=-1*(w[0]*x1+w0)/(w[1]+w[2]*x1) # 计算mse
  20. if abs(x2)>5.0:
  21. continue
  22. results.append([x1,x2])
  23. results=["{},{}".format(x1,x2) for [x1,x2] in results]
  24. return results
  25. def get_data(center_label,num=100):
  26. X_train=[]
  27. y_train=[]
  28. sigma=0.01
  29. for point,label in center_label:
  30. c1,c2=point
  31. for _ in range(0,num):
  32. x1=c1+random.uniform(-sigma,sigma)
  33. x2=c2+random.uniform(-sigma,sigma)
  34. X_train.append([x1,x2])
  35. y_train.append([label])
  36. return X_train,y_train
  37. center_label=[[[0,0],1],[[1,1],1],[[0,1],0],[[1,0],0]]
  38. X_train,y_train=get_data(center_label)
  39. #X_train=10*[[0,0],[1,1],[1,0],[0,1]]
  40. X_train=[ x+[x[0]*x[1]] for x in X_train]
  41. X_train=np.array(X_train)
  42. #model = LogisticRegression(penalty="l2")
  43. model = LogisticRegression()
  44. model.fit(X_train, y_train)
  45. print (model.coef_)
  46. print (model.intercept_)
  47. curve_results=curve(X_train,model.coef_.tolist()[0],model.intercept_.tolist()[0])
  48. '''
  49. with open("no_separa_traindata.csv","w") as f :
  50. f.writelines("\n".join(curve_results[0:400]))
  51. with open("no_separa_train_with_splitline.csv","w") as f :
  52. f.writelines("\n".join(curve_results))
  53. '''