机器学习算法练习之(一):Python实现logistic回归
来源:互联网 发布:iphone透明屏幕软件 编辑:程序博客网 时间:2024/06/04 20:11
第一步:生成数据并可视化
import numpy as npimport matplotlib.pyplot as pltnp.random.seed(12)num_observations=5000#生成二维高斯分布数据x1 = np.random.multivariate_normal([0, 0], [[1, .75],[.75, 1]], num_observations)x2 = np.random.multivariate_normal([1, 4], [[1, .75],[.75, 1]], num_observations)simulated_separableish_features = np.vstack((x1, x2)).astype(np.float32)simulated_labels = np.hstack((np.zeros(num_observations), np.ones(num_observations)))plt.figure(figsize=(12,8))plt.scatter(simulated_separableish_features[:, 0], simulated_separableish_features[:, 1], c = simulated_labels, alpha = .4)
plt.figure(figsize=(12,8))plt.scatter(simulated_separableish_features[:, 0], simulated_separableish_features[:, 1], c = simulated_labels, alpha = .4)
第二步:定义sigmoid函数以及对数似然函数
#定义sigmoid函数def sigmoid(scores): return 1 / (1 + np.exp(-scores))
#对数似然估计def log_likelihood(features, target, weights): scores = np.dot(features, weights) ll = np.sum( target*scores - np.log(1 + np.exp(scores)) ) return ll
第三步:定义对数似然回归
#对数似然回归def logistic_regression(features, target, num_steps, learning_rate, add_intercept = False): if add_intercept: intercept = np.ones((features.shape[0], 1)) features = np.hstack((intercept, features)) weights = np.zeros(features.shape[1]) for step in range(num_steps): scores = np.dot(features, weights) predictions = sigmoid(scores) # Update weights with gradient output_error_signal = target - predictions gradient = np.dot(features.T, output_error_signal) weights += learning_rate * gradient # Print log-likelihood every so often if step % 10000 == 0: print (log_likelihood(features, target, weights)) return weights
weights = logistic_regression(simulated_separableish_features, simulated_labels, num_steps = 300000, learning_rate = 5e-5, add_intercept=True)
weights:-4346.26477915
[…]
-140.725421362
-140.725421357
-140.725421355
从sklearn包导入LogisticRegression,得到权值
from sklearn.linear_model import LogisticRegressionclf = LogisticRegression(fit_intercept=True, C = 1e15)clf.fit(simulated_separableish_features, simulated_labels)print (clf.intercept_, clf.coef_)print (weights)
[-13.99400797] [[-5.02712572 8.23286799]]
[-14.09225541 -5.05899648 8.28955762]
第四步:与sklearn得到的训练准确率相比较
data_with_intercept = np.hstack((np.ones((simulated_separableish_features.shape[0], 1)), simulated_separableish_features))final_scores = np.dot(data_with_intercept, weights)preds = np.round(sigmoid(final_scores))print ('Accuracy from scratch: {0}'.format((preds == simulated_labels).sum().astype(float) / len(preds)))print ('Accuracy from sk-learn: {0}'.format(clf.score(simulated_separableish_features, simulated_labels)))
Accuracy from scratch: 0.9948
Accuracy from sk-learn: 0.9948
plt.figure(figsize = (12, 8))plt.scatter(simulated_separableish_features[:, 0], simulated_separableish_features[:, 1], c = preds == simulated_labels - 1, alpha = .8, s = 50)
蓝色代表预测正确的数据,红色代表预测错误的数据
阅读全文
0 0
- 机器学习算法练习之(一):Python实现logistic回归
- 机器学习算法 --- 用Python原生码实现Logistic回归
- 机器学习之Logistic回归与Python实现
- 机器学习之logistic回归算法的java实现
- 【机器学习系列】logistic回归python实现
- 机器学习-Logistic回归python实现
- 机器学习 Logistic回归 原理Python实现
- Python机器学习之Logistic回归
- Logistic回归---机器学习算法之四
- 【机器学习算法】之logistic回归
- 【机器学习算法】之logistic回归
- 机器学习笔记之Logistic回归算法
- 机器学习经典算法详解及Python实现---Logistic回归(LR)分类器
- 机器学习经典算法详解及Python实现---Logistic回归(LR)分类器
- 机器学习算法与Python实践之(七)逻辑回归(Logistic Regression)
- 机器学习算法与Python实践之(七)逻辑回归(Logistic Regression)
- 机器学习算法与Python实践之(七)逻辑回归(Logistic Regression)
- 机器学习算法与Python实践之(七)逻辑回归(Logistic Regression)
- 2. JNI 类型和数据结构
- 指针与地址
- 用c语言实现字符串倒序的几种情况
- 各种xlsx转换程Csv
- 静音去除的问题
- 机器学习算法练习之(一):Python实现logistic回归
- 使用Git Submodule管理子模块
- 怎么aop中实现记录请求访问时间:用ThreadLocal
- 3. JNI Functions
- Java树形菜单_轻量级js树形插件_jsTree树形插件
- Java---发送HTTPS请求
- 如何将python3.6软件的py文件打包成exe程序
- 初次运行 Git 前的配置
- win8.1 or win server 2012 安装python3.5以上导致Error 0x80240017