본문 바로가기
AI

회귀분석 연습 프로그램

by 자동차생각_모듈러설계 2021. 9. 18.
728x90

from google.colab import drive

drive.mount('/content/gdrive')

import tensorflow as tf

import numpy as np

import matplotlib.pyplot as plt

import keras

from keras.models import Sequential

from keras.layers.core import Dense

#from keras.optimizers import RMSprop

from tensorflow.keras.optimizers import RMSprop

np.random.seed(7)

print('tensorflow version: ', tf.__version__)

print('keras version: ', keras.__version__)

raw_data = np.genfromtxt('/content/gdrive/My Drive/data/data1.txt', skip_header=36)

print(raw_data)

import matplotlib.pyplot as plt

 from mpl_toolkits.mplot3d import Axes3D

 %matplotlib inline

# 입력데이터를 잘라 넣기

xs = np.array(raw_data[:,2], dtype=np.float32)

ys = np.array(raw_data[:,3], dtype=np.float32)

zs = np.array(raw_data[:,4], dtype=np.float32)

# 입력 데이터를 그래프로 표현 

fig = plt.figure(figsize=(12,12))

ax = fig.add_subplot(111, projection='3d')

ax.scatter(xs, ys, zs)

ax.set_xlabel('Weight')

ax.set_ylabel('Age')

ax.set_zlabel('Blood fat')

ax.view_init(15, 15)

 

plt.show()

x_data = np.array(raw_data[:,2:4], dtype=np.float32)

y_data = np.array(raw_data[:,4], dtype=np.float32)

y_data = y_data.reshape((25,1))

rmsprop = RMSprop(lr=0.01)

model = Sequential()

model.add(Dense(1,input_shape=(2,)))

model.compile(loss='mse',optimizer=rmsprop)

model.summary()

hist = model.fit(x_data, y_data, epochs=1000)

print(hist.history.keys())

#loss 그래프를 그려 봅시다.

plt.plot(hist.history['loss'])

plt.title('model loss')

plt.ylabel('loss')

plt.xlabel('epoch')

plt.legend(['train''test'], loc='upper left')

plt.show()

print("95Kg 45 blood fat=",model.predict(np.array([9545]).reshape(1,2)))

print("65Kg 28 blood fat=",model.predict(np.array([6528]).reshape(1,2)))

W_, b_ = model.get_weights()

 

x = np.linspace(2010050).reshape(50,1)

y = np.linspace(107050).reshape(50,1)

 

X = np.concatenate((x,y), axis=1)

Z = np.matmul(X, W_) + b_

fig = plt.figure(figsize=(12,12))

ax = fig.add_subplot(111, projection='3d')

ax.scatter(x, y, Z)

ax.scatter(xs, ys, zs)

ax.set_xlabel('Weight')

ax.set_ylabel('Age')

ax.set_zlabel('Blood fat')

ax.view_init(1515)

 

plt.show()

'AI' 카테고리의 다른 글

딥러닝 개론. 시그모이드 함수와 zigzag 문제  (0) 2021.11.19
선행대수-인공지능을 위한- 스탠포드-강의록 등  (0) 2021.09.20
fmincon 연습  (0) 2021.09.19
minst 관련 연습 소프트웨어  (0) 2021.09.17
prolog 연습  (0) 2021.09.04