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
#Import
data = pd.read_csv('uscrime.txt', sep="t")
crime = pd.concat([data], axis = 1)
print(crime)
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 = []
X_train_new.append(X_train.values)
y_train_new = []
y_train_new.append(y_train.values)
regression.fit(X_train_new, y_train_new)
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("http://www.statsci.org/data/general/uscrime.txt", sep="t")
x = data.loc[:, data.columns != 'Crime'].to_numpy()
y = np.squeeze(data.loc[:,'Crime'].to_numpy())
regression = LinearRegression()
regression.fit(x, 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
1 Asked on March 12, 2021 by youness-saadna
1 Asked on March 12, 2021 by khair
1 Asked on March 12, 2021
0 Asked on March 12, 2021 by dylan
1 Asked on March 12, 2021 by salman-ahmed
clustering feature selection machine learning machine learning model scikit learn
2 Asked on March 12, 2021 by tidylobster
2 Asked on March 11, 2021 by srj577
deep learning information extraction machine learning nlp semantic segmentation
5 Asked on March 11, 2021
0 Asked on March 11, 2021 by nick17
2 Asked on March 11, 2021 by vikasreddy
computer vision convolutional neural network deep learning image classification neural network
0 Asked on March 11, 2021 by joseph-doob
1 Asked on March 11, 2021 by indigo
backpropagation classification gradient descent machine learning neural network
1 Asked on March 11, 2021 by iain-maccormick
1 Asked on March 11, 2021 by william-abma
2 Asked on March 11, 2021 by pingu2k4
1 Asked on March 11, 2021 by chinti
1 Asked on March 11, 2021 by nixon
1 Asked on March 10, 2021 by 1b15
0 Asked on March 10, 2021
Get help from others!
Recent Questions
Recent Answers
© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP