Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to master Python linear regression

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to master Python linear regression". The content of the explanation is simple and clear, and it is easy to learn and understand. let's follow the editor's train of thought to study and learn "how to master Python linear regression".

1. Prepare for

Before you begin, make sure that Python and pip are successfully installed on your computer. Oh, if not, please visit this article: hyperdetailed Python installation Guide for installation. If you use Python for data analysis, you can directly install Anaconda:Python data analysis and mining helper-Anaconda

Open Cmd (start-run-CMD) in Windows environment, and open Terminal (command+ space input Terminal) in Apple system environment, ready to start entering commands to install dependencies.

Of course, I recommend that you use the VSCode editor, Copy the code of this article, and install the dependency module by running commands in the terminal below the editor. What a comfortable thing: the best partner of Python programming-VSCode detailed guide.

Enter the following command at the terminal to install the dependent module we need:

Pip install scikit-learn

two。 A simple training set

Winter is coming, and Shenzhen is ready to enter winter these days.

Starting from life, the influence of external temperature on whether or not to wear a coat has a linear relationship:

Now, consider this question: if the temperature in Shenzhen is 12 degrees, should we wear coats?

This question is very simple, the above simple training set, we do not even need machine learning to easily get the answer: yes. But what if the training set gets a little more complicated:

Can you see the pattern between x1, x2, x3 and y?

It's difficult, but if you have enough data (say 100), machine learning can solve the problem quickly.

In order to demonstrate the power of machine learning, we produce 100 such training sets here (formula: y=x1 + 2*x2 + 3*x3):

From random import randint TRAIN_SET_LIMIT = 1000 TRAIN_SET_COUNT = 100TRAIN_INPUT = list () TRAIN_OUTPUT = list () for i in range (TRAIN_SET_COUNT): a = randint (0, TRAIN_SET_LIMIT) b = randint (0, TRAIN_SET_LIMIT) c = randint (0, TRAIN_SET_LIMIT) op = a + (2folb) + (3folc) TRAIN_INPUT.append ([a, b, c]) TRAIN_OUTPUT.append (op)

Then let the linear regression model use the training set (Training Set) for training (fit), and then give three parameters (Test Data) to predict (predict) so that it gets the y value (Prediction), as shown in the following figure.

3. Training and testing

Why do I use sklearn? Because it's really convenient. For a training behavior like this, you only need three lines of code to do it:

From sklearn.linear_model import LinearRegression predictor = LinearRegression (n_jobs=-1) predictor.fit (X=TRAIN_INPUT, y=TRAIN_OUTPUT)

You should pay attention to the parameters of the linear regression model (LinearRegression):

N_jobs: the default is 1, which indicates the number of CPU used. When-1, all CPU is used.

Predictor.fit is the training model, and X is the TRAIN_INPUT,Y or TRAIN_OUTPUT when we generate the training set.

You can test immediately after training. Just call the predict function:

X_TEST = [[10,20,30]] outcome = predictor.predict (X=X_TEST) coefficients = predictor.coef_ print ('Outcome: {}\ nCoefficients: {}' .format (outcome, coefficients))

The coefficients here refers to the coefficient, that is, x1, x2, x3.

The results are as follows:

Outcome: [140.] Coefficients: [1. 2. 3.]

Verify that 10 + 20 2 + 30 3 = 140 is completely correct.

How, the machine learning model is really not as difficult to use as you think, most people are probably just stuck on the way to install scikit-learn.

By the way, I'll give you a little exercise to express the following Euclidean distances using a linear regression model.

The solution is actually similar to the solution in this article, but it needs to be modified.

Thank you for your reading, the above is the content of "how to master Python linear regression", after the study of this article, I believe you have a deeper understanding of how to master Python linear regression, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report