



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




import numpy as nm import matplotlib.pyplot as mtp import pandas as pd data_set= pd.read_csv('Salary_Data.csv') x= data_set.iloc[:, :-1].values y= data_set.iloc[:, 1].values
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) STEP-2: TRAINING DATASET #Fitting the Simple Linear Regression model to the training dataset from sklearn.linear_model import LinearRegression regressor= LinearRegression() regressor.fit(x_train, y_train) STEP-3: PREDICTION OF TEST SET RESULT #Prediction of Test and Training set result y_pred= regressor.predict(x_test) x_pred= regressor.predict(x_train) STEP-4: VISUALIZING THE TRAINING SET RESULTS mtp.scatter(x_train, y_train, color="green") mtp.plot(x_train, x_pred, color="red")
mtp.title("Salary vs Experience (Training Dataset)") mtp.xlabel("Years of Experience") mtp.ylabel("Salary(In Rupees)") mtp.show() STEP-5: VISUALIZING THE TEST SET RESULTS #visualizing the Test set results mtp.scatter(x_test, y_test, color="blue") mtp.plot(x_train, x_pred, color="red") mtp.title("Salary vs Experience (Test Dataset)") mtp.xlabel("Years of Experience") mtp.ylabel("Salary(In Rupees)") mtp.show()
classifier.fit(x_train, y_train) LogisticRegression(C=1.0,class_weight=None,dual=False,fit_intercept=True, intercept_scaling=1, l1_ratio=None, max_iter=100, multi_class='warn', n_jobs=None, penalty='l2', random_state=0, solver='warn', tol=0.0001, verbose=0, warm_start=False) STEP-4: PREDICTION OF TEST SET RESULT #Predicting the test set result y_pred= classifier.predict(x_test) STEP-5: TEST ACCURACY OF THE RESULT #Creating the Confusion matrix from sklearn.metrics import confusion_matrix cm= confusion_matrix() STEP-6: VISUALIZING THE TRAINING SET RESULTS #Visualizing the training set result from matplotlib.colors import ListedColormap x_set, y_set = x_train, y_train x1, x2 = nm.meshgrid(nm.arange(start = x_set[:, 0].min() - 1, stop = x_set[:, 0].max() + 1, step =0.01), nm.arange(start = x_set[:, 1].min() - 1, stop = x_set[:, 1].max() + 1, step = 0.01)) mtp.contourf(x1, x2, classifier.predict(nm.array([x1.ravel(), x2.ravel()]).T).reshape(x1.sh ape), alpha = 0.75, cmap = ListedColormap(('purple','green' ))) mtp.xlim(x1.min(), x1.max()) mtp.ylim(x2.min(), x2.max())