易百教程人工智慧python修正-人工智慧監督學習(迴歸)

來源:互聯網
上載者:User

標籤:變化   如何   link   square   ring   jpg   point   cti   可視化   

迴歸是最重要的統計和機器學習工具之一。 我們認為機器學習的旅程從迴歸開始並不是錯的。 它可以被定義為使我們能夠根據資料做出決定的參數化技術,或者換言之,允許通過學習輸入和輸出變數之間的關係來基於資料做出預測。 這裡,依賴於輸入變數的輸出變數是連續值的實數。 在迴歸中,輸入和輸出變數之間的關係很重要,它有助於我們理解輸出變數的值隨輸入變數的變化而變化。 迴歸常用於預測價格,經濟,變化等。

在Python中構建迴歸器

在本節中,我們將學習如何構建單一以及多變數迴歸器。

線性迴歸器/單變數迴歸器

讓我們重點介紹一些必需的軟體包 -

import numpy as npfrom sklearn import linear_modelimport sklearn.metrics as smimport matplotlib.pyplot as plt

 

現在,我們需要提供輸入資料,並將資料儲存在名為linear.txt的檔案中。

input = ‘F:\\notebook\\linear.txt‘
使用 np.loadtxt函數載入這些資料。
input_data = np.loadtxt(input, delimiter=‘,‘)X, y = input_data[:, :-1], input_data[:, -1]

 

下一步將是培訓模型。下面給出培訓和測試樣本。

training_samples = int(0.6 * len(X))testing_samples = len(X) - num_trainingX_train, y_train = X[:training_samples], y[:training_samples]X_test, y_test = X[training_samples:], y[training_samples:]

 

現在,我們需要建立一個線性迴歸器對象。

reg_linear = linear_model.LinearRegression()

 

用訓練樣本訓練對象。
reg_linear.fit(X_train, y_train)

 

下面使用測試資料做預測。
y_test_pred = reg_linear.predict(X_test)

 

現在繪製並可視化資料。

plt.scatter(X_test, y_test, color = ‘red‘)plt.plot(X_test, y_test_pred, color = ‘black‘, linewidth = 2)plt.xticks(())plt.yticks(())plt.show()

 

執行上面範例程式碼,輸出以下結果 -

現在,我們可以計算線性迴歸的效能如下 -

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))

 

線性迴歸器的效能輸出結果如下 -
Mean absolute error = 1.78Mean squared error = 3.89Median absolute error = 2.01Explain variance score = -0.09R2 score = -0.09
 

在上面的代碼中,我們使用了這些小資料來源。 如果想要處理一些大的資料集,那麼可以使用sklearn.dataset來匯入更大的資料集。

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
 

多變數迴歸
首先,讓我們匯入一些必需的軟體包 -

import numpy as npfrom sklearn import linear_modelimport sklearn.metrics as smimport matplotlib.pyplot as pltfrom sklearn.preprocessing import PolynomialFeatures

 

現在,需要提供輸入資料,並將資料儲存在名為linear.txt的檔案中。

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

我們將通過使用np.loadtxt函數載入這些資料。

input_data = np.loadtxt(input, delimiter=‘,‘)X, y = input_data[:, :-1], input_data[:, -1]

下一步將是訓練模型; 會提供訓練和測試樣品資料。

training_samples = int(0.6 * len(X))testing_samples = len(X) - num_trainingX_train, y_train = X[:training_samples], y[:training_samples]X_test, y_test = X[training_samples:], y[training_samples:]

 

現在,我們需要建立一個線性迴歸器對象。

reg_linear_mul = linear_model.LinearRegression()

 

用訓練樣本訓練對象。
reg_linear_mul.fit(X_train, y_train)

 

現在,最後需要用測試資料做預測。

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))

線性迴歸器的效能輸出結果如下 -

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

現在,我們將建立一個10階多項式並訓練迴歸器。並提供樣本資料點。

polynomial = PolynomialFeatures(degree = 10)X_train_transformed = polynomial.fit_transform(X_train)datapoint = [[2.23, 1.35, 1.12]]poly_datapoint = polynomial.fit_transform(datapoint)poly_linear_model = 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))

 

線性迴歸 -

[2.40170462]
多項式迴歸 -
[1.8697225]
 

在上面的代碼中,我們使用了這些小資料。 如果想要一個大的資料集,那麼可以使用sklearn.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
 

易百教程移動端:請掃描本頁面底部(右側)二維碼並關注公眾號,回複:"教程" 選擇相關教程閱讀或直接存取:http://m.yiibai.com 。

易百教程人工智慧python修正-人工智慧監督學習(迴歸)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.