Python scikit-learn sample code for linear regression, pythonscikit-learn

Source: Internet
Author: User

Python scikit-learn sample code for linear regression, pythonscikit-learn

I. Overview

Machine learning algorithms have become "well-known" by the heat of big data in recent years. Even if you do not know each algorithm theory, you can call the names of the two famous algorithms, you can also jump out of your head. Of course, although the forest of algorithms is large, there are still limited talents. algorithms that can adapt to certain environments and achieve better results will stand out, while those with average performance will be forgotten by history. With the development of the Machine Learning Community and practical verification, this group of talents are gradually recognized and favored, and have gained more support, improvement and promotion from the community.

Taking the widest classification algorithm as an example, it can be roughly divided into two main factions: linear and non-linear. Linear algorithms include well-known logistic regression, Naive Bayes, and maximum entropy. Nonlinear algorithms include random forest, decision tree, neural network, and kernel machines. Linear algorithms are highly efficient in training and prediction, but the final effect is highly dependent on features. data must be linearly segmented at the feature layer. Therefore, using linear algorithms requires a great deal of effort in Feature Engineering. We recommend that you select, transform, or combine features to make them distinctive. While non-linear algorithms are awesome, they can model complex classification surfaces to better fit data.

Which machine learning algorithm can achieve better results on the basis of our feature selection? No one knows. Practice is to test which is the best standard. Is it hard to write five or six Machine Learning codes? No, the power of the Machine Learning Community is powerful, and the consensus of the Code farming community is not repetitive! Therefore, for some mature algorithms, there are always some excellent libraries that can be used directly, saving most of the time for research.

Based on the fact that many python are currently used, the number of well-known machine learning libraries in python is scikit-learn. This library has many advantages. Simple and easy to use, the interface is very abstract, and documentation support is very touching. In this article, we can encapsulate many of the machine learning algorithms and perform one-time tests to facilitate analysis and optimization. Of course, super parameter optimization is also very important for specific algorithms.

Ii. Scikit-learn python practices

This article uses Linear Regression Algorithms to predict housing prices in Boston. The Boston price dataset contains information about the value of housing in the Boston suburbs.

Step 1: import the Python Library

%matplotlib inlineimport numpy as npimport pandas as pdimport matplotlib.pyplot as pltimport sklearn

Step 2: Data Acquisition and understanding

The Boston dataset is a built-in scikit-learn dataset that can be used directly.

from sklearn.datasets import load_bostonboston = load_boston()
print(boston.keys())

Dict_keys (['data', 'target', 'feature _ names', 'descal'])

print(boston.data.shape)

(506, 13)

print(boston.feature_names)

['Crim' 'zn' 'indus' 'chas ''nox ''rm ''age' DIS ''rad ''tax ''PTRATIO'' B 'lstat']

Conclusion: The Boston dataset contains 506 samples and 14 features.

print(boston.DESCR)
bos = pd.DataFrame(boston.data)print(bos.head())
    0   1   2  3   4   5   6    7  8   9   10 \0 0.00632 18.0 2.31 0.0 0.538 6.575 65.2 4.0900 1.0 296.0 15.3  1 0.02731  0.0 7.07 0.0 0.469 6.421 78.9 4.9671 2.0 242.0 17.8  2 0.02729  0.0 7.07 0.0 0.469 7.185 61.1 4.9671 2.0 242.0 17.8  3 0.03237  0.0 2.18 0.0 0.458 6.998 45.8 6.0622 3.0 222.0 18.7  4 0.06905  0.0 2.18 0.0 0.458 7.147 54.2 6.0622 3.0 222.0 18.7      11  12 0 396.90 4.98 1 396.90 9.14 2 392.83 4.03 3 394.63 2.94 4 396.90 5.33 
bos.columns = boston.feature_namesprint(bos.head())
print(boston.target[:5])
bos['PRICE'] = boston.target
bos.head()

Step 3: Data Model Construction-Linear Regression

from sklearn.linear_model import LinearRegressionX = bos.drop('PRICE', axis=1)lm = LinearRegression()lm
lm.fit(X, bos.PRICE)
Print ('linear regression algorithm w value: ', lm. coef _) print ('linear regression algorithm B value:', lm. intercept _)
Import matplotlib. font_manager as fmmyfont = fm. fontProperties (fname = 'C:/Windows/Fonts/msyh. ttc ') plt. scatter (bos. RM, bos. PRICE) plt. xlabel (u 'average number of houses ', fontproperties = myfont) plt. ylabel (u'house price', fontproperties = myfont) plt. title (u'rm-PRICE relation ', fontproperties = myfont) plt. show ()

Step 4: Data Model Application-predicting House Price

lm.predict(X)[0:5]

Array ([30.00821269, 25.0298606, 30.5702317, 28.60814055, 27.94288232])

mse = np.mean((bos.PRICE - lm.predict(X)) ** 2)print(mse)

21.897779217687486

Summary

1. Use DESCR to explore the Boston dataset. The business goal is to predict housing prices in the Boston suburbs;

2. Use scikit-learn to fit the linear regression model for the entire dataset and calculate the mean square error.

Thinking stage

Split one pair of datasets into a training dataset and a test dataset.

2. Train the linear regression model on the Training dataset and use the linear regression model to predict the test dataset.

3. Calculate the MSE of the training model and the MSE of the prediction result of the test dataset.

4. Draw the residual diagram of the test Dataset

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.