KEMBAR78
Python Scikit-Learn | PDF
2018/19 - 1Carlos J. Costa (ISEG)
SCIKIT-LEARN
Carlos J. Costa
2018/19 - 2Carlos J. Costa (ISEG)
scikit-learn
• A scikit-learn (anteriormente scikits.learn)
• É uma biblioteca de machine learning em
Open Source
• Inclui vários algoritmos de classificação,
regressão, clustering, redução dimensional,
seleção de modelos, pré-processamento
• https://scikit-learn.org/
• https://scikit-learn.org/stable/index.html
2018/19 - 3Carlos J. Costa (ISEG)
scikit-learn
• Dividir a amostra em treino e teste
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test =
train_test_split(X, Y, test_size=0.4,
random_state=50)
2018/19 - 4Carlos J. Costa (ISEG)
scikit-learn
• Vamos então fazer regressão…
• Importar modulo relevante:
from sklearn.linear_model import LinearRegression
• Criar modelo
lm = LinearRegression()
• Ajustar modelo
lm.fit(x_train,y_train)
2018/19 - 5Carlos J. Costa (ISEG)
scikit-learn
• coeficiente de determinação R^2
• lm.score(X, Y)
• Coeficientes
• lm.coef_
2018/19 - 6Carlos J. Costa (ISEG)
scikit-learn
• Vamos agora fazer predição
predicao = lm.predict(x_test)
Não confundir
com perdição!
2018/19 - 7Carlos J. Costa (ISEG)
scikit-learn
• A utilização de uma nova biblioteca agora
pode ser relevante a matplotlib (serve
para fazer gráficos)
import matplotlib.pyplot as plt
plt.scatter(y_test,predicao)
2018/19 - 8Carlos J. Costa (ISEG)
Bibliografia
• https://pandas.pydata.org/
• https://pandas.pydata.org/pandas-docs/sta
ble/getting_started/10min.html
• https://scikit-learn.org/
• https://scikit-learn.org/stable/index.html
• https://www.statsmodels.org/stable/index.h
tml

Python Scikit-Learn

  • 1.
    2018/19 - 1CarlosJ. Costa (ISEG) SCIKIT-LEARN Carlos J. Costa
  • 2.
    2018/19 - 2CarlosJ. Costa (ISEG) scikit-learn • A scikit-learn (anteriormente scikits.learn) • É uma biblioteca de machine learning em Open Source • Inclui vários algoritmos de classificação, regressão, clustering, redução dimensional, seleção de modelos, pré-processamento • https://scikit-learn.org/ • https://scikit-learn.org/stable/index.html
  • 3.
    2018/19 - 3CarlosJ. Costa (ISEG) scikit-learn • Dividir a amostra em treino e teste from sklearn.model_selection import train_test_split x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.4, random_state=50)
  • 4.
    2018/19 - 4CarlosJ. Costa (ISEG) scikit-learn • Vamos então fazer regressão… • Importar modulo relevante: from sklearn.linear_model import LinearRegression • Criar modelo lm = LinearRegression() • Ajustar modelo lm.fit(x_train,y_train)
  • 5.
    2018/19 - 5CarlosJ. Costa (ISEG) scikit-learn • coeficiente de determinação R^2 • lm.score(X, Y) • Coeficientes • lm.coef_
  • 6.
    2018/19 - 6CarlosJ. Costa (ISEG) scikit-learn • Vamos agora fazer predição predicao = lm.predict(x_test) Não confundir com perdição!
  • 7.
    2018/19 - 7CarlosJ. Costa (ISEG) scikit-learn • A utilização de uma nova biblioteca agora pode ser relevante a matplotlib (serve para fazer gráficos) import matplotlib.pyplot as plt plt.scatter(y_test,predicao)
  • 8.
    2018/19 - 8CarlosJ. Costa (ISEG) Bibliografia • https://pandas.pydata.org/ • https://pandas.pydata.org/pandas-docs/sta ble/getting_started/10min.html • https://scikit-learn.org/ • https://scikit-learn.org/stable/index.html • https://www.statsmodels.org/stable/index.h tml