0% found this document useful (0 votes)
13 views4 pages

Exp 2 ML

The document outlines a Python script for performing simple linear regression analysis on a salary dataset based on years of experience. It includes steps for importing libraries, reading the dataset, splitting it into training and test sets, fitting a linear regression model, making predictions, and visualizing the results. Additionally, it calculates the regression coefficients manually and plots the regression line against the data points.

Uploaded by

anant chaudhari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Exp 2 ML

The document outlines a Python script for performing simple linear regression analysis on a salary dataset based on years of experience. It includes steps for importing libraries, reading the dataset, splitting it into training and test sets, fitting a linear regression model, making predictions, and visualizing the results. Additionally, it calculates the regression coefficients manually and plots the regression line against the data points.

Uploaded by

anant chaudhari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

EXP 2

# Importing the libraries


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset


dataset = pd.read_csv('/content/Salary_Data (1).csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 1].values

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 1/3, random_state = 0)

# Fitting Simple Linear Regression to the Training set


from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)

# Predicting the Test set results


y_pred = regressor.predict(X_test)

# Visualising the Training set results


plt.scatter(X_train, y_train, color = 'red')
plt.plot(X_train, regressor.predict(X_train), color = 'blue')
plt.title('Salary vs Experience (Training set)')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()

# Visualising the Test set results


plt.scatter(X_test, y_test, color = 'red')
plt.plot(X_train, regressor.predict(X_train), color = 'blue')
plt.title('Salary vs Experience (Test set)')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

# Step 1: Read the dataset


# Replace 'dataset.csv' with the path to your dataset file
df = pd.read_csv('/content/Salary_Data (1).csv')

# Step 2: Select 10 samples


f=df.head(10)

x = f["YearsExperience"]
print(x)
y = f["Salary"]
print(y)

mean_x = np.mean(x)
mean_y = np.mean(y)
print(mean_x, mean_y )

def calculate_beta(x, y):


mean_X = np.mean(x)
mean_Y = np.mean(y)
numerator = np.sum((x - mean_x) * (y - mean_y))
denominator = np.sum((x - mean_x)**2)
beta_1 = numerator / denominator
beta_0 = mean_y - beta_1 * mean_x
return beta_0, beta_1

beta_0, beta_1 = calculate_beta(x, y)


print(beta_0, beta_1)

Y_pred = beta_0 + beta_1 * x


print(Y_pred)
plt.figure(figsize=(10, 6))
plt.scatter(x, y, color='blue', label='Data points')
plt.plot( x,Y_pred, color='red', label='Regression line')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.title('Experience vs. Salary')
plt.legend()
plt.grid(True)
plt.show()

You might also like