[Python] reg.predict is telling me I am not providing an array

Discussão em 'Python' iniciado por Stack, Abril 12, 2025.

  1. Stack

    Stack Membro Participativo

    I am new to Linear Regression in sklearn. It seems I have an issue with an array that I thought I coded correctly. When I ask for reg.score or reg.coef_ the code works great, but when I try to predict it throws an error that is saying it is not 2 dimensional or an array. Can somebody tell me what I did wrong on this one?

    Code is below.

    Thanks, Hoot

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    import seaborn as sns
    sns.set()

    from sklearn.linear_model import LinearRegression

    data =pd.read_csv('1.01. Simple linear regression.csv')

    x = data['SAT']
    y = data['GPA']

    x_matrix = x.values.reshape(-1,1) #creating a matrix for sklearn
    x_matrix.shape

    reg = LinearRegression()

    reg.fit(x_matrix,y)

    reg.score(x_matrix,y)

    reg.coef_

    reg.intercept_

    reg.predict(1740)

    *# Everything worked up till this point. This code is throwing the error saying that it is not 2 dimensional. I thought I fixed it in the x_matrix code.*

    Continue reading...

Compartilhe esta Página