-
Notifications
You must be signed in to change notification settings - Fork 0
/
10week_Task.py
89 lines (77 loc) · 2.03 KB
/
10week_Task.py
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
import pymongo
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn import linear_model
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["BigDataAV"]
mycol = mydb["usedcars"]
# 2. Perform linear regression for two attributes: YEAR and MILEAGE
# x_train = []
# y_train = []
# regr = linear_model.LinearRegression()
#
# for item in mycol.find():
# x_train.append(item["year"])
# y_train.append(item["mileage"])
#
# X = np.asarray(x_train)
# Y = np.asarray(y_train)
# regr.fit(X.reshape(-1, 1), Y)
#
# plt.xlabel('year')
# plt.ylabel('mileage')
# plt.plot(X, Y, 'o')
# plt.plot(X, regr.predict(X.reshape(-1, 1)), color='r')
# plt.show()
# 3. Predict car price when car year is 2010 and price is 10000
x_train = []
y_train = []
regr = linear_model.LinearRegression()
for item in mycol.find():
x_train.append([item["year"], item["mileage"]])
y_train.append(item["price"])
regr.fit(x_train, y_train)
unknown_points = [[2010, 15000]]
guesses = regr.predict(unknown_points)
print(guesses)
# 4. Draw boxplot for YEAR and MILEAGE
# x_train = []
# y_train = []
#
# for item in mycol.find():
# x_train.append(item["year"])
# y_train.append(item["mileage"])
# plt.boxplot(x_train)
# plt.show()
# plt.boxplot(y_train)
# plt.show()
# remove outlier
# def find_upper_lower_bound(data_list):
# temp = sorted(data_list)
# q1, q3 = np.percentile(temp, [25, 75])
# iqr = q3 - q1
# lower_bound = q1 - (1.5 * iqr)
# upper_bound = q3 + (1.5 * iqr)
# return lower_bound, upper_bound
# YEAR
# lower, upper = find_upper_lower_bound(x_train)
# print('YEAR lower bound, upper bound : ', lower, upper)
#
# for val in x_train:
# if val < lower or val > upper:
# x_train.remove(val)
#
# plt.boxplot(x_train)
# plt.show()
#
# # MILEAGE
# lower, upper = find_upper_lower_bound(y_train)
# print('MILEAGE lower bound, upper bound : ', lower, upper)
#
# for val in y_train:
# if val < lower or val > upper:
# y_train.remove(val)
#
# plt.boxplot(y_train)
# plt.show()