Yi Hundred tutorial ai python correction-ai supervised learning (regression)

Source: Internet
Author: User

Regression is one of the most important statistical and machine learning tools. We think that the journey of machine learning is not wrong from the beginning of the return. It can be defined as a parameterized technique that enables us to make decisions based on data, or, in other words, allows you to make predictions based on data by learning the relationships between input and output variables. Here, the output variable that relies on the input variable is the real number of the continuous value. In regression, the relationship between input and output variables is important, and it helps us understand that the value of the output variable changes with the input variable. Regression is often used to predict price, economy, change, etc.

building a regression in Python

In this section, we'll learn how to build a single and multivariable regression.

Linear regression/univariate regression device

Let's focus on some of the required packages-

Import NumPy as NP  from Import Linear_model Import sklearn.metrics as SM import Matplotlib.pyplot as Plt

Now we need to provide input data and save the data in a file named linear.txt .

input = ' F:\\notebook\\linear.txt '
Use np.loadtxtfunction to load the data.
Input_data = Np.loadtxt (input, delimiter=','= input_data[:,: -1], Input_ data[:,-1]

The next step will be the training model. The training and test samples are given below.

training_samples = Int (0.6 *= len (X)-== x[training_samples:], Y[training_samples:]

Now, we need to create a linear regression object.

Reg_linear = Linear_model. Linearregression ()

Train the object with a training sample.
Reg_linear.fit (X_train, Y_train)

Use the test data below to make predictions.
y_test_pred = Reg_linear.predict (x_test)

Now draw and visualize the data.

' Red '  'black', linewidth = 2) plt.xticks (()) Plt.yticks (()) plt.show ()

Execute the example code above and output the following results-

Now, we can calculate the performance of linear regression as follows--

Print("performance of Linear regressor:")Print("Mean Absolute error =", round (Sm.mean_absolute_error (Y_test, y_test_pred), 2))Print("Mean squared error =", round (Sm.mean_squared_error (Y_test, y_test_pred), 2))Print("Median Absolute error =", round (Sm.median_absolute_error (Y_test, y_test_pred), 2))Print("Explain Variance score =", round (Sm.explained_variance_score (Y_test, y_test_pred),2))Print("R2 score =", round (Sm.r2_score (Y_test, y_test_pred), 2))

The performance output of the linear regression is as follows-
Mean absolute error = 1.78Mean squared error = 3.89Median absolute error = 2.01Explain variance score = -0.09R2 score = -0.09

In the code above, we used these small data sources. If you want to work with some large datasets, you can use sklearn.dataset them to import larger datasets.

2,4.82.9,4.72.5,53.2,5.56,57.6,43.2,0.92.9,1.92.4,3.50.5,3.41,40.9,5.91.2,2.583.2,5.65.1,1.54.5,1.22.3,6.32.1,2.8

Multivariable regression
First, let's import some of the required packages-

Import NumPy as NP  from Import Linear_model Import sklearn.metrics as SM Import Matplotlib.pyplot as Plt  from Import Polynomialfeatures

Now you need to provide input data and save the data in a linear.txt file named.

input = ‘F:\\notebook\\Mul_linear.txt‘

We will np.loadtxt load this data by using functions.

Input_data = Np.loadtxt (input, delimiter=','= input_data[:,: -1], input_data[:,- 1]

The next step will be the training model; Training and test sample data will be provided.

training_samples = Int (0.6 *= len (X)-== x[training_samples:], Y[training_samples:]

Now, we need to create a linear regression object.

Reg_linear_mul = Linear_model. Linearregression ()

Train the object with a training sample.
Reg_linear_mul.fit (X_train, Y_train)

Now, finally, you need to make predictions with test data.

y_test_pred =reg_linear_mul.predict (x_test)Print("performance of Linear regressor:")Print("Mean Absolute error =", round (Sm.mean_absolute_error (Y_test, y_test_pred), 2))Print("Mean squared error =", round (Sm.mean_squared_error (Y_test, y_test_pred), 2))Print("Median Absolute error =", round (Sm.median_absolute_error (Y_test, y_test_pred), 2))Print("Explain Variance score =", round (Sm.explained_variance_score (Y_test, y_test_pred), 2))Print("R2 score =", round (Sm.r2_score (Y_test, y_test_pred), 2))

The performance output of the linear regression is as follows-

= 0.6Mean squared error = 0.65Median absolute error = 0.41Explain variance score = 0.34R2 score = 0.33

Now we will create a 10-order polynomial and train the regression. and provide sample data points.

polynomial = polynomialfeatures (degree = ten== [2.23, 1.35, 1.12== linear_ Model. Linearregression () Poly_linear_model.fit (x_train_transformed, y_train)print("\nlinear regression:\n", Reg_linear_mul.predict (datapoint))print("\ Npolynomial regression:\n", Poly_linear_model.predict (Poly_datapoint))

Linear regression-

[2.40170462]
Polynomial regression-
[1.8697225]

In the code above, we used these small data. If you want a large data set, you can use it sklearn.dataset to import a larger dataset.

2,4.8,1.2,3.22.9,4.7,1.5,3.62.5,5,2.8,23.2,5.5,3.5,2.16,5,2,3.27.6,4,1.2,3.23.2,0.9,2.3,1.42.9,1.9,2.3,1.22.4,3.5,2.8,3.60.5,3.4,1.8,2.91,4,3,2.50.9,5.9,5.6,0.81.2,2.58,3.45,1.233.2,5.6,2,3.25.1,1.5,1.2,1.34.5,1.2,4.1,2.32.3,6.3,2.5,3.22.1,2.8,1.2,3.6

Easy Hundred Tutorial Mobile : Please scan the bottom of this page (right) QR code and follow the public number, reply: " tutorial " Select the relevant tutorials to read or direct access: http://m.yiibai.com.

Yi Hundred tutorial ai python correction-ai supervised learning (regression)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.