In addition to Weibo, there is also WeChat
Please pay attention

WeChat public account
Shulou
2025-10-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "python over-fitting case analysis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The following figure visually shows the most serious over-fitting situation:
The model fits almost all the points, that is, the accuracy on the training set is close to 100%. What are the characteristics of this kind of model? Take a look at the parameters of this model:
1.24700471e-13.-2.35752755e-11, 2.06759733e-09,-1.11665116e-07.
4.15722794e-06.-1.13161697e-04, 2.33087852e-03,-3.70886530e-02.
4.61321531e-01.-4.50943817e+00, 3.46373724e+01,-2.07949995e+02.
9.65158102e+02.-3.40164962e+03, 8.85765503e+03,-1.63366853e+04.
1.99303609e+04.-1.41930185e+04, 4.37094529e+03, 2.87198980e+00.
A total of 20, which is exactly equal to the number of points that need to be fitted.
The above figure is fitted by Lagrangian interpolation method, with the help of scipy package to complete the interpolation, the code is shown below.
Data preparation phase:
From scipy.interpolate import lagrange
Import numpy as np
Import matplotlib.pyplot as plt
# number of samples used
N = 20
# seed ensures that a fixed random number is generated each time
Np.random.seed (2)
Eps = np.random.rand (n) * 2
# construct sample data
X = np.linspace (0,20, n)
Y = np.linspace (2,14, n) + eps
Call Lagrange interpolation to get the interpolation function p, and then input the interpolation point x to complete the interpolation to get the interpolation point (xx,yy).
# call Lagrangian interpolation to get the interpolation function p
P = lagrange (x, y)
Xx = x
Yy = p (xx)
A polynomial model is obtained by Lagrangian interpolation, and the number of parameters is equal to the number of samples.
Above we restore a method of fitting all sample points.
In order to improve the generalization ability of the model in machine learning, it is necessary to simplify the model parameters, in other words, to regularize the parameters, which is also in line with Occam's razor law, that is, the simple and effective principle.
The commonly used L1 regular will make the parameters sparse, and it will return the weights of some of them to 0. 5. Of course, as far as the data points to be fitted today are concerned, if the parameters of the model are directly simplified to 2, the fitting effect will not be bad.
Choose sklearn's simplest linetype regression model:
From sklearn import linear_model
Reg = linear_model.LinearRegression ()
Reg.fit (x.reshape (len (x),-1), y)
# get 2 parameter values
Reg.coef_,reg.intercept_
(array ([0.62182096]), 2.644854261121125)
Then the fitting effect under plot:
Plt.figure (figsize= (122.8))
Plt.scatter (x, y, color= "r")
# Lagrangian interpolation complex model
Plt.plot (xx, yy, color= "b", label='lagrange')
# Linetype regression minimalist model
Plt.plot (xx,xx*reg.coef_+reg.intercept_,color='green',label='linear_model')
Plt.show ()
This is the end of the content of "python over-fitting case Analysis". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.

The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope





About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.