Create a linear regression model within 20 minutes in Python
Question
This question was asked in one of the interviews where the ask was to create a Linear Regression model from importing library to the final prediction within 20 minutes.
The objective was to check if you can think fast and are fluent in basics of model building.
Even better, create your own dataset with 5 independent variable in MS Excel
in progress
0
Machine Learning
55 years
2 Answers
694 views
Grand Master 0
Answers ( 2 )
data = pd.read_csv(‘data.csv’) # load data set
X = data.iloc[:, 0].values.reshape(-1, 1) # values converts it into a numpy array
Y = data.iloc[:, 1].values.reshape(-1, 1) # -1 means that calculate the dimension of rows, but have 1 column
linear_regressor = LinearRegression() # create object for the class
linear_regressor.fit(X, Y) # perform linear regression
Y_pred = linear_regressor.predict(X) # make predictions
# i have created a simple model to predict height on the basis of weight and gender.
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
data = pd.read_csv(r”data.csv”)
print(data)
#check for missing values
data.isnull().sum()
# encoding hot encoder
data[“Sex”] = np.where(data[“Gender”] ==”F” , 1, 0)
data.drop([“Gender”] , axis = 1)
correlation = data.corr()
X = data.drop([“Height”, “Gender”], axis = 1)
Y = data[“Height”]
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=42)
print(X_train)
lm = LinearRegression().fit(X_train, y_train)
y_predict = lm.predict(X_test)
print(y_predict)
print(“regression coefficient”, lm.score(X_train, y_train))