逻辑回归梯度下降法
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report
from sklearn import preprocessing
# 数据是否需要标准化
# 数据标准化有利于梯度下降法的优化
scale = False
data = np.genfromtxt('LR-testSet.csv' ,delimiter=',')
x_data = data[: ,:-1]
y_data = data[: ,-1]
def plot():
x0 = []
x1 = []
y0 = []
y1 = []
# 切分不同类别的数据
for i in range(len(x_data)):
if y_data[i] == 0:
x0.append(x_data[i ,0])
y0.append(x_data[i ,1])
else:
x1.append(x_data[i ,0])
y1.append(x_data[i ,1])
# 画图
scatter0 = plt.scatter(x0 ,y0 ,c='b' ,marker='o')
scatter1 = plt.scatter(x1 ,y1 ,c='r' ,marker='x')
# 画图例
plt.legend(handles = [scatter0 ,scatter1] ,labels = ['label0' ,'label1'] ,loc = 'best')
plot()
plt.show()
# 数据处理,添加偏置项
x_data = data[: ,:-1]
y_data = data[: ,-1 ,np.newaxis]
print(x_data)
print(y_data)
print(np.mat(x_data).shape)
print(np.mat(y_data).shape)
# 给样本添加偏置项
X_data = np.concatenate((np.ones((100 ,1)) ,x_data) ,axis=1)
print(X_data.shape)
def sigmoid(x):
return 1.0 / (1 + np.exp(-x))
def cost(xMat ,yMat ,ws):
left = np.multiply(yMat ,np.log(sigmoid(xMat*ws)))
# multiply按位相乘
right = np.multiply(1 - yMat ,np.log(1 - sigmoid(xMat*ws)))
return np.sum(left + right) / -(len(xMat))
def gradAscent(xArr ,yArr):
if scale == True:
xArr = preprocessing.scale(xArr)
xMat = np.mat(xArr)
yMat = np.mat(yArr)
lr = 0.001
epochs = 10000
costList = []
# 计算数据行列数
# 行代表数据个数,列代表权值个数
m ,n = np.shape(xMat)
# 初始化权值
ws = np.mat(np.ones((n ,1)))
for i in range(epochs + 1):
# xMat和weights矩阵相乘
h = sigmoid(xMat * ws)
# 计算误差
ws_grad = xMat.T * (h - yMat) / m
ws = ws - lr * ws_grad
if i % 50 == 0:
costList.append(cost(xMat ,yMat ,ws))
return ws,costList
# 训练模型,得到权值和cost值的变化
ws,costList = gradAscent(X_data ,y_data)
print(ws)
if scale == False:
# 画图决策边界
plot()
x_test = [[-4] ,[3]]
y_test = (-ws[0] - x_test * ws[1])/ws[2]
plt.plot(x_test ,y_test ,'k')
plt.show()
# 画图loss值的变化
x = np.linspace(0 ,10000 ,201)
plt.plot(x ,costList ,c='r')
plt.title('Train')
plt.xlabel('Epochs')
plt.ylabel('Cost')
plt.show()
# 预测
def predict(x_data ,ws):
if scale == True:
x_data = preprocessing.scale(x_data)
xMat = np.mat(x_data)
ws = np.mat(ws)
return [1 if x >= 0.5 else 0 for x in sigmoid(xMat * ws)]
predictions = predict(X_data ,ws)
print(classification_report(y_data ,predictions))
sklearn逻辑回归梯度下降法
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report
from sklearn import preprocessing
from sklearn import linear_model
scale = False
data = np.genfromtxt('LR-testSet.csv' ,delimiter=',')
x_data = data[: ,:-1]
y_data = data[: ,-1]
def plot():
x0 = []
x1 = []
y0 = []
y1 = []
# 切分不同类别的数据
for i in range(len(x_data)):
if y_data[i] == 0:
x0.append(x_data[i ,0])
y0.append(x_data[i ,1])
else:
x1.append(x_data[i ,0])
y1.append(x_data[i ,1])
# 画图
scatter0 = plt.scatter(x0 ,y0 ,c='b' ,marker='o')
scatter1 = plt.scatter(x1 ,y1 ,c='r' ,marker='x')
# 画图例
plt.legend(handles = [scatter0 ,scatter1] ,labels = ['label0' ,'label1'] ,loc = 'best')
plot()
plt.show()
logistic = linear_model.LogisticRegression()
logistic.fit(x_data ,y_data)
if scale == False:
plot()
x_test = np.array([[-4] ,[3]])
y_test = (-logistic.intercept_ - x_test * logistic.coef_[0][0]) / logistic.coef_[0][1]
plt.plot(x_test ,y_test ,'k')
plt.show()
predictions = logistic.predict(x_data)
print(classification_report(y_data ,predictions))
逻辑回归非线性问题梯度下降法
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report
from sklearn import preprocessing
from sklearn.preprocessing import PolynomialFeatures
scale = False
data = np.genfromtxt('LR-testSet2.txt' ,delimiter=',')
x_data = data[: ,:-1]
y_data = data[: ,-1 ,np.newaxis]
print(y_data)
def plot():
x0 = []
x1 = []
y0 = []
y1 = []
# 切分不同类别的数据
for i in range(len(x_data)):
if y_data[i] == 0:
x0.append(x_data[i ,0])
y0.append(x_data[i ,1])
else:
x1.append(x_data[i ,0])
y1.append(x_data[i ,1])
# 画图
scatter0 = plt.scatter(x0 ,y0 ,c='b' ,marker='o')
scatter1 = plt.scatter(x1 ,y1 ,c='r' ,marker='x')
# 画图例
plt.legend(handles = [scatter0 ,scatter1] ,labels = ['label0' ,'label1'] ,loc = 'best')
plot()
plt.show()
# 定义多项式回归,degree的值可以调节多项式的特征
poly_reg= PolynomialFeatures(degree=3)
# 特征处理
x_poly = poly_reg.fit_transform(x_data)
def sigmoid(x):
return 1.0 / (1 + np.exp(-x))
def cost(xMat ,yMat ,ws):
left = np.multiply(yMat ,np.log(sigmoid(xMat*ws)))
right = np.multiply(1 - yMat ,np.log(1 - sigmoid(xMat*ws)))
return np.sum(left + right) / -(len(xMat))
def gradAscent(xArr ,yArr):
if scale == True:
xArr = preprocessing.scale(xArr)
xMat = np.mat(xArr)
yMat = np.mat(yArr)
lr = 0.03
epochs = 50000
costList = []
# 计算数据行列数
# 行代表数据个数,列代表权值个数
m, n = np.shape(xMat)
# 初始化权值
ws = np.mat(np.ones((n, 1)))
for i in range(epochs + 1):
# xMat和weights矩阵相乘
h = sigmoid(xMat * ws)
# 计算误差
ws_grad = xMat.T * (h - yMat) / m
ws = ws - lr * ws_grad
if i % 50 == 0:
costList.append(cost(xMat, yMat, ws))
return ws, costList
# 训练模型,得到权值和cost值的变化
ws ,costList = gradAscent(x_poly ,y_data)
print(ws)
# 基本完成
# 获取数据值所在的范围
x_min ,x_max = x_data[: ,0].min() ,x_data[: ,0].max() + 1
y_min ,y_max = x_data[: ,1].min() ,x_data[: ,1].max() + 1
# 生成网络矩阵
xx ,yy = np.meshgrid(np.arange(x_min ,x_max ,0.02) ,
np.arange(y_min ,y_max ,0.02))
z = sigmoid(poly_reg.fit_transform(np.c_[xx.ravel() ,yy.ravel()]).dot(np.array(ws)))
# ravel与flatten类似,多维数据转一维,flatten不会改变数据原始数据,ravel会改变原始数据
for i in range(len(z)):
if z[i] > 0.5:
z[i] = 1
else:
z[i] = 0
z = z.reshape(xx.shape)
# 等高线图
cs = plt.contour(xx ,yy ,z)
plot()
plt.show()
# 预测
def predict(x_data ,ws):
if scale == True:
x_data = preprocessing.scale(x_data)
xMat = np.mat(x_data)
ws = np.mat(ws)
return [1 if x >= 0.5 else 0 for x in sigmoid(xMat * ws)]
predictions = predict(x_poly ,ws)
print(classification_report(y_data ,predictions))
sklearn逻辑回归非线性
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model
from sklearn.datasets import make_gaussian_quantiles
from sklearn.preprocessing import PolynomialFeatures
# 生成2维正态分布,生成的数据按分位数分为两类,500个样本,2个样本特征
# 可以生成两类或多类数据
x_data ,y_data = make_gaussian_quantiles(n_samples=500 ,n_features=2 ,n_classes=2)
plt.scatter(x_data[: ,0] ,x_data[: ,1] ,c = y_data)
plt.show()
# 定义多项式回归,degree的值可以调节多项式的特征
poly_reg = PolynomialFeatures(degree=5)
# 特征处理
x_poly = poly_reg.fit_transform(x_data)
logistic = linear_model.LogisticRegression()
logistic.fit(x_poly ,y_data)
# 获取数据值所在的范围
x_min ,x_max = x_data[: ,0].min() ,x_data[: ,0].max() + 1
y_min ,y_max = x_data[: ,1].min() ,x_data[: ,1].max() + 1
# 生成网络矩阵
xx ,yy = np.meshgrid(np.arange(x_min ,x_max ,0.02) ,
np.arange(y_min ,y_max ,0.02))
z = logistic.predict(poly_reg.fit_transform(np.c_[xx.ravel() ,yy.ravel()]))
z = z.reshape(xx.shape)
# 等高线图
cs = plt.contourf(xx ,yy ,z)
# 样本点数图
plt.scatter(x_data[: ,0] ,x_data[: ,1] ,c = y_data)
plt.show()
print('score' ,logistic.score(x_poly ,y_data))
KNN算法
import matplotlib.pyplot as plt
import numpy as np
import operator
# 已知分类的数据
x1 = np.array([3 ,2 ,1])
y1 = np.array([104 ,100 ,81])
x2 = np.array([101 ,99 ,98])
y2 = np.array([10 ,5 ,2])
scatter1 = plt.scatter(x1 ,y1 ,c = 'r')
scatter2 = plt.scatter(x2 ,y2 ,c = 'b')
# 未知数据
x = np.array([18])
y = np.array([90])
scatter3 = plt.scatter(x ,y ,c = 'k')
# 画图例
plt.legend(handles = [scatter1 ,scatter2 ,scatter3] ,labels = ['labelA' ,'labelB' ,'X'] ,loc = 'best')
plt.show()
# 已知分类的数据
x_data = np.array([[3,104],
[2,100],
[1,81],
[101,10],
[99,5],
[81,2]])
y_data = np.array(['A','A','A','B','B','B'])
x_test = np.array([18,90])
# 计算样本数量
x_data_size = x_data.shape[0]
x_data_size
# 复制x_test
np.tile(x_test ,(x_data_size ,1))
# 计算x_test与每一个样本的差值
diffMat = np.tile(x_test ,(x_data_size ,1)) - x_data
diffMat
# 计算差值的平方
sqDiffMat = diffMat**2
sqDiffMat
# 求和
sqDistances = sqDiffMat.sum(axis = 1)
sqDistances
#开方
distances = sqDistances**0.5
distances
# 从小到大排序
sortedDistances = distances.argsort()
classCount = {}
# 设置k
k = 5
for i in range(k):
#获取标签
votelabel = y_data[sortedDistances[i]]
# 统计标签数量
classCount[votelabel] = classCount.get(votelabel ,0) + 1
classCount
# 根据operator.itemgetter(1)-第1个值对classCount排序,然后再取倒序
sortedClassCount = sorted(classCount.items() ,key=operator.itemgetter(1) ,reverse=True)
sortedClassCount
# 获取数量最多的标签
knnclass = sortedClassCount[0][0]
knnclass
图片:
KNN鸢尾花
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report,confusion_matrix
import operator
import random
# 鸢尾花,数据属性:萼片长度(sepal length),萼片宽度,花瓣长度(petal length),花瓣宽度
# 类别:Iris setosa,lris versicolor,Iris virginica
def knn(x_test ,x_data ,y_data ,k):
#计算样本数量
x_data_size = x_data.shape[0]
# 复制x_test
np.tile(x_test ,(x_data_size ,1))
# 计算x_test与每一个样本的差值
diffMat = np.tile(x_test ,(x_data_size ,1)) - x_data
# 计算差值的平方
sqDiffMat = diffMat**2
# 求和
sqDistances = sqDiffMat.sum(axis=1)
# 开方
distances = sqDistances**0.5
# 从小到大排序
sortedDistances = distances.argsort()
classCount = {}
for i in range(k):
# 获取标签
votelabel = y_data[sortedDistances[i]]
# 统计标签数量
classCount[votelabel] = classCount.get(votelabel, 0) + 1
# 根据operator.itemgetter(1)-第1个值对classCount排序,然后再取倒序
sortedClassCount = sorted(classCount.items(), key=operator.itemgetter(1), reverse=True)
# 获取数量最多的标签
return sortedClassCount[0][0]
# 载入数据
iris = datasets.load_iris()
# 打乱数据
data_size = iris.data.shape[0]
index = [i for i in range(data_size)]
random.shuffle(index)
iris.data = iris.data[index]
iris.target = iris.target[index]
# 切分数据集
test_size = 40
x_train = iris.data[test_size:]
x_test = iris.data[:test_size]
y_train = iris.target[test_size:]
y_test = iris.target[:test_size]
predictions = []
for i in range(x_test.shape[0]):
predictions.append(knn(x_test[i] ,x_train ,y_train ,5))
print(classification_report(y_test ,predictions))