Python 我衡量多元线性回归模型的绩效是否正确?
这可能是一个有点愚蠢的问题(可能是一个微不足道的问题),但我对机器学习还是新手。这可能很容易从我提出的代码中推断出来,这并不是一个表述糟糕的问题的借口。如果你发现这个问题表述得很糟糕,请通知我,以便我可以更新它 我训练了一个多元线性回归模型,我想看看它对给定数据集的表现如何。所以,我在谷歌上搜索了一下,找到了解释如何从真实值中找出预测值的“误差”的方法。它给了我几个选择: 我应用了所有这些,它们给了我难以置信的高值,所以我不知道这些是否正确,或者我应该如何解释它们 文章接收的输出:Python 我衡量多元线性回归模型的绩效是否正确?,python,machine-learning,regression,Python,Machine Learning,Regression,这可能是一个有点愚蠢的问题(可能是一个微不足道的问题),但我对机器学习还是新手。这可能很容易从我提出的代码中推断出来,这并不是一个表述糟糕的问题的借口。如果你发现这个问题表述得很糟糕,请通知我,以便我可以更新它 我训练了一个多元线性回归模型,我想看看它对给定数据集的表现如何。所以,我在谷歌上搜索了一下,找到了解释如何从真实值中找出预测值的“误差”的方法。它给了我几个选择: 我应用了所有这些,它们给了我难以置信的高值,所以我不知道这些是否正确,或者我应该如何解释它们 文章接收的输出: 10.0
- 10.0
- 150.0
- 12.2474487139
- 7514.293659640891
- 83502864.03257468
- 9137.990152794797
我希望这有帮助,Umberto您的标签是一个热编码的,您正在进行回归?我想你要做的是分类。我这样做是为了把各州分成各自的列。正如我所说,我是机器学习新手,我正在学习一门机器学习课程。遗憾的是,他们没有包括一种方法来衡量模型的性能。@UmangGupta我添加了一个真实/预测数据的屏幕截图,希望能澄清问题。@UmangGupta数据的平均值看起来很大,这意味着错误的平均值可能很好。您能否打印出sqrt均方误差除以测试集中目标变量平均绝对值的结果?你不能说均方误差的sqrt“很大”,而不知道误差相对于你的观察值本身的大小有多大。举个例子,想象一个数据集,你在预测股票价格,精确到几便士是很重要的。现在想象另一个问题,你在预测遥远星系之间的距离。数十亿英里范围内的准确度可能非常好。我想你是对的。谢谢你的回答。你几乎把目标弄对了;“试着根据初创公司在研发、管理、营销和所在州的花费来预测其利润,以确定投资哪些公司。”如果你想尝试一些线性回归,试试波士顿房价数据集:这是你可以尝试你的模型的东西。这个关系不是线性的,但是如果你尝试。。。我建议你试试。对于初创企业来说,很难从你在研究、管理等方面的投资等数据中推断出利润。。。
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.linear_model import LinearRegression
from sklearn import metrics
dataset = pd.read_csv('50_Startups.csv')
X = dataset.iloc[:, :-1].values # Independent variables
y = dataset.iloc[:, 4].values # Dependent variable
# Encode categorical data into numerical values (1, 2, 3)
# For example; New york becomes 1 and Florida becomes 2
labelencoder_states = LabelEncoder()
# We just want to apply this to the state column, since this has categorical data
states_encoded = labelencoder_states.fit_transform(X[:, 3])
# Update the states with the new encoded data
X[:, 3] = states_encoded
# Now that we have the categories as numerical data,
# we can split them into multiple dummy variables:
# Split the categories into columns (more optimal)
# Tell it too look at the state column
onehotencoder_states = OneHotEncoder(categorical_features = [3])
# Actually transforms them into columns
X = onehotencoder_states.fit_transform(X).toarray()
# Avoiding the Dummy Variable Trap
# Remove the first column from X
# Since; dummy variables -1
X = X[:, 1:]
# Splitting the dataset into the Training set and Test set
# In this case we are going to use 40 of the 50 records for training
# and ten of the 50 for testing, hence the 0.2 split ratio
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
# Create a regressor
regressor = LinearRegression()
# Fit the model to the training data
regressor.fit(X_train, y_train)
# Make predictions on the test set, using our model
y_pred = regressor.predict(X_test)
# Evaluating the model (Am I doing this correct?)
# How well did it do?
print(metrics.mean_absolute_error(y_test, y_pred))
print(metrics.mean_squared_error(y_test, y_pred))
print(np.sqrt(metrics.mean_squared_error(y_test, y_pred)))