<jupyter_start><jupyter_text>**Turma de Pós-Graduação de Ciência de Dados****Disciplina: Linguagem de Programação Python****prof: <NAME>, DSc** **Aula 05** **Regressão Linear**<jupyter_code>import matplotlib.pyplot as plt
from scipy import stats

def gerar_dados_l01():
  x = [5,7,8,7,2,17,2,9,4,11,12,9,6]
  y = [99,86,87,88,111,86,103,87,94,78,77,85,86]
  return x, y

def f_regressao_linear(x, y):  #f(x) = coef_angular * x + coef_linear
  coef_angular, coef_linear, r, p, std_err = stats.linregress(x, y)
  return coef_angular, coef_linear

def funcao_linear(x, coeficiente_angular, coeficiente_linear): 
  f = coeficiente_angular * x + coeficiente_linear
  return f

x, y = gerar_dados_l01()

coef_angular, coef_linear = f_regressao_linear(x, y)

regressao_linear = []
for w in x:
  f = funcao_linear(w, coef_angular, coef_linear)
  regressao_linear.append(f)

plt.scatter(x, y, color = 'red')
plt.plot(x, regressao_linear, color = 'blue')
plt.show()
#print(regressao_linear)

f = funcao_linear(9, coef_angular, coef_linear)
print('f(9)={}'.format(f))<jupyter_output><empty_output><jupyter_text>**Regressão Polinomial**<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import r2_score

def gerar_dados_p01():
  x = [1,2,3,5,6,7,8,9,10,12,13,14,15,16,18,19,21,22] #data
  y = [100,90,80,60,60,55,60,65,70,70,75,76,78,79,90,99,99,100]
  return x, y

def f_regressao_polinomial(x, y):
  modelo = np.poly1d(np.polyfit(x, y, 3))
  return modelo

x, y = gerar_dados_p01()

modelo = f_regressao_polinomial(x, y)

linha = np.linspace(1, 22, 100)

plt.scatter(x, y, color='red')
plt.plot(x, y, color='green')
plt.plot(linha, modelo(linha), color='blue')
plt.show()
erro=r2_score(y, modelo(x))
print('Erro de aproximação: {}'.format(erro))
print(modelo(22))
print(modelo(23))
print(modelo(24))
print(modelo(25))<jupyter_output>99.24127229005046
98.09548850249544
95.4520871661491
91.12914300383954<jupyter_text>**Árvore de Decisão** **exemplo 01**<jupyter_code>import pydotplus
from sklearn.datasets import load_iris
from sklearn import tree
import collections

# Dados
X = [ [180, 15, 0],     
      [177, 42, 0],
      [136, 35, 1],
      [174, 65, 0],
      [141, 28, 1]]

Y = ['homem', 
     'mulher', 
     'mulher', 
     'homem', 
     'mulher']    

nomes_das_caracteristicas = ['altura', 
                             'comprimento do cabelo', 
                             'tom de voz' ]
# Training
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X,Y)
# Visualize data
dot_data = tree.export_graphviz(clf,
                                feature_names=nomes_das_caracteristicas,
                                out_file=None,
                                filled=True,
                                rounded=True)

grafico = graphviz.Source(dot_data)  
grafico
#Teste da árvore
X_test = [[180, 15, 0]]
y_pred = clf.predict(X_test)
y_pred
#Teste da árvore
X_test = [[180, 30, 1]]
y_pred = clf.predict(X_test)
y_pred<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1445299431247909, 'fraction_numerical': 0.08263633322181331, 'mean_word_length': 3.9750415973377704, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '503610', 'license': 'MIT', 'n_tokens_mistral': 1419, 'n_tokens_neox': 1257, 'n_words': 238, 'path': 'Aula_05_Pos_Ciencia_De_Dados.ipynb', 'repo_name': 'rodrigosilvaluz/python_aulas_2022.1', 'size': '3007'}