Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我衡量多元线性回归模型的绩效是否正确?_Python_Machine Learning_Regression - Fatal编程技术网

Python 我衡量多元线性回归模型的绩效是否正确?

Python 我衡量多元线性回归模型的绩效是否正确?,python,machine-learning,regression,Python,Machine Learning,Regression,这可能是一个有点愚蠢的问题(可能是一个微不足道的问题),但我对机器学习还是新手。这可能很容易从我提出的代码中推断出来,这并不是一个表述糟糕的问题的借口。如果你发现这个问题表述得很糟糕,请通知我,以便我可以更新它 我训练了一个多元线性回归模型,我想看看它对给定数据集的表现如何。所以,我在谷歌上搜索了一下,找到了解释如何从真实值中找出预测值的“误差”的方法。它给了我几个选择: 我应用了所有这些,它们给了我难以置信的高值,所以我不知道这些是否正确,或者我应该如何解释它们 文章接收的输出: 10.0

这可能是一个有点愚蠢的问题(可能是一个微不足道的问题),但我对机器学习还是新手。这可能很容易从我提出的代码中推断出来,这并不是一个表述糟糕的问题的借口。如果你发现这个问题表述得很糟糕,请通知我,以便我可以更新它

我训练了一个多元线性回归模型,我想看看它对给定数据集的表现如何。所以,我在谷歌上搜索了一下,找到了解释如何从真实值中找出预测值的“误差”的方法。它给了我几个选择:

我应用了所有这些,它们给了我难以置信的高值,所以我不知道这些是否正确,或者我应该如何解释它们

文章接收的输出:

  • 10.0
  • 150.0
  • 12.2474487139
我的模型收到的输出:

  • 7514.293659640891
  • 83502864.03257468
  • 9137.990152794797
作为快速参考,这些是我的真实/预测值

关于“TLDR”的问题:我是否使用上述方法正确测量了我的误差,这些结果是否意味着我的模型性能非常差?(当我将预测值与真实值进行比较时,情况似乎并非如此)

你可以看看我使用的数据集

我用来创建模型和预测值的代码(我试图删除不需要的代码)

让我们来回答这个问题: 我认为您的测量(至少使用代码)是正确的。但是:

  • 谁在告诉你这种关系是线性的?你试图预测利润(对吗?)。我想说,线性回归可能不会很好地工作。所以我对你没有得到好结果并不感到惊讶

  • 为了了解你的预测是如何工作的,试着绘制预测与真实的对比图,并检查你的点在一条线上保持得有多好

  • 总而言之:获得大值并不意味着您的代码是错误的。很可能这种关系不是线性的

    另一方面:使用分类变量可能是问题的根源。你有没有试过做没有状态的线性回归?你的结果是什么?哪一个变量在回归中最重要?你应该检查一下。你的R平方是多少


    我希望这有帮助,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)))