-
Notifications
You must be signed in to change notification settings - Fork 0
/
multilinear.html
175 lines (151 loc) · 7.72 KB
/
multilinear.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="{{ url_for('static', filename='vendor/bootstrap/css/bootstrap.min.css') }}" rel="stylesheet">
<link href="{{ url_for('static', filename='vendor/font-awesome/css/font-awesome.min.css') }}" rel="stylesheet"
type="text/css">
<link href='https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet'
type='text/css'>
<link
href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800'
rel='stylesheet' type='text/css'>
<!-- Custom styles for this template -->
<link href="{{ url_for('static', filename='css/clean-blog.min.css') }}" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css" />
<link href="https://api.mapbox.com/mapbox-gl-js/v2.1.1/mapbox-gl.css" rel="stylesheet" />
<link rel="stylesheet" href="style.css" />
<title> Linear Regression</title>
</head>
<body>
<!-- Navbar -->
<nav class="navbar navbar-expand-lg bg-dark navbar-dark py-10 fixed-top">
<div class="container">
<a href="/" class="navbar-brand">Machine learning Bootcamp</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navmenu">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navmenu">
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<a href="/ml" class="nav-link">Machine Learning</a>
</li>
<li class="nav-item">
<a href="/contact" class="nav-link">Contact Us</a>
</li>
</ul>
</div>
</div>
</nav>
<br> <br> <br>
<div style="text-align:center;" class="login">
<h1 class="header centre"> Linear Regression </h1>
</div>
<p> In previous chapter you had learned about linear regression in 1 variable Now you will learn about multiple
regression</p>
<p> In multiple regression we have many features </p> <br>
<div style="text-align:center;" class="login">
<h3 class="header centre"> y = A <sub>0</sub>*area + A <sub>1</sub>*bedroom + A <sub>2</sub>*age + A
<sub>3</sub>*bathroom + C</h3>
</div> <br>
<ul>
<li> <strong>Coeffcients = </strong> A <sub>0</sub> , A <sub>1</sub>, A <sub>2</sub>, A <sub>3</sub></li>
<li> <strong>Features(Independent variables) = </strong> area, bedrooms, age and bathroom </li>
<li> <strong> Dependent features = </strong> y </li>
<li> <strong> Intercept </strong> = C </li>
</ul>
<section id="learn" class="p-5 bg-dark text-light">
<h2> <span class="text-warning">#Train model for house prediction using multi regression </span> </h2>
<p class="lead">
<pre>
<code>
<strong>#importing modules </strong> <br>
import numpy as np <br>
import matplotlib.pyplot as plt <br>
import pandas as pd <br>
import matplotlib.pyplot as plt<br>
import seaborn as sns <br>
<strong># load the data </strong> <br>
df = pd.read_excel('Book 7.xlsx') <strong> #if you have csv file so use
pd.read_csv('book7.csv') it is excel file so i use read_excel </strong> <br>
df.head() <strong> # It prints first few lines of datasets </strong> <br> <br>
output area bedroom age bathroom price <br>
0 2500 2.0 10 1 40000 <br>
1 3700 4.0 5 3 80000<br>
2 3000 2.0 16 2 48000<br>
3 2900 NaN 2 1 39000<br>
4 5000 6.0 1 2 120000<br>
<br>
<br>
df.columns <strong>#check your columns</strong> <br>
output Index(['area', 'bedroom', 'age', 'bathroom', 'price'], dtype='object')<br>
<br>
df.shape <strong>#check how many rows and columns they are very very small datasets in later
we work on big datasets more than thousand rows and column</strong> <br>
output (14, 5)<br> <br>
<strong>
# some values in bedroom are nan/missing so we are not use this in your model so how to use that<br>
<h4># first method drop that columns using dropna method </h4> <br>
#df1 = df.dropna() #they drop that columns which have nan values but it's not good
method because sometimes they drop valuable columns means they have much information <br>
#df1.head() <br> <br>
</strong>
<h4>#second method fill the drop values which the help of fillna method</h4> <br>
df.bedroom = df.bedroom.fillna(df.bedroom.median()) <strong>#we fill the nan value with median value you can use mean and mode</strong> <br>
df.head() <br>
<br>
output area bedroom age bathroom price <br>
0 2500 2.0 10 1 40000 <br>
1 3700 4.0 5 3 80000 <br>
2 3000 2.0 16 2 48000<br>
3 2900 5.0 2 1 39000<br>
4 5000 6.0 1 2 120000<br>
<br>
x = df[['area','bedroom','age','bathroom']] <strong> <strong> </strong> #features</strong><br>
y=df['price'] <strong> #Label </strong><br> <br>
from sklearn.model_selection import train_test_split<br>
x_train, x_test, y_train, y_test = train_test_split(x,y, test_size=0.2, train_size=0.8, random_state=0)<br>
<br> <br>
<strong>#train the model</strong><br>
from sklearn.linear_model import LinearRegression<br>
my_model = LinearRegression()<br>
my_model.fit(x_train, y_train)<br>
my_model.predict(x_test)<br> <br>
output array([ 79668.84424049, 44032.37002709, 117310.25777372])<br>
</p>
<h4> #predict price for 4000sqft area and 3bedrooms and 5 year old and 1 bathroom</h4>
my_model.predict([[4000,3,5,1]]) <br>
output array([86018.62999155])
</section>
<h4 style="margin-left: 20px;" > You can also calculated it's coffecient and intercept</h4>
<p style="margin-left: 20px;">my_model.coef_</p>
<p style="margin-left: 20px;">output array([ 32.69220009, -2671.1041587 , -1327.72283984, 1301.84881334])</p>
<p style="margin-left: 20px;">my_model.intercept_</p>
<p style="margin-left: 20px;">output -31400.092488786802</p>
</code>
</pre>
<h5 style="margin-left: 20px;">I hope you understand linear and multiple regression </h5>
<!-- pagination -->
<nav aria-label="Page navigation example" my=100>
<ul class="pagination justify-content-center" center="right">
<li class="page-item"><a class="page-link bg-dark text-light" href="/gradient">Next</a></li>
</ul>
</nav>
<!-- Footer -->
<footer class="p-5 bg-dark text-white text-center position-relative">
<div class="container">
<p class="lead">Copyright © 2021 Machine learning Bootcamp</p>
<a href="#" class="position-absolute bottom-0 end-0 p-5">
<i class="bi bi-arrow-up-circle h1"></i>
</a>
</div>
</footer>
</body>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4"
crossorigin="anonymous"></script>
</html>