Welcome to the world of Machine Learning !

Linear Regression Python Code

In this code we have used iris dataset of sklearn library. You can copy the code and execute it in juypter or PyCharm.

import pandas as pd
import numpy as np
from sklearn.datasets import load_diabetes
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
diabetes = load_diabetes()
data = pd.DataFrame(diabetes.data, columns=diabetes.feature_names)
data['target'] = diabetes.target
# Split the data into independent (X) and dependent (y) variables
X = data.iloc[:, :-1].values
y = data.iloc[:, -1].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
# Create the linear regression model
model = LinearRegression()
# Train the model on the training data
model.fit(X_train, y_train)
# Evaluate the model on the testing data
score = model.score(X_test, y_test)
print('Score:', score)
print(model.predict([X_test[1]]))

Score: 0.33223321731061817
[248.92812015]

Advertisement

Advertisement