<jupyter_start><jupyter_text>Machine Learning with Data extracted from Pokemons  Location of data obtained¶- Niantic Pokemon https://www.pokemon.com/us/pokedex/- My Scrapper https://github.com/Renanrbsc/DatabasePokemon/tree/master/backend/scrapper- Text file https://github.com/Renanrbsc/DatabasePokemon/blob/master/backend/scrapper/data_save/pokemons.txt- Text file Pokemon types https://raw.githubusercontent.com/Renanrbsc/DatabasePokemon/master/backend/scrapper/data_save/types_pokemons.txt- Text file Pokemon Statushttps://raw.githubusercontent.com/Renanrbsc/DatabasePokemon/master/backend/scrapper/data_save/status_pokemons.txt  Imports<jupyter_code>import pandas as pd<jupyter_output><empty_output><jupyter_text>Loading csv file<jupyter_code>pokemons = pd.read_csv('datasets/pokemons.csv', sep=',', header=0)
pokemons.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 809 entries, 0 to 808
Data columns (total 18 columns):
 #   Column           Non-Null Count  Dtype  
---  ------           --------------  -----  
 0   id_pokedex       809 non-null    int64  
 1   type             809 non-null    object 
 2   type_two         405 non-null    object 
 3   name             809 non-null    object 
 4   height           809 non-null    float64
 5   weight           809 non-null    float64
 6   category         809 non-null    object 
 7   ability          807 non-null    object 
 8   ability_two      380 non-null    object 
 9   weakness         809 non-null    object 
 10  weakness_two     712 non-null    object 
 11  description      809 non-null    object 
 12  HP               809 non-null    int64  
 13  Attack           809 non-null    int64  
 14  Defense          809 non-null    int64  
 15  Special_Attack   809 non-null    int64  
 16  Special_Defense  809 non-null    int64  
 17  Speed           [...]<jupyter_text>Exploratory Analysis  We'll check columns in the df<jupyter_code>pokemons.shape
pokemons.index
pokemons.columns
pokemons.dtypes<jupyter_output><empty_output><jupyter_text>We'll see first and last record<jupyter_code>pokemons.head(1)
pokemons.tail(1)<jupyter_output><empty_output><jupyter_text>Let's select the id_pokemon column and set it as the index<jupyter_code>pokemons.set_index('id_pokedex', inplace=True)
pokemons.head()<jupyter_output><empty_output><jupyter_text>We'll look at the Pokémon type column now<jupyter_code># Column 'type'
# amount of types // total amount // number of nulls
pokemons['type'].nunique(), pokemons['type'].shape, pokemons['type'].isnull().sum()
# Column 'type_two'
# amount of types // total amount // number of nulls
pokemons['type_two'].nunique(), pokemons['type_two'].shape, pokemons['type_two'].isnull().sum()
type_one = pokemons['type'].value_counts()
type_one.head(18)
type_two = pokemons['type_two'].value_counts()
type_two.head(18)
total_types = type_one.sort_index() + type_two.sort_index()
total_types.sort_values(ascending=False).head(18)<jupyter_output><empty_output><jupyter_text>-------------------------- Search -------------------------------------------------------------------- Fast Search https://www.pokemon.com/br/pokedex/200<jupyter_code>pokemons[pokemons.index == 200]<jupyter_output><empty_output><jupyter_text>Fast Search https://www.pokemon.com/br/pokedex/750<jupyter_code>po = pokemons[pokemons == 2]
pokemons[pokemons.index == 750]
pokemons[pokemons.Defense >= 14]<jupyter_output><empty_output><jupyter_text>-------------------------- ML --------------------------------------------------------------------<jupyter_code># Preenchimento de Nulos nas colunas principais
pokemons['ability'].fillna('unskilled', inplace=True)
# Colunas para usar na função Dummies 0, 1
pokemons[['type', 'type_two', 'ability', 'ability_two', 'weakness', 'weakness_two']]
# separando dados da segunda coluna pra usar apenas uma colunas de predição
pokemons_one = pokemons.drop(columns=['type_two'])
pokemons_two = pokemons.drop(columns=['type'])
pokemons_two.rename(columns={'type_two':'type'}, inplace=True)
# juntando as duas colunas em 1 dataframe e removendo as linhas type nulas
pokemons_ml = pd.concat([pokemons_one, pokemons_two])
pokemons_ml.dropna(subset=['type'], inplace=True)
pokemons_ml
# Criação das variaveis Dummies e 
# preenchimento dos valores nulos posteriores
pokemon_weakness = pokemons_ml[['weakness', 'weakness_two']].copy()
dummies_weakness = pd.get_dummies(pokemon_weakness['weakness'])

pokemon_weakness.drop(columns='weakness', inplace=True)
pokemon_weakness.rename(columns={'weakness_two': 'weakness'}, inplace=True)

dummies_weakness = dummies_weakness + pd.get_dummies(pokemon_weakness['weakness'])

# dummies_weakness.rename(columns={'None': 'Normal'}, inplace=True)
# dummies_weakness['Normal'].fillna(0, inplace=True)
# dummies_weakness['Normal'] = dummies_weakness['Normal'].astype(int)

dummies_weakness
pokemon_skills = pokemons_ml[['ability', 'ability_two']].copy()
dummies_skills = pd.get_dummies(pokemon_skills['ability'])

pokemon_skills.drop(columns='ability', inplace=True)
pokemon_skills.rename(columns={'ability_two': 'ability'}, inplace=True)

dummies_skills_two = pd.get_dummies(pokemon_skills['ability'])

dummies_skills
pokemon_category = pokemons_ml['category'].copy()
dummies_category = pd.get_dummies(pokemon_category)

dummies_category
pokemon_all_dummies = pd.concat([dummies_weakness, dummies_skills_two, dummies_skills, dummies_category], axis=1)
pokemon_all_dummies
pokemons_ml = pd.concat([pokemons_ml[['type', 'height','weight','category','HP',
                                      'Attack','Defense','Special_Attack',
                                      'Special_Defense','Speed']], pokemon_all_dummies], axis=1)
pokemons_ml
pokemons_ml.drop(columns='category', inplace=True)
pokemons_ml
pokemons_ml[['HP', 'Attack', 'Defense', 'Special_Attack', 'Special_Defense', 'Speed']]

pokemons['type'].unique()
pokemons_ml['type'] = pokemons_ml['type'].map({'Grass':1, 'Fire':2, 'Water':3, 'Bug':4, 'Normal':5, 
                                               'Poison':6, 'Electric':7, 'Ground':8, 'Fairy':9, 
                                               'Fighting':10, 'Psychic':11, 'Rock':12, 'Ghost':13, 
                                               'Ice':14, 'Dragon':15, 'Dark':16, 'Steel':17, 'Flying':18})
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
# passando os valores de x e y como Dataframes
y = pokemons_ml['type']
pokemons_ml.drop(columns='type', inplace=True)
x = pokemons_ml
x.info()
# Divisão dos dados em 80% Treino e 20% Test de Predição
xtrain, xtest, ytrain, ytest = train_test_split(x, y,
                                                test_size=0.2, 
                                                random_state=42)
# criando e treinando o modelo
model = LinearRegression()
model.fit(xtrain, ytrain)
# para visualizar os coeficientes encontrados
# model.coef_
# para visualizar o R² TRAIN
model.score(xtrain, ytrain)

# para visualizar o R²
model.score(xtest, ytest)
# mostrando as previsões para o mesmo conjunto passado
pokemon_predict = model.predict(xtest)
# Resumindo o DataFrame da predição
pokemon_predict = pd.DataFrame(data=pokemon_predict)
pokemon_predict.index = xtest.index
pokemon_predict.rename(columns={0:'type_predict'}, inplace=True)
pokemon_predict['type_predict'] = pokemon_predict['type_predict'].astype(int)
pokemon_pred = pokemons['name'][xtest.index]
pokemon_pred = pd.concat([pokemon_pred, ytest, pokemon_predict], axis=1)
pokemon_pred
pokemon_pred['type_predict'].sort_values(ascending=False)
pokemon_pred.loc[pokemon_pred['type_predict'] < 0, 'type_predict'] = 0
pokemon_pred
# Corrigindo outlayers
pokemon_pred.loc[pokemon_pred['type_predict'] > 18, 'type_predict'] = 19
pokemon_pred.loc[pokemon_pred['type_predict'] < 0, 'type_predict'] = 0
import plotly.express as px
pokemon_compare_100
pokemon_pred['id'] = pokemon_pred.index.astype(str)
pokemon_compare_100 = pokemon_pred.iloc[:50, :]

fig = px.scatter(pokemon_compare_100,  y=pokemon_compare_100.columns[1:3], x=pokemon_compare_100.name,
              labels=dict(value="Tipo", name="Nome Pokemon"),
              title="Tipos de Pokemon Previstos")
fig.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14370893161353127, 'fraction_numerical': 0.02385008517887564, 'mean_word_length': 3.9691656590084645, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 7, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '467643', 'license': 'MIT', 'n_tokens_mistral': 3019, 'n_tokens_neox': 2852, 'n_words': 520, 'path': 'MachineLearning/DatabasePokemon/ml.ipynb', 'repo_name': 'Renanrbsc/DataScience', 'size': '8218'}