{"id":1311,"date":"2023-03-25T10:12:45","date_gmt":"2023-03-25T02:12:45","guid":{"rendered":""},"modified":"2023-03-25T10:12:45","modified_gmt":"2023-03-25T02:12:45","slug":"\u4eba\u5de5\u667a\u80fd\u76d1\u7763\u5b66\u4e60(\u56de\u5f52)","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1311.html","title":{"rendered":"\u4eba\u5de5\u667a\u80fd\u76d1\u7763\u5b66\u4e60(\u56de\u5f52)"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

\u4eba\u5de5\u667a\u80fd\u76d1\u7763\u5b66\u4e60(\u56de\u5f52)<\/h1>\n

\u4eba\u5de5\u667a\u80fd\u76d1\u7763\u5b66\u4e60(\u56de\u5f52)\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n

\n \u56de\u5f52\u662f\u6700\u91cd\u8981\u7684\u7edf\u8ba1\u548c\u673a\u5668\u5b66\u4e60\u5de5\u5177\u4e4b\u4e00\u3002 \u6211\u4eec\u8ba4\u4e3a\u673a\u5668\u5b66\u4e60\u7684\u65c5\u7a0b\u4ece\u56de\u5f52\u5f00\u59cb\u5e76\u4e0d\u662f\u9519\u7684\u3002 \u5b83\u53ef\u4ee5\u88ab\u5b9a\u4e49\u4e3a\u4f7f\u6211\u4eec\u80fd\u591f\u6839\u636e\u6570\u636e\u505a\u51fa\u51b3\u5b9a\u7684\u53c2\u6570\u5316\u6280\u672f\uff0c\u6216\u8005\u6362\u8a00\u4e4b\uff0c\u5141\u8bb8\u901a\u8fc7\u5b66\u4e60\u8f93\u5165\u548c\u8f93\u51fa\u53d8\u91cf\u4e4b\u95f4\u7684\u5173\u7cfb\u6765\u57fa\u4e8e\u6570\u636e\u505a\u51fa\u9884\u6d4b\u3002 \u8fd9\u91cc\uff0c\u4f9d\u8d56\u4e8e\u8f93\u5165\u53d8\u91cf\u7684\u8f93\u51fa\u53d8\u91cf\u662f\u8fde\u7eed\u503c\u7684\u5b9e\u6570\u3002 \u5728\u56de\u5f52\u4e2d\uff0c\u8f93\u5165\u548c\u8f93\u51fa\u53d8\u91cf\u4e4b\u95f4\u7684\u5173\u7cfb\u5f88\u91cd\u8981\uff0c\u5b83\u6709\u52a9\u4e8e\u6211\u4eec\u7406\u89e3\u8f93\u51fa\u53d8\u91cf\u7684\u503c\u968f\u8f93\u5165\u53d8\u91cf\u7684\u53d8\u5316\u800c\u53d8\u5316\u3002 \u56de\u5f52\u5e38\u7528\u4e8e\u9884\u6d4b\u4ef7\u683c\uff0c\u7ecf\u6d4e\uff0c\u53d8\u5316\u7b49\u3002\n <\/div>\n

\u5728Python\u4e2d\u6784\u5efa\u56de\u5f52\u5668<\/h2>\n
\n \u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u5b66\u4e60\u5982\u4f55\u6784\u5efa\u5355\u4e00\u4ee5\u53ca\u591a\u53d8\u91cf\u56de\u5f52\u5668\u3002\n <\/div>\n
\n \u7ebf\u6027\u56de\u5f52\u5668\/\u5355\u53d8\u91cf\u56de\u5f52\u5668<\/strong>\n <\/div>\n
\n \u8ba9\u6211\u4eec\u91cd\u70b9\u4ecb\u7ecd\u4e00\u4e9b\u5fc5\u9700\u7684\u8f6f\u4ef6\u5305 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
import <\/span>numpy as <\/span>np
from <\/span>sklearn import <\/span>linear_model
import <\/span>sklearn.metrics as <\/span>sm
import <\/span>matplotlib.pyplot as <\/span>plt
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\uff0c\u6211\u4eec\u9700\u8981\u63d0\u4f9b\u8f93\u5165\u6570\u636e\uff0c\u5e76\u5c06\u6570\u636e\u4fdd\u5b58\u5728\u540d\u4e3alinear.txt\u7684\u6587\u4ef6\u4e2d\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
input = 'D:\/ProgramData\/linear.txt'<\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4f7f\u7528np.loadtxt\u51fd\u6570\u52a0\u8f7d\u8fd9\u4e9b\u6570\u636e\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
input_data = np.loadtxt<\/span>(input, delimiter=','<\/span>)
X, y = input_data[:, :-1], input_data[:, -1]
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4e0b\u4e00\u6b65\u5c06\u662f\u57f9\u8bad\u6a21\u578b\u3002\u4e0b\u9762\u7ed9\u51fa\u57f9\u8bad\u548c\u6d4b\u8bd5\u6837\u672c\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
training_samples = int(0.6 * len<\/span>(X))
testing_samples = len(X) - num_training
X_train, y_train <\/span>= X[:training_samples], y[:training_samples]
X_test, y_test = X[training_samples:], y[training_samples:]
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u7ebf\u6027\u56de\u5f52\u5668\u5bf9\u8c61\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
reg_linear = linear_model.LinearRegression<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u7528\u8bad\u7ec3\u6837\u672c\u8bad\u7ec3\u5bf9\u8c61\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
reg_linear.fit<\/span>(X_train, y_train)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4e0b\u9762\u4f7f\u7528\u6d4b\u8bd5\u6570\u636e\u505a\u9884\u6d4b\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
y_test_pred = reg_linear.predict<\/span>(X_test)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\u7ed8\u5236\u5e76\u53ef\u89c6\u5316\u6570\u636e\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
plt.scatter<\/span>(X_test, y_test, color <\/span>= 'red'<\/span>)
plt.plot<\/span>(X_test, y_test_pred, color <\/span>= 'black'<\/span>, linewidth = 2)
plt.xticks<\/span>(())
plt.yticks<\/span>(())
plt.show<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u4e0a\u9762\u793a\u4f8b\u4ee3\u7801\uff0c\u8f93\u51fa\u4ee5\u4e0b\u7ed3\u679c -\n <\/div>\n
\n \u4eba\u5de5\u667a\u80fd\u76d1\u7763\u5b66\u4e60(\u56de\u5f52)_https:\/\/bianchenghao6.com_\u3010\u4eba\u5de5\u667a\u80fd\u6559\u7a0b\u3011_\u7b2c1\u5f20\n <\/div>\n
\n \u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba1\u7b97\u7ebf\u6027\u56de\u5f52\u7684\u6027\u80fd\u5982\u4e0b -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
print(\"Performance of Linear regressor:\"<\/span>)
print(\"Mean absolute error <\/span>=\"<\/span>, round(sm.mean_absolute_error<\/span>(y_test, y_test_pred), 2))
print(\"Mean squared error <\/span>=\"<\/span>, round(sm.mean_squared_error<\/span>(y_test, y_test_pred), 2))
print(\"Median absolute error <\/span>=\"<\/span>, round(sm.median_absolute_error<\/span>(y_test, y_test_pred), 2))
print(\"Explain <\/span>variance score =\"<\/span>, round(sm.explained_variance_score<\/span>(y_test, y_test_pred),
2))
print(\"R2 score =\"<\/span>, round(sm.r2_score<\/span>(y_test, y_test_pred), 2))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u7ebf\u6027\u56de\u5f52\u5668\u7684\u6027\u80fd\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
Mean absolute error <\/span>= 1.78
Mean squared error <\/span>= 3.89
Median absolute error <\/span>= 2.01
Explain <\/span>variance score = -0.09
R2 score = -0.09
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u8fd9\u4e9b\u5c0f\u6570\u636e\u6e90\u3002 \u5982\u679c\u60f3\u8981\u5904\u7406\u4e00\u4e9b\u5927\u7684\u6570\u636e\u96c6\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528sklearn.dataset\u6765\u5bfc\u5165\u66f4\u5927\u7684\u6570\u636e\u96c6\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
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
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u591a\u53d8\u91cf\u56de\u5f52<\/strong>\u9996\u5148\uff0c\u8ba9\u6211\u4eec\u5bfc\u5165\u4e00\u4e9b\u5fc5\u9700\u7684\u8f6f\u4ef6\u5305 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
import <\/span>numpy as <\/span>np
from <\/span>sklearn import <\/span>linear_model
import <\/span>sklearn.metrics as <\/span>sm
import <\/span>matplotlib.pyplot as <\/span>plt
from <\/span>sklearn.preprocessing import <\/span>PolynomialFeatures
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\uff0c\u9700\u8981\u63d0\u4f9b\u8f93\u5165\u6570\u636e\uff0c\u5e76\u5c06\u6570\u636e\u4fdd\u5b58\u5728\u540d\u4e3alinear.txt\u7684\u6587\u4ef6\u4e2d\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
input = 'D:\/ProgramData\/Mul_linear.txt'<\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6211\u4eec\u5c06\u901a\u8fc7\u4f7f\u7528np.loadtxt\u51fd\u6570\u52a0\u8f7d\u8fd9\u4e9b\u6570\u636e\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
input_data = np.loadtxt<\/span>(input, delimiter=','<\/span>)
X, y = input_data[:, :-1], input_data[:, -1]
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4e0b\u4e00\u6b65\u5c06\u662f\u8bad\u7ec3\u6a21\u578b; \u4f1a\u63d0\u4f9b\u8bad\u7ec3\u548c\u6d4b\u8bd5\u6837\u54c1\u6570\u636e\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
training_samples = int(0.6 * len<\/span>(X))
testing_samples = len(X) - num_training
X_train, y_train <\/span>= X[:training_samples], y[:training_samples]
X_test, y_test = X[training_samples:], y[training_samples:]
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u7ebf\u6027\u56de\u5f52\u5668\u5bf9\u8c61\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
reg_linear_mul = linear_model.LinearRegression<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u7528\u8bad\u7ec3\u6837\u672c\u8bad\u7ec3\u5bf9\u8c61\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
reg_linear_mul.fit<\/span>(X_train, y_train)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\uff0c\u6700\u540e\u9700\u8981\u7528\u6d4b\u8bd5\u6570\u636e\u505a\u9884\u6d4b\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
y_test_pred = reg_linear_mul.predict<\/span>(X_test)
print(\"Performance of Linear regressor:\"<\/span>)
print(\"Mean absolute error <\/span>=\"<\/span>, round(sm.mean_absolute_error<\/span>(y_test, y_test_pred), 2))
print(\"Mean squared error <\/span>=\"<\/span>, round(sm.mean_squared_error<\/span>(y_test, y_test_pred), 2))
print(\"Median absolute error <\/span>=\"<\/span>, round(sm.median_absolute_error<\/span>(y_test, y_test_pred), 2))
print(\"Explain <\/span>variance score =\"<\/span>, round(sm.explained_variance_score<\/span>(y_test, y_test_pred), 2))
print(\"R2 score =\"<\/span>, round(sm.r2_score<\/span>(y_test, y_test_pred), 2))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u7ebf\u6027\u56de\u5f52\u5668\u7684\u6027\u80fd\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
Mean absolute error <\/span>= 0.6
Mean squared error <\/span>= 0.65
Median absolute error <\/span>= 0.41
Explain <\/span>variance score = 0.34
R2 score = 0.33
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a10\u9636\u591a\u9879\u5f0f\u5e76\u8bad\u7ec3\u56de\u5f52\u5668\u3002\u5e76\u63d0\u4f9b\u6837\u672c\u6570\u636e\u70b9\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
polynomial = PolynomialFeatures(degree = 10)
X_train_transformed = polynomial.fit_transform<\/span>(X_train)
datapoint = [[2.23, 1.35, 1.12]]
poly_datapoint = polynomial.fit_transform<\/span>(datapoint)
poly_linear_model <\/span>= linear_model.LinearRegression<\/span>()
poly_linear_model.fit<\/span>(X_train_transformed, y_train)
print(\"\\nLinear regression:\\n\"<\/span>, reg_linear_mul.predict<\/span>(datapoint))
print(\"\\nPolynomial regression:\\n\"<\/span>, poly_linear_model.predict<\/span>(poly_datapoint))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u7ebf\u6027\u56de\u5f52 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
[2.40170462]
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u591a\u9879\u5f0f\u56de\u5f52 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
[1.8697225]
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u8fd9\u4e9b\u5c0f\u6570\u636e\u3002 \u5982\u679c\u60f3\u8981\u4e00\u4e2a\u5927\u7684\u6570\u636e\u96c6\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528sklearn.dataset\u6765\u5bfc\u5165\u4e00\u4e2a\u66f4\u5927\u7684\u6570\u636e\u96c6\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
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
<\/span><\/code><\/pre>\n<\/p><\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"\u4eba\u5de5\u667a\u80fd\u76d1\u7763\u5b66\u4e60(\u56de\u5f52)zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[137],"tags":[],"class_list":["post-1311","post","type-post","status-publish","format-standard","hentry","category-rgznjc1"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1311"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1311"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1311\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}