import math
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
class LogisticReressionClsaaifier:
def __init__(self,max_iter=200,learning_rate=0.01):
self.max_iter=max_iter
self.learning_rate=learning_rate
def sigmoid(self,x):
#将x转化为0~1之间的数
return 1/(1+math.exp(-x))
def data_matrix(self,X):
data_mat=[]
for d in X:
data_mat.append([1.0,*d])
#d: [5.5 4.2]
#[1.0,*d]:[1.0, 5.5, 4.2]
return data_mat
def fit(self, X, y):
data_mat = self.data_matrix(X) # m*n
self.weights = np.zeros((len(data_mat[0]), 1), dtype=np.float32)
for iter_ in range(self.max_iter):
# #利用梯度下降法一次次迭代求正确的权重
for i in range(len(X)):
result = self.sigmoid(np.dot(data_mat[i], self.weights))
error = y[i] - result # 迭代
self.weights += self.learning_rate * error * np.transpose([data_mat[i]])
#np.transpose:调换数组的行列值的索引值,类似于矩阵的转置
print('LogisticRegression Model(learning_rate={},max_iter={})'.format(self.learning_rate, self.max_iter))
def score(self,X_test,y_test):
right=0
X_test=self.data_matrix(X_test)
for x,y in zip(X_test,y_test):
resault=np.dot(X_test,self.weights)
if (resault>0 and y==1) or (resault<0 and y==0):
right+=1
return right/len(X_test)
def create_data():
# 导入数据
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['label'] = iris.target
df.columns = ['sepal length', 'sepal width', 'petal length', 'petal width', 'label']
data = np.array(df.iloc[:100, [0, 1, -1]])
# print(data)
return data[:, :2], data[:, -1]
# 调用了两个特征一个标记
X, y = create_data()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
# 得到测试数据和训练数据
lr_clf = LogisticReressionClsaaifier()
lr_clf.fit(X_train, y_train)
# 调用逻辑斯蒂函数
x_ponits = np.arange(4, 8)
y_ = -(lr_clf.weights[1] * x_ponits + lr_clf.weights[0]) / lr_clf.weights[2]
plt.plot(x_ponits, y_)
# lr_clf.show_graph()
plt.scatter(X[:50, 0], X[:50, 1], label='0')
plt.scatter(X[50:, 0], X[50:, 1], label='1')
plt.legend()
第六章
最新推荐文章于 2024-04-22 00:00:00 发布