본문 바로가기
파이썬

머신런닝 1일차 소스

by 리틀홍콩 2017. 2. 11.
728x90
반응형

 

 

[문제] 나이,성별,몸무게, 혈압 등 10개의 변수를 이용해 당뇨병 환자를 예측할 수 있는 Linear Regression Machine Learning Progrram을 만들어라.

 

 

환경

  파이썬 3.6

  Spyder Program Tool 사용

--------------------------------------------------------------------------------------------

 

from sklearn import datasets, linear_model
import numpy as np
import matplotlib.pyplot as plt

def loadData() :
    diabetes = datasets.load_diabetes()
    return diabetes

def doPreprocessing(dataset) :
    diabetes_X = dataset.data[:,np.newaxis,2]
    return diabetes_X

def prepareDataset(dataset,diabetes_X) :
    train_x = diabetes_X[:-20]
    test_x = diabetes_X[-20:]
    train_y = dataset.target[:-20]
    test_y = dataset.target[-20:]
    return train_x, train_y, test_x, test_y

def createLinearRegressionModel() :
    regr = linear_model.LinearRegression()
    return regr

def trainModel(model,train_x, train_y) :
    #학습
    model.fit(train_x, train_y)
   

def printTrainingResult(model,test_x,test_y) :
    print('Coefficients : \n', model.coef_)
    print("Residual sum of squares :%2.f" % np.mean((model.predict(test_x)-test_y)**2))
    print("Variance score:%.2f" % model.score(test_x,test_y))
   

def plotTrainningResult(model, test_x, test_y) :
    plt.scatter(test_x, test_y, color='black')
    plt.plot(test_x,model.predict(test_x), color='blue', linewidth=3)
    plt.xticks(())
    plt.yticks(())
    plt.show()


#trainModel(createLinearRegressionModel(), ))

g_dataset = loadData()

processed_dataset = doPreprocessing(g_dataset)
train_x,train_y,test_x,test_y = prepareDataset(loadData(),doPreprocessing(loadData()))
g_model = createLinearRegressionModel()

trainModel(g_model,train_x,train_y)
printTrainingResult(g_model,train_x,train_y)
plotTrainningResult(g_model,train_x,train_y)

print(len(g_dataset.data))

 

 

--------------------------------------------------------------------------------------------

[결과물]

 

반응형

'파이썬' 카테고리의 다른 글

머신런닝 1일차 교육자료  (0) 2017.02.11
파이썬-SFTP 모듈 설치  (0) 2016.12.13
파이썬 설치(아나콘다)  (0) 2016.12.13

댓글