首页 > 编程语言 >实验二:逻辑回归算法实验

实验二:逻辑回归算法实验

时间:2022-11-05 18:23:10浏览次数:43  
标签:iris 逻辑 plt 算法 实验 ax np theta data

1导包和读取数据

#导包
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#读取数据
data=pd.read_csv("D:/1/ex2data1.txt",delimiter=',',header=None,names=['exam1','exam2','isAdmitted'])
print(data)
data.head()

 

 2.绘制数据观察数据分布情况

admittedData=data[data['isAdmitted'].isin([1])]
noAdmittedData=data[data['isAdmitted'].isin([0])]
fig,ax=plt.subplots(figsize=(12,8))
ax.scatter(admittedData['exam1'],admittedData['exam2'],marker='+',label='addmitted')
ax.scatter(noAdmittedData['exam1'],noAdmittedData['exam2'],marker='o',label="not addmitted")
ax.legend(loc=1)
ax.set_xlabel('Exam1 score')
ax.set_ylabel('Exam2 score')
ax.set_title("Scatter plot of training data")
plt.show()

 

 

3编写sigmoid函数代码

def sigmoid(z):
return 1/(1+np.exp(-z))

 

 4编写逻辑回归代价函数代码

def computeCost(theta,X,Y):
theta = np.matrix(theta) 
h=sigmoid(np.dot(X,(theta.T)))
a=np.multiply(-Y,np.log(h))
b=np.multiply((1-Y),np.log(1-h))
return np.sum(a-b)/len(X)
computeCost(theta,X,Y) 

5编写梯度函数代码

 

def gradient(theta,X,Y):
theta = np.matrix(theta) #要先把theta转化为矩阵
h = sigmoid(np.dot(X, (theta.T)))
grad = np.dot(((h-Y).T), X)/len(X)
return np.array(grad).flatten()

6编写寻找最优化参数代码

import scipy.optimize as opt
result = opt.fmin_tnc(func=computeCost, x0=theta, fprime=gradient, args=(X, Y))
print(result)
theta=result[0]

 

7 编写模型评估(预测)代码,输出预测准确率

def predict(theta, X):
theta = np.matrix(theta)
temp = sigmoid(X * theta.T)
#print(temp)
return [1 if x >= 0.5 else 0 for x in temp]
predictValues=predict(theta,X)
hypothesis=[1 if a==b else 0 for (a,b)in zip(predictValues,Y)]
accuracy=hypothesis.count(1)/len(hypothesis)
print ('accuracy = {0}%'.format(accuracy*100))

 

 

8寻找决策边界,画出决策边界直线图

#决策边界
def find_x2(x1,theta):
return [(-theta[0]-theta[1]*x_1)/theta[2] for x_1 in x1]
x1 = np.linspace(30, 100, 1000)
x2=find_x2(x1,theta)

admittedData=data[data['isAdmitted'].isin([1])]
noAdmittedData=data[data['isAdmitted'].isin([0])]
fig,ax=plt.subplots(figsize=(12,8))
ax.scatter(admittedData['exam1'],admittedData['exam2'],marker='+',label='addmitted')
ax.scatter(noAdmittedData['exam2'],noAdmittedData['exam1'],marker='o',label="not addmitted")
ax.plot(x1,x2,color='r',label="decision boundary")
ax.legend(loc=1)
ax.set_xlabel('Exam1 score')
ax.set_ylabel('Exam2 score')

ax.set_title("Training data with decision boundary")

plt.show()

 

二. 针对iris数据集,应用sklearn库的逻辑回归算法进行类别预测。

1使用seaborn库进行数据可视化

 

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import load_iris
data = load_iris()
iris_target = data.target
iris_features = pd.DataFrame(data=data.data, columns=data.feature_names)
iris_features.info()
iris_features.head()
iris_features.tail()
iris_target
pd.Series(iris_target).value_counts()
iris_features.describe()
iris_all = iris_features.copy()
iris_all['target'] = iris_target
sns.pairplot(data=iris_all,diag_kind='hist', hue= 'target')
plt.show()

 

 2将iri数据集分为训练集和测试集(两者比例为8:2)进行三分类训练和预测

from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(iris_features,iris_target,test_size=0.2,random_state=2020)
from sklearn.linear_model import LogisticRegression
clf=LogisticRegression(random_state=0,solver='lbfgs')
# 在训练集上训练逻辑回归模型
clf.fit(X_train,y_train)
print('the weight of Logistic Regression:\n',clf.coef_)
print('the intercept(w0) of Logistic Regression:\n',clf.intercept_)
# 在训练集和测试集上分布进行预测
train_predict = clf.predict(x_train)
test_predict = clf.predict(x_test)

 

 3输出分类结果的混淆矩阵

#查看混淆矩阵
confusion_matrix_result = metrics.confusion_matrix(test_predict,y_test)
print('混淆矩阵结果:\n',confusion_matrix_result)

 

 

plt.figure(figsize=(8, 6))
sns.heatmap(confusion_matrix_result, annot=True, cmap='Blues')
plt.xlabel('Predicted labels')
plt.ylabel('True labels')
plt.show()

 

标签:iris,逻辑,plt,算法,实验,ax,np,theta,data
From: https://www.cnblogs.com/1759945451qq/p/16860792.html

相关文章

  • 实验4
    实验4.5:vectorInt.hpp1#include<iostream>2usingnamespacestd;3classvectorInt{4public:5vectorInt(intn);6vectorInt(intn,intv......
  • 数字逻辑笔记 全加器全减器8421BCD转余3
    二进制全加器全减器十进制加法8421BCD转余3码......
  • 实验3
    task1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_s......
  • 实验7:基于REST API的SDN北向应用实践
    (一)基本要求编写Python程序,调用OpenDaylight的北向接口实现以下功能(1)利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight;.生成拓扑sudomn--topo=single,3--......
  • 随机森林算法原理
    随机森林算法原理:随机森林是从原始训练样本集N中有放回地重复随机抽取k个样本生成新的训练样本集合,然后根据自助样本集生成k个分类树组成随机森林,新数据的分类结果按分......
  • Java雷电游戏 毕业实训实验
    Java雷电游戏实训实验能满足学习和二次开发可以作为熟悉Java的学习,作为老师阶段性学习的一个成功检验不再是单调的理解老师空泛的知识,导入就能运行。满足下述条件:1.熟练掌......
  • 实验4 类与数组 指针
    实验五:vectorInt.hpp1#pragmaonce2#include<cassert>3usingnamespacestd;45classvectorInt{6public:7vectorInt(intn);8......
  • 实验三
    task1.c#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_spa......
  • 实验四
    task5://point.hpp1#pragmaonce2classpoint{3public:4point(){}5point(intxx){x=xx;}6intreturn_x(){returnx;}7private:8i......
  • 【数据库系统概论】实验五 SQL数据库安全控制
    一、实验目的1.掌握SQLServer数据库用户基本操作2.掌握SQLServer数据库授权及回收权限的方法二、实验内容创建登录用户st1,st2使st1,st2成为stu_db的合法用户EXECsp_grant......