Linear Regression not working due to wrong kind of array

Data Science Asked on September 16, 2020

I try to deal with my homework. The Job is to take this Data and perform a linear regression on it.

The code is published here.

I am quite new to programming in Python and in data science. So I tried transforming as the interpreter suggests, but it didn’t work.
My first error was that there was a 2d array expected but 1d given. Then I took the pure array and put it into an empty one suggested by a StackOverflow answer now the error is that a scalar array is given but a 2d array is given.

import pandas as pd
from sklearn.preprocessing import StandardScaler

data = pd.read_csv('uscrime.txt', sep="t")
crime = pd.concat([data], axis = 1)

from sklearn.linear_model import LinearRegression
regression = LinearRegression()
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(crime.get("M"), crime.get("Crime"), test_size=0.2, random_state=0)

X_train_new = []

y_train_new = []
y_train_new.append(y_train.values), y_train_new)

One Answer

import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split

data = pd.read_csv("", sep="t")
x = data.loc[:, data.columns != 'Crime'].to_numpy() 
y = np.squeeze(data.loc[:,'Crime'].to_numpy())

regression = LinearRegression(), y)

scikit learn expects numpy arrays and not pandas dataframes. You need to convert from one to the other on top, you need to make sure that the array for y only has one dimension which I achieved via np.squeeze. Bonus: see above how you can directly load the csv from the website.

Correct answer by PalimPalim on September 16, 2020

Add your own answers!

Related Questions

BLEU_SCORE gives bad scores – what am I doing wrong?

1  Asked on March 12, 2021 by youness-saadna


word2vec – log in the objective softmax function

2  Asked on March 12, 2021 by tidylobster


K-Medoid Clustering with Point Weights

0  Asked on March 11, 2021 by joseph-doob


Ask a Question

Get help from others!

© 2023 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP