4. Write a python code to read (advertisement.csv) csv file to
generate a plot with actual data (Red dots) and predictive data(
blue line) with the help of linear regression and find out the
Money spent on particular item as well sales of it.
4
LINEAR REGRESSION - EXAMPLE
6. import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
import statsmodels.api as sm
data = pd.read_csv("advertisement.csv")
data.head()
6
LINEAR REGRESSION - EXAMPLE
7. 7
sklearn.metrics.r2_score(y_true, y_pred, *, sample_weight=None, multioutput='uniform_averag
• R^2 (coefficient of determination) regression score function.
• Best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse).
• A constant model that always predicts the expected value of y,
• disregarding the input features, would get a R^2 score of 0.0.
LINEAR REGRESSION - EXAMPLE
12. SIGMOID FUNCTION
import math
def sigmoid(x):
a = []
for item in x:
a.append(1/(1+math.exp(-item)))
return a
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-10, 10, 0.2)
sig = sigmoid(x)
plt.plot(x,sig)
plt.show()
12
13. LOGISTIC REGRESSION-EXAMPLE
• Write down a python code for
Logistic Regression where needs
to read csv file(banking.csv)
• Generate a bar graph after read
the predictive variable Y
• Calculate the percentage of
Subscription / No Subscription.
13
14. import pandas as pd
import numpy as np
from sklearn import preprocessing
import matplotlib.pyplot as plt
plt.rc("font", size=14)
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
import seaborn as sns
sns.set(style="white")
sns.set(style="whitegrid", color_codes=True)
14
LOGISTIC REGRESSION-EXAMPLE
17. DATA FRAME DROPNA () FUNCTION 17
DataFrame.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False)
axis{0 or ‘index’, 1 or ‘columns’}, default 0
•0, or ‘index’ : Drop rows which contain missing values.
•1, or ‘columns’ : Drop columns which contain missing
value.
how{‘any’, ‘all’}, default ‘any’
•any’ : If any NA values are present, drop that row or
column.
•‘all’ : If all values are NA, drop that row or column.
Thresh : int, optional
Require that many non-NA values
Subset : arra-like, optional
Labels along other axis to consider, e.g. if you are
dropping rows these would be a list of columns to
include.
Inplace : bool, default False
If True, do operation inplace and return None.