<jupyter_start><jupyter_text>GB<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
df = pd.read_csv(r'C:\Users\rodri\GitHub\My_Projects\1 Aulas Data Science\4 - Machine Learning\Regressao Linear/autompg-dataset.zip')
df.head()
df = df[df.horsepower != '?']
df.drop('car name', axis=1, inplace=True)
from sklearn.model_selection import train_test_split
X = df.drop(['mpg'], axis=1)
y = df['mpg']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Import RandomForestRegressor
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.ensemble import GradientBoostingClassifier
from sklearn import metrics

# Instantiate gb
gb = GradientBoostingRegressor(learning_rate=0.01,
                               n_estimators=500,
                               random_state=42)

# Instantiate sgbr
sgbr = GradientBoostingRegressor(max_depth=4, 
                                 subsample=0.9,
                                 max_features=0.75,
                                 learning_rate=0.01,
                                 n_estimators=500,                                
                                 random_state=42)
                           
# Fit gb to the training set            
gb.fit(X_train, y_train)                           

sgbr.fit(X_train, y_train) 
y_pred = gb.predict(X_test)
print('MAE:', metrics.mean_absolute_error(y_test, y_pred))
print('MSE:', metrics.mean_squared_error(y_test, y_pred))
print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
y_pred = sgbr.predict(X_test)
print('MAE:', metrics.mean_absolute_error(y_test, y_pred))
print('MSE:', metrics.mean_squared_error(y_test, y_pred))
print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
sgbr.feature_importances_
# Create a pd.Series of features importances
importances = pd.Series(data=sgbr.feature_importances_,
                        index= X_train.columns)

# Sort importances
importances_sorted = importances.sort_values()

# Draw a horizontal barplot of importances_sorted
importances_sorted.plot(kind='barh', color='lightgreen')
plt.title('Features Importances')
plt.show()
df = pd.read_csv(r'C:\Users\rodri\GitHub\My_Projects\1 Aulas Data Science\Data Sets/breast-cancer-wisconsin-data.zip')
df.head()
d={'M':1,'B':0}
df['diagnosis'] = df['diagnosis'].map(d)
df['diagnosis'].value_counts()
from sklearn.model_selection import train_test_split
features = df.drop(['id', 'diagnosis', 'Unnamed: 32'], axis=1)
targets = df['diagnosis']

X_train, X_test, y_train, y_test = train_test_split(features, targets, test_size=0.30, stratify=targets,random_state=1)
# Instantiate gb
gb = GradientBoostingClassifier(learning_rate=0.01,
                               n_estimators=350,
                               random_state=42)
                           
# Instantiate sgbr
sgbr = GradientBoostingClassifier(subsample=0.8,
                                 max_features=0.70,
                                 learning_rate=0.001,
                                 n_estimators=750,                                
                                 random_state=42)
                           
# Fit gb to the training set            
gb.fit(X_train, y_train)                           

sgbr.fit(X_train, y_train) 
# Import accuracy_score
from sklearn.metrics import accuracy_score

# Predict test set labels
y_pred = gb.predict(X_test)

# Compute test set accuracy  
acc = accuracy_score(y_test, y_pred)
print("Test set accuracy: {:.2f} %".format(acc*100))
# Predict test set labels
y_pred = sgbr.predict(X_test)

# Compute test set accuracy  
acc = accuracy_score(y_test, y_pred)
print("Test set accuracy: {:.2f} %".format(acc*100))
y_pred_proba = gb.predict_proba(X_test)
y_pred_proba
y_pred<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup