python-Logistic回归实现鸢尾花分类
import numpy as np import pandas as pd import matplotlib as mpl import matplotlib.pyplot as plt from sklearn.datasets import data pd.set_option("display.max_rows",None) data = pd.read_csv(r"D:\Anaconda\ana\envs\python32\Lib\site-packages\sklearn\datasets\data\iris.csv") data.drop_duplicates(inplace = True) #删除重复数据 #选取0和1进行二分类 data = data[data["species"] != 2] class LogisticRegression: def __init__(self,alpha,times): self.alpha = alpha self.times = times def sigmoid(self,z): """ z为float类型 """ return 1.0 / (1.0 + np.exp(-z)) def fit(self,X,y): X = np.asarray(X) y = np.asarray(y) self.w_ = np.zeros(1 + X.shape[1]) self.loss_ = [] for i in range(self.times): z = np.dot(X,self.w_[1:]) + self.w_[0] #获得多项式输入z值 p = self.sigmoid(z) #获得为1的概率值 cost = -np.sum(y * np.log(p) + (1 - y) * np.log(1 - p)) error = y - p self.loss_.append(cost) """ 调整权重值 """ self.w_[0] += self.alpha * np.sum(error) self.w_[1:] += self.alpha * np.dot(X.T,error) def predict_pro(self,X): X = np.asarray(X) z = np.dot(X,self.w_[1:]) + self.w_[0] p = self.sigmoid(z) p = p.reshape(-1,1) return np.concatenate([1 - p,p],axis = 1) def predict(self,X): return np.argmax(self.predict_pro(X),axis = 1) #此时axis = 1为横向比较,返回数值较大的下标 data0 = data[data["species"] == 0] data1 = data[data["species"] == 1] data2 = data[data["species"] == 2] data0 = data0.sample(len(data0),random_state = 0) data1 = data1.sample(len(data1),random_state = 0) data2 = data2.sample(len(data2),random_state = 0) train_X = pd.concat([data1.iloc[:40,:-1],data0.iloc[:40,:-1]] , axis = 0) train_y = pd.concat([data1.iloc[:40,-1],data0.iloc[:40,-1]] , axis = 0) test_mix = pd.concat([data0.iloc[40:,:],data1.iloc[40:,:]],axis = 0) test_mix = test_mix.sample(len(test_mix),random_state = 1) test_X = test_mix.iloc[:,:-1] test_y = test_mix.iloc[:,-1] #训练 lr = LogisticRegression(0.01,20) lr.fit(train_X,train_y) result = lr.predict(test_X) #输出准确率 accuracy = np.sum(result == test_y) / len(test_y) print(accuracy) """ 对于plot函数: I:ro:代表绘制红色圆圈 ms:代表圆圈大小 ro-:代表绘制红色实线 ro--:代表绘制红色虚线 II: 在每个想要显示的图像前添加figure函数,再在后面添加show函数可以一次性展示多个函数 """ mpl.rcParams["axes.unicode_minus"] = False plt.figure(figsize = (15,15)) plt.plot(result,"ro",ms = 15,label = "predict") plt.plot(test_y.values,"go",label = "real") plt.title("Logistic Solve Classification",fontsize = 30) plt.xlabel("Sample",fontsize = 20) plt.ylabel("Sepices",fontsize = 20) plt.legend(fontsize = 20) plt.savefig(r"C:\Users\Y_ch\Desktop\Iris\Iris.png") plt.show() plt.figure(figsize = (15,15)) plt.plot(range(1,lr.times + 1),lr.loss_,"bo-") plt.title("Loss Sigmoid Figure",fontsize = 30) plt.ylabel("Loss",fontsize = 20) plt.xlabel("Number of Training",fontsize = 20) plt.savefig(r"C:\Users\Y_ch\Desktop\Iris\Iris_loss.png") plt.show()