Python method to complete logistic regression
This article mainly introduces an example of how to implement logistic regression in Python. This is an experiment in the machine learning course. It is compiled and shared with everyone. Friends who need it can refer to it. Let’s take a look together.
The principle implemented in this article is very simple. The optimization method is gradient descent. There will be test results later.
Let’s take a look at the implemented sample code first:
# coding=utf-8 from math import exp import matplotlib.pyplot as plt import numpy as np from sklearn.datasets.samples_generator import make_blobs def sigmoid(num): ''' :param num: 待计算的x :return: sigmoid之后的数值 ''' if type(num) == int or type(num) == float: return 1.0 / (1 + exp(-1 * num)) else: raise ValueError, 'only int or float data can compute sigmoid' class logistic(): def init(self, x, y): if type(x) == type(y) == list: self.x = np.array(x) self.y = np.array(y) elif type(x) == type(y) == np.ndarray: self.x = x self.y = y else: raise ValueError, 'input data error' def sigmoid(self, x): ''' :param x: 输入向量 :return: 对输入向量整体进行simgoid计算后的向量结果 ''' s = np.frompyfunc(lambda x: sigmoid(x), 1, 1) return s(x) def train_with_punish(self, alpha, errors, punish=0.0001): ''' :param alpha: alpha为学习速率 :param errors: 误差小于多少时停止迭代的阈值 :param punish: 惩罚系数 :param times: 最大迭代次数 :return: ''' self.punish = punish dimension = self.x.shape[1] self.theta = np.random.random(dimension) compute_error = 100000000 times = 0 while compute_error > errors: res = np.dot(self.x, self.theta) delta = self.sigmoid(res) - self.y self.theta = self.theta - alpha * np.dot(self.x.T, delta) - punish * self.theta # 带惩罚的梯度下降方法 compute_error = np.sum(delta) times += 1 def predict(self, x): ''' :param x: 给入新的未标注的向量 :return: 按照计算出的参数返回判定的类别 ''' x = np.array(x) if self.sigmoid(np.dot(x, self.theta)) > 0.5: return 1 else: return 0 def test1(): ''' 用来进行测试和画图,展现效果 :return: ''' x, y = make_blobs(n_samples=200, centers=2, n_features=2, random_state=0, center_box=(10, 20)) x1 = [] y1 = [] x2 = [] y2 = [] for i in range(len(y)): if y[i] == 0: x1.append(x[i][0]) y1.append(x[i][1]) elif y[i] == 1: x2.append(x[i][0]) y2.append(x[i][1]) # 以上均为处理数据,生成出两类数据 p = logistic(x, y) p.train_with_punish(alpha=0.00001, errors=0.005, punish=0.01) # 步长是0.00001,最大允许误差是0.005,惩罚系数是0.01 x_test = np.arange(10, 20, 0.01) y_test = (-1 * p.theta[0] / p.theta[1]) * x_test plt.plot(x_test, y_test, c='g', label='logistic_line') plt.scatter(x1, y1, c='r', label='positive') plt.scatter(x2, y2, c='b', label='negative') plt.legend(loc=2) plt.title('punish value = ' + p.punish.str()) plt.show() if name == 'main': test1()
The running result is as shown below
Summary
[Related recommendations]
2. Python basic introductory tutorial
3. Python object-oriented video tutorial
The above is the detailed content of Python method to complete logistic regression. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
