0% found this document useful (0 votes)
12 views2 pages

Data Analytics Competition Syntax Guide

This document provides a comprehensive guide for a data analytics competition using Python, detailing essential libraries and steps for data processing, including loading data, cleaning, encoding, and splitting into features and targets. It covers model training for classification and regression, evaluation metrics, cross-validation, grid search for hyperparameter tuning, and clustering techniques. Additionally, it includes instructions for saving the trained model.

Uploaded by

Akash
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Data Analytics Competition Syntax Guide

This document provides a comprehensive guide for a data analytics competition using Python, detailing essential libraries and steps for data processing, including loading data, cleaning, encoding, and splitting into features and targets. It covers model training for classification and regression, evaluation metrics, cross-validation, grid search for hyperparameter tuning, and clustering techniques. Additionally, it includes instructions for saving the trained model.

Uploaded by

Akash
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Data Analytics Competition - Complete Python

Syntax Guide

# ===== BASIC IMPORTS =====


import numpy as np
import pandas as pd
import [Link] as plt
import seaborn as sns

from sklearn.model_selection import train_test_split, cross_val_score, GridSearchCV


from [Link] import StandardScaler, MinMaxScaler, OneHotEncoder, LabelEncoder
from [Link] import ColumnTransformer
from [Link] import Pipeline
from [Link] import accuracy_score, classification_report, confusion_matrix
from [Link] import mean_squared_error, r2_score, f1_score
from sklearn.linear_model import LogisticRegression, LinearRegression, Ridge, Lasso
from [Link] import RandomForestClassifier, RandomForestRegressor
from [Link] import SVC
from [Link] import KMeans
from [Link] import SimpleImputer

# ===== LOAD DATA =====


df = pd.read_csv("[Link]")
[Link]()
[Link]()
[Link]()

# ===== DATA CLEANING =====


[Link]().sum()
[Link](inplace=True)
[Link]([Link](), inplace=True)

[Link]().sum()
df.drop_duplicates(inplace=True)

df["col"] = df["col"].astype(int)

# ===== OUTLIER DETECTION (IQR) =====


Q1 = df["col"].quantile(0.25)
Q3 = df["col"].quantile(0.75)
IQR = Q3 - Q1
lower = Q1 - 1.5 * IQR
upper = Q3 + 1.5 * IQR
df = df[(df["col"] >= lower) & (df["col"] <= upper)]

# ===== CORRELATION =====


corr = [Link]()
[Link](corr, annot=True)
[Link]()

# ===== ENCODING =====


le = LabelEncoder()
df["col"] = le.fit_transform(df["col"])

ct = ColumnTransformer(
transformers=[("encoder", OneHotEncoder(drop="first"), [0])],
remainder="passthrough"
)

# ===== FEATURE/TARGET SPLIT =====


X = [Link]("target", axis=1)
y = df["target"]

# ===== TRAIN TEST SPLIT =====


X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)

# ===== SCALING =====


scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = [Link](X_test)
# ===== CLASSIFICATION MODELS =====
model = LogisticRegression()
[Link](X_train, y_train)
y_pred = [Link](X_test)

rf = RandomForestClassifier()
[Link](X_train, y_train)

svc = SVC()
[Link](X_train, y_train)

# ===== REGRESSION MODELS =====


lin = LinearRegression()
[Link](X_train, y_train)

rfr = RandomForestRegressor()
[Link](X_train, y_train)

ridge = Ridge()
lasso = Lasso()

# ===== EVALUATION =====


accuracy_score(y_test, y_pred)
confusion_matrix(y_test, y_pred)
classification_report(y_test, y_pred)

mean_squared_error(y_test, y_pred)
r2_score(y_test, y_pred)

# ===== CROSS VALIDATION =====


scores = cross_val_score(model, X, y, cv=5)

# ===== GRID SEARCH =====


param_grid = {"n_estimators": [100, 200]}
grid = GridSearchCV(RandomForestClassifier(), param_grid, cv=5)
[Link](X_train, y_train)
grid.best_params_

# ===== CLUSTERING =====


kmeans = KMeans(n_clusters=3)
[Link](X)
labels = kmeans.labels_

# ===== SAVE MODEL =====


import joblib
[Link](model, "[Link]")

You might also like