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 realize Linear regression by Python

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Python how to achieve linear regression, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

1. Overview (1) artificial intelligence learning

(2) Machine learning

(3) supervised learning

(4) Linear regression

2. Linear regression (1) implementation steps

Calculate loss based on randomly initialized w x b and y

Calculate the gradient based on the current values of w x b and y

Update the gradient, the loop assigns new w' and b' to w and b, and finally obtains an optimal w' and b' as the final of the equation.

(2) Mathematical expression

3. Code implementation (Python) (1) Machine Learning Library (sklearn.linear_model)

Code:

From sklearn import linear_modelfrom sklearn.linear_model import LinearRegressionimport matplotlib.pyplot as plt# is used to map from pylab import * mpl.rcParams ['font.sans-serif'] = [' SimHei'] mpl.rcParams ['axes.unicode_minus'] = Falseimport numpy as np# to create vector reg=linear_model.LinearRegression (fit_intercept=True,normalize=False) x = [[32.50235], [53.4268], [61.53036], [47.47564], [59.81321], [55.14219], [52.14219], [39.29957] [48.10504], [52.55001], [45.41873], [54.35163], [44.16405], [58.16847], [56.72721]] y = [31.70701, 62.56238, 71.54663, 87.23093, 78.21152, 79.64, 1979.17144, 71.3008, 52.16568, 62.00888, 62.00287, 81.43619] reg.fit (xQuy) k=reg.coef_# acquires the slope of w1th, w2, and w3, and gets the intercept w0x0=np.arange (30602, 0.2) (y0=k*x0+bprint "= {0}). B = {1} ".format (KMagneb)) plt.scatter (XMagi y) plt.plot (x0LLY) plt.xlabel ('X') plt.ylabel ('Y') plt.legend () plt.show ()

Results:

K = [1.36695374], baked 0.13079331831460195

(2) detailed implementation of Python (method 1)

Code:

# method 1import numpy as npimport matplotlib.pyplot as pltfrom pylab import * mpl.rcParams ['font.sans-serif'] = [' SimHei'] mpl.rcParams ['axes.unicode_minus'] = False# data generation data = [] for i in range (100): X = np.random.uniform (3.12.) # mean=0, std=1 eps = np.random.normal (0.1,1) y = 1.677 * x + 0.039 + eps data.append ([x Y]) data = np.array (data) # Statistical error # y = wx + bdef compute_error_for_line_given_points (b, w, points): totalError = 0 for i in range (0, len (points)): X = points [I, 0] y = points [I 1] # computer mean-squared-error totalError + = (y-(w * x + b)) * * 2 # average loss for each point return totalError / float (len (points)) # calculate gradient def step_gradient (b_current, w_current, points, learningRate): b_gradient = 0 w_gradient = 0 N = float (len (points)) for i in range (0, len (points)): X = points [I 0] y = points [I 1] # grad_b = 2 (wx+b-y) b_gradient + = (2max N) * ((w_current * Xubb _ current)-y) # grad_w = 2 (wx+b-y) * x w_gradient + = (2max N) * x * ((w_current * Xubb _ current)-y) # update w'new_b = b_current-(learningRate * b) _ gradient) new_w = w_current-(learningRate * w_gradient) return [new_b New_w] # iterative update def gradient_descent_runner (points, starting_b, starting_w, learning_rate, num_iterations): B = starting_b w = starting_w # update for several times for i in range (num_iterations): B, w = step_gradient (b, w, np.array (points), learning_rate) return [b W] def main (): learning_rate = 0.0001 initial_b = 0 # initial y-intercept guess initial_w = 0 # initial slope guess num_iterations = 1000 print ("pre-iteration b = {0}, w = {1}, error = {2}" .format (initial_b, initial_w, compute_error_for_line_given_points (initial_b, initial_w) Data)) print ("Running...") [B, w] = gradient_descent_runner (data, initial_b, initial_w, learning_rate, num_iterations) print ("the result of the {0} iteration b = {1}, w = {2}, error = {3}". Format (num_iterations, b, w, compute_error_for_line_given_points (b, w, data)) plt.plot (data [:, 0], data [:, 1], color='b', marker='+', linestyle='--',label='true') plt.plot (data [:, 0], w*data [:, 0] + blot colorcolors Label='predict') plt.xlabel ('X') plt.ylabel ('Y') plt.legend () plt.show () if _ _ name__ ='_ _ main__': main ()

Results:

Before iteration: B = 0, w = 0, error = 186.61000821356697

Running...

The result of the 1000 iteration: B = 0.20558501549252192, w = 1.6589067569038516, error = 0.9963685680112963

(3) detailed implementation of Python (method 2)

Code:

# method 2 import numpy as npimport pandas as pdimport matplotlib.pyplot as pltimport matplotlib as mplmpl.rcParams ["font.sans-serif"] = ["SimHei"] mpl.rcParams ["axes.unicode_minus"] = False # y = wx + b#Import datafile=pd.read_csv ("data.csv") def compute_error_for_line_given (b W): totalError = np.sum ((file ['y']-(w*file ['x'] + b)) * * 2) return np.mean (totalError) def step_gradient (b_current, w_current, learningRate): b_gradient = 0 w_gradient = 0 N = float (len (file ['x'])) for i in range (0 Len (file ['x']): # grad_b = 2 (wx+b-y) b_gradient + = (2 / N) * ((w_current * file ['x'] + b_current)-file ['y']) # grad_w = 2 (wx+b-y) * x w_gradient + = (2 / N) * file ['x'] * (w_current * file ['x'] + b_current)-file ['x']) # update w' new_b = b_current-(learningRate * b_gradient) new_w = w_current-(learningRate * w_gradient) return [new_b New_w] def gradient_descent_runner (starting_b, starting_w, learning_rate, num_iterations): B = starting_b w = starting_w # update for several times for i in range (num_iterations): B, w = step_gradient (b, w, learning_rate) return [b W] def main (): learning_rate = 0.0001 initial_b = 0 # initial y-intercept guess initial_w = 0 # initial slope guess num_iterations = 100print ("Starting gradient descent at b = {0}, w = {1}, error = {2}" .format (initial_b, initial_w, compute_error_for_line_given (initial_b) Initial_w)) print ("Running...") [B, w] = gradient_descent_runner (initial_b, initial_w, learning_rate, num_iterations) print ("After {0} iterations b = {1}, w = {2}, error = {3}". Format (num_iterations, b, w, compute_error_for_line_given (b, w)) plt.plot (file ['x'], file ['y'], 'ro',label=' linear regression') plt.xlabel ('X') plt.ylabel ('Y') plt.legend () plt.show () if _ name__ = ='_ main__': main ()

Results:

Starting gradient descent at b = 0, w = 0, error = 75104.71822821398Running...After 100 iterations b = 0 0.0148451 0.3256212 0.0368833 0.5022654 0.5649175 0.4793666 0.5689687 0.4226198 0.5650739 0.39390710 0.21685411 0.58075012 0.37935013 0.36157414 0.511651dtype: float64 W = 0 0.9995201 0.9940062 0.9994053 0.9896454 0.9906835 0.9914446 0.9892827 0.9895738 0.9884989 0.99263310 0.99532911 0.98949012 0.99161713 0.99387214 0.991116dtype: float64, error = 6451.5510231710905

Data:

(4) Python detailed implementation (method 3) # method 3 import numpy as np points = np.genfromtxt ("data.csv", delimiter= ",") # requires two iterative loops from data input to return. The first iteration converts each line in the file into a string sequence. # the second loop iteration specifies the appropriate data type for each string sequence: # y = wx + bdef compute_error_for_line_given_points (b, w, points): totalError = 0 for i in range (0, len (points)): X = points [I, 0] y = points [I 1] # computer mean-squared-error totalError + = (y-(w * x + b)) * * 2 # average loss for each point return totalError / float (len (points)) def step_gradient (b_current, w_current, points, learningRate): b_gradient = 0 w_gradient = 0 N = float (len (points)) for i in range (0, len (points)): X = points [I, 0] y = points [I 1] # grad_b = 2 (wx+b-y) b_gradient + = (2 / N) * ((w_current * xroomb _ current)-y) # grad_w = 2 (wx+b-y) * x w_gradient + = (2 / N) * x * ((w_current * xroomb _ current)-y) # update w'new_b = b_current-(learningRate * b) _ gradient) new_w = w_current-(learningRate * w_gradient) return [new_b New_w] def gradient_descent_runner (points, starting_b, starting_w, learning_rate, num_iterations): B = starting_b w = starting_w # update for several times for i in range (num_iterations): B, w = step_gradient (b, w, np.array (points), learning_rate) return [b W] def main (): learning_rate = 0.0001 initial_b = 0 # initial y-intercept guess initial_w = 0 # initial slope guess num_iterations = 1000 print ("Starting gradient descent at b = {0}, w = {1}, error = {2}" .format (initial_b, initial_w, compute_error_for_line_given_points (initial_b, initial_w) Points)) print ("Running...") [B, w] = gradient_descent_runner (points, initial_b, initial_w, learning_rate, num_iterations) print ("After {0} iterations b = {1}, w = {2}, error = {3}". Format (num_iterations, b, w, compute_error_for_line_given_points (b, w, points)) if _ _ name__ = ='_ main__': main () 4, case-house and price, size

(1) Code # 1 Import package import matplotlib.pyplot as pltimport numpy as npimport pandas as pdfrom sklearn import linear_model # 2. Load the training data, establish the regression equation # take the data set (1) datasets_X = [] # store the house area datasets_Y = [] # store the transaction price fr = open ('house price and house size .csv','r') # read the file, r: open the file read-only, w: open a file for writing only. Lines = fr.readlines () # read the entire file at once. For line in lines: # operates line by line, iterating through all the data items = line.strip (). Split (',') # removes commas from the data file, and strip () is used to remove the character specified at the beginning and end of the string (the default is a space or newline character) or character sequence. # split (''): slices the string by specifying the delimiter, and separates the num+1 substrings if the parameter num has the specified value. Datasets_X.append (int (items [0])) # converts the read data to int type, and writes datasets_Y.append (int (items [1])) length = len (datasets_X) # to get the length of datasets_X, that is, the total number of data datasets_X = np.array (datasets_X). Reshape ([length,1]) # converts datasets_X into an array and becomes one-dimensional To meet the input parameter requirements of linear regression fitting function datasets_Y = np.array (datasets_Y) # convert datasets_Y into an array # fetch data set (2)''fr = pd.read_csv ('house price and house size .csv' Encoding='utf-8') datasets_X=fr ['housing area'] datasets_Y=fr ['transaction price']''minX = min (datasets_X) maxX = max (datasets_X) X = np.arange (minX,maxX). Reshape ([- 1mai 1]) # takes the maximum and minimum values of data datasets_X as the range The isometric sequence is established to facilitate subsequent drawing. # reshape ([- 1jue 1]), converted to 1 column, reshape ([2je linear_model.LinearRegression 1]): converted into two lines linear = linear_model.LinearRegression () # call the linear regression module, establish the regression equation, fit the data linear.fit (datasets_X, datasets_Y) # 3. Slope and intercept print ('Coefficients:', linear.coef_) # View regression equation coefficients (k) print (' intercept:', linear.intercept_) # # View regression equation intercept (b) print ('y = {0} x + {1} '.format (linear.coef_,linear.intercept_)) # fit line # 4. The image shows plt.scatter (datasets_X, datasets_Y, color = 'red') plt.plot (X, linear.predict (X), color =' blue') plt.xlabel ('Area') plt.ylabel (' Price') plt.show () (2) result Coefficients: [0.14198749] intercept: 53.43633899175563y53.43633899175563

(3) data

The first column is the housing area, and the second column is the transaction price:

So far, this is the end of this article on the realization of linear regression in artificial intelligence-Python. For more information about the realization of linear regression in Python, please search for previous articles or continue to browse the relevant articles below. I hope you will support it in the future!

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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