Frames

Untitled

0
1
2import sys
3import pandas as pd
4import numpy as np
5#import matplotlib as mpl
6#import matplotlib.pyplot as plt
7#from mpl_toolkits.mplot3d import Axes3D
8
9inputfile = "input2.csv"
10try:
11 inputfile = sys.argv[1]
12except:
13 inputfile = "input2.csv"
14outputfile = "output.csv"
15try:
16 outputfile = sys.argv[2]
17except:
18 outputfile = "output.csv"
19
20df = pd.read_csv(inputfile, header = None, names = ["age","weight","heigth"])
21
22def normalize(df):
23 result = df.copy()
24 for feature_name in df.columns:
25 max_value = df[feature_name].max()
26 min_value = df[feature_name].min()
27 result[feature_name] = (df[feature_name] - min_value) / (max_value - min_value)
28 return result
29def frange(start,end,step):
30 return [x*step for x in range(start,int(end/step))]
31def graddesc(df, features, value, alpha, size, startimage):
32 def summfxi(df,features,value,beta):
33 def fxi(betas,row):
34 return np.sum(betas*row[features])
35 values = [(fxi(beta,row)-row[value])*row[features] for (index,row) in df.iterrows()]
36 return np.sum(values, axis=0)
37 n = len(df)
38 overn = 1/n
39 betas = np.repeat(0, len(features))
40 for i in range(size):
41 betas = betas - alpha*overn*summfxi(df,features,value,betas)
42# fig = plt.figure()
43# ax = fig.add_subplot(111, projection='3d')
44# ax.scatter(df["age"], df["weight"], df["heigth"])
45# ax.set_xlabel('age')
46# ax.set_ylabel('weight')
47# ax.set_ylabel('heigth')
48# xx, yy = np.meshgrid(frange(0,1,0.1), frange(0,1,0.1))
49# z = (betas[1] * xx + betas[2] * yy + betas[0])
50# ax.plot_wireframe(xx, yy, z, alpha=0.2)
51# fig.savefig("figure%04d.png" % (i+startimage))
52 return betas
53
54df = normalize(df)
55df["_one"] = np.repeat(1, len(df))
56params = [(0.001,100), (0.005,100), (0.01,100), (0.05,100), (0.1,100), (0.5,100), (1,100), (5,100), (10,100),(1.25,300)]
57with open(outputfile,"w") as file:
58 i = 0
59 for (r,size) in params:
60 betas = graddesc(df,["_one","age","weight"],"heigth", r, size, i)
61 file.write("%f,%d,%f,%f,%f\n" % (r,100,betas[0],betas[1],betas[2]))
62 i = i + 100
63
64
65