代码之家  ›  专栏  ›  技术社区  ›  Jaquarh

如何在Python中为两个特性绘制散点图和预测线?

  •  -2
  • Jaquarh  · 技术社区  · 6 年前

         SibSp  Parch
    0        1      0
    1        1      0
    2        0      0
    3        1      0
    4        0      0
    5        0      0
    6        0      0
    7        3      1
    8        0      2
    9        1      0
    

    y 表示存活率,存活1例,死亡0例。X有更多的行。我正在使用 train_test_split(X, y, test_size=0.4, random_state=101) 获取培训和测试数据,并制定培训和测试方法。我的培训代码如下所示:

    def train():
        # Get Data Split
        X_train, X_test, y_train, y_test = initData()
    
        # Create LinearRegression Instance
        lm = LinearRegression()
    
        # Fit Training Values
        lm.fit(X_train,y_train)
    
        visualise(X_test, y_test, lm.predict(X_test))
    
        # Return Trained Data With Testing Data
        return X_test, y_test, lm
    

    def test():
        # Get The Trained Classifier
        X, y, lm = train()
    
        # Fit New Values
        lm.fit(X, y)
    
        visualise(X, y, lm.predict(X))
    

    这看起来很好——至少我认为是这样。我现在尝试将数据可视化为带有预测线图的散点图。

    def visualise(X, y, predictions):
        features = X.shape[1]
        colors   = ['red', 'blue']
        i        = 0
        while i <= features -1:
            plt.scatter(X.iloc[:, i], y, color=colors[i])
            # Update: Forgot to add this line when posting question
            plt.plot(X.iloc[:, i], predictions, color=colors[i])
            i=+1
    

    但这给了我疯狂的输出,到处都是线。我试着在网上查找,发现 sklearn's example . 这是我试图复制的:

    我想也许,因为我有两个特征,我可能需要分别识别它们。

    def visualise(X, y, predictions):
        newY = np.zeros(X.shape[0], X.shape[1]);
        newY[:, 0:1] = newY.iloc[:, 0]
        plt.scatter(X, y, color='blue')
        plt.plot(X, predictions, color='red')
    
        plt.xticks(())
        plt.yticks(())
    
        plt.show()
    

    newY = np.zeros(X.shape[0], X.shape[1]);

    def visualise(X, y, predictions):
        newY = np.zeros((X.shape[0], X.shape[1]));
        newY[:, 0] = y
        newY[:, 1] = y
        plt.scatter(X, newY, color='blue')
        plt.plot(X, predictions, color='red')
    

    现在修复错误,但这是我的输出:

    enter image description here

    如何绘制散点图和预测线?

    1 回复  |  直到 6 年前
        1
  •  1
  •   IronFarm    6 年前

    因为有两个特性,所以无法绘制预测线。如果有什么你可能想要一个预测等高线图。

    您的示例与这里的两个特色示例非常相似 https://scikit-learn.org/stable/auto_examples/svm/plot_iris.html