ĐỂ BẮT ĐẦU
{{noshy}}# IMPORT DATA LIBRARIES
import pandas as pd
import numpy as np
# IMPORT VIS LIBRARIES
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
# IMPORT MODELLING LIBRARIES
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn import metrics
Hoạt động sơ bộ
df = pd.read_csv ['data.csv'] | Đọc dữ liệu |
df.head[] | Kiểm tra đầu DF |
df.info[] | Kiểm tra thông tin DF |
df.describe [] | Kiểm tra số liệu thống kê df |
df.columns | Kiểm tra tên col |
Trực quan hóa dữ liệu
SNS.Pairplot [DF] | cặp |
sns.distplot [df ['y']]] | âm mưu phân phối |
sns.HeatMap [df.corr [], annot = true] | Nhiệt Hap với các giá trị |
Mô hình xe lửa
Tạo X và Y --------------- --------------- | |
X = df [['col1', 'col2', v.v.]]]] | Tạo các tính năng DF |
y = df ['col'] | Tạo DF VAR để dự đoán |
Chia dữ liệu chia --------------- --------------- | |
X_train, x_test, y_train, y_test = Train_test_split [& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; x, & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; test_size = 0.3] | Chia DF trong tàu hỏa và thử nghiệm DF |
Phù hợp với mô hình --------------- --------------- | |
lm = tuyến tính [] | Mô hình Instatiate |
lm.fit [x_train, y_train] | Train/phù hợp với mô hình |
Hiển thị kết quả --------------- --------------- | |
lm.Intercept_ | Hiển thị đánh chặn |
lm.coef_ | Hiển thị hệ số |
coeff_df = pd.dataframe [lm.Coef_, x.columns, cột = ['coeff']]** | Tạo coeff DF |
pd.dataFrame: pd.dataFrame [data = none, index = none, cột = none, dtype = none, copy = false]. data = value, index = tên index, cột = cột tên. Điều này có thể hữu ích chỉ để giải thích hệ số của hồi quy.: pd.DataFrame[data=None, index=None, columns=None, dtype=None, copy=False]. data = values, index= name index, columns= name column. This could be useful just to interpret the coefficient of the regression.
Đưa ra dự đoán
Dự đoán = lm.predict [x_test] | Tạo dự đoán |
plt.scatter [y_test, dự đoán]* | Dự đoán cốt truyện |
sns.distplot [[y_test-predictions], bins = 50]* | méo phần dư |
Phân tán: Biểu đồ này cho thấy sự khác biệt giữa các giá trị thực và các giá trị được dự đoán bởi mô hình chúng tôi đã đào tạo. Nó sẽ giống với càng nhiều càng tốt một đường chéo.distplot: biểu đồ này cho thấy các phân phối của các lỗi còn lại, nghĩa là sự khác biệt giữa các giá trị thực tế trừ đi các giá trị dự đoán; Nó sẽ dẫn đến một phân phối bình thường càng nhiều càng tốt. Nếu không, có thể thay đổi mô hình!: this graph show the difference between actual values and the values predicted by the model we trained. It should resemble as much as possible a diagonal line.
distplot: this graph shows the distributions of the residual errors, that is, the difference between the actual values minus the predicted values; it should result in an as much as possible normal distribution. If not, maybe change
model!
Số liệu đánh giá
in ['mae:', số liệu.mean_absolute_error [y_test, dự đoán]]]metrics.mean_absolute_error[y_test, predictions]] |
in ['MSE:', số liệu.mean_squared_error [y_test, dự đoán]]]metrics.mean_squared_error[y_test, predictions]] |
in ['rmse:', np.sqrt [metrics.mean_squared_error [y_test, dự đoán]]]]np.sqrt[metrics.mean_squared_error[y_test, predictions]]] |
Mae là dễ hiểu nhất, bởi vì đó là lỗi trung bình trong các đơn vị "y". is the easiest to understand, because it's the average error.
MSE is more popular than MAE, because MSE "punishes" larger errors, which tends to be useful in the real world.
RMSE is even more popular than MSE, because RMSE is interpretable in the "y" units.
Chủ đề
Hồi quy tuyến tính
Bước đi xuống độ dốc
Kích thước của bước mà độ dốc giảm dần được gọi là tỷ lệ học tập. Tìm một giá trị phù hợp cho tỷ lệ học tập là chìa khóa để đạt được sự hội tụ. Nếu giá trị này quá lớn, thuật toán sẽ không bao giờ đạt đến Optimus, nhưng nếu quá nhỏ thì sẽ mất quá nhiều thời gian để đạt được giá trị mong muốn.
Độ dốc xuống trong hồi quy
Gradient Descent
là một thuật toán lặp được sử dụng để điều chỉnh các tham số trong các mô hình hồi quy để mất tối thiểu.
Tìm hiểu thêm về Codecademy
PRO JERYSKILL PATH
Phân tích dữ liệu tài chính với Python
Người mới bắt đầu thân thiện,,
19Lessons Lessons