リニアモデルpythonコード
1341 ワード
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 10 23:10:00 2017
Version:python3.5.1
@author: Stone
"""
import pandas as pd
from numpy.linalg import inv
from numpy import dot
#
# : Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width
# + + 。
# :Sepal.Length = Sepal.Width * x1 + Petal.Length * x2 + Petal.Width * x3 + b
iris = pd.read_csv('iris.csv') # iris.csv
temp = iris.iloc[:,2:5] # iris , [0,end), [2:5) # iloc ,
temp['x0'] = 1 # b w*=(w,b) ( 《 》P55 w* ‘^’ , ),temp 1 X0, 1, b w*。
X = temp.iloc[:,[0,1,2,3 ]] # X 0,1,2,3 ,150 。 Sepal.Width,Petal.Length,Petal.Width,x0
Y = iris.iloc[:,1] # :Sepal.Length , Sepal.Length。
Y = Y.values.reshape(len(iris), 1)
#values reshape() , 1 iris , [[a],[b],...]
# Y , 。
theta_n = dot(dot(inv(dot(X.T, X)), X.T), Y) # theta = (X'X)^(-1)X'Y dot ,inv() X.T X
print(theta_n) #
print(' Sepal.Width,Petal.Length,Petal.Width:')
x1,x2,x3 = map(float,input().split())
t = (0.65083716*x1 + 0.70913196*x2 - 0.55648266*x3 + 1.85599749 )
print(' :',t)
# , , 。
# x1,x2,x3,b 。