forked from sookie233/Food-security-risk-index
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinear_regression.py
55 lines (40 loc) · 1.67 KB
/
linear_regression.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
'''
Created on Mar 26, 2017
@author: Cheng-lin Li
'''
import sys
import numpy as np
import math
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn.linear_model import LinearRegression
INPUT_FILE = 'classification.txt' #Default input file name
ORIG_STDOUT = None
#OUTPUT_FILE = 'output.txt' # OUTPUT_FILE COULD BE 'OUTPUT_FILE = None' for console or file name (e.g. 'OUTPUT_FILE = 'output.txt') for file.'
OUTPUT_FILE = None # OUTPUT_FILE COULD BE 'OUTPUT_FILE = None' for console or file name (e.g. 'OUTPUT_FILE = 'output.txt') for file.'
def getInputData(filename):
_data = np.genfromtxt(filename, delimiter = ',')
_X = _data[1:, 1:12] # variable numbers are 11
_Y = _data[1:, 12] # column for label data
return _X, _Y
if __name__ == '__main__':
input_file = ''
output_file = ''
if len(sys.argv) < 2 :
print('Usage of Linear Regression: %s input_matrix.dat output.txt '%(sys.argv[0]))
print(' input_matrix is the input variable matrix.')
print(' output.txt will output weights for each dimensions')
else:
input_file = sys.argv[1] if len(sys.argv) > 1 else INPUT_FILE
output_file = sys.argv[2] if len(sys.argv) > 2 else OUTPUT_FILE
X, Y = getInputData("summarize-new.csv") #Get column 1,2 as X, column 3 as Z
lr = LinearRegression(normalize=True)
_X = list(X)
lr.fit(X, Y)
print(str(lr.get_params))
score = lr.score(X, Y)
W = lr.coef_
print('score =', score)
print ('W=', W )
New_Y = lr.predict(_X)
print('Prediction:%s'%(New_Y))