Height weight
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
data = pd.read_csv('HeightWeight.csv')
print(data.info())
X = data[['Height']]
y = data['Weight']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
model = LinearRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
print(mse)
print(y_pred)
Or
Importnumpyasnp
Importpandasaspd
Fromsklearn.linear_modelimportLinearRegression
Fromsklearn.model_selectionimporttrain_test_split
#Createarandomdatasetwith10samples
Heights=np.random.normal(170,10,10)
Weights=np.random.normal(70,5,10)
#Combinethetwoarraysintoasingledataset
Dataset=pd.DataFrame({‘Height’:heights,‘Weight’:weights})
#Splitthedatasetintotrainingandtestingsets
X_train,X_test,y_train,y_test=train_test_split(dataset[‘Height’],dataset[‘Weight’],test_size=0.2,
random_state=42)
#CreateaLinearRegressionmodelandfitittothetrainingdata
Lr_model=LinearRegression()
Lr_model.fit(X_train.values.reshape(-1,1),y_train)
#Printthemodelcoefficients
Print(‘ModelCoefficients:’,lr_model.coef_)
#Predicttheweightsforthetestdataandprintthepredictions
Y_pred=lr_model.predict(X_test.values.reshape(-1,1))
Print(‘Predictions:’,y_pred