Essential Pandas & Sklearn Commands
Essential Pandas & Sklearn Commands
Missing data can be handled using SimpleImputer by first defining the missing value and strategy, such as the mean, median, or most frequent value. The steps include: defining an instance of SimpleImputer with the chosen strategy, selecting the columns that contain missing values by using the fit method, and finally, filling the missing values by transforming the data using the transform method .
Separating features and response variables is crucial for predictive modeling as it clearly distinguishes between inputs (independent variables) and outputs (dependent variable). In Python, this is achieved using libraries like pandas, where features and response are separated into two objects, often using iloc to slice the dataset. For example, features are stored as x = df.iloc[:,:-1].values and response as y = df.iloc[:,-1].values .
Feature scaling adjusts the range of features to a standard scale without distorting differences in the ranges of values. This is necessary as many machine learning algorithms perform better or converge faster with features on a relatively similar scale. Techniques include MinMaxScaler and StandardScaler from Scikit-learn. MinMaxScaler scales features to a predefined range, typically 0 to 1, while StandardScaler standardizes features by removing the mean and scaling to unit variance .
One Hot Encoding can be applied to categorical data using Scikit-learn's ColumnTransformer and OneHotEncoder by first specifying the columns to transform and then fitting the transformer to the data. This process is necessary because machine learning models require numerical input, and One Hot Encoding converts categorical variables into a format suitable for the model (binary vectors).
Skewness can impact model accuracy by distorting statistical assumptions like normality, which many algorithms rely on. Skewed distributions often lead to poorly estimated model parameters. In Python, skewness can be handled by applying transformations such as the square root or log to make the data distribution more symmetrical before fitting models, while ensuring no negative values are transformed to avoid NaNs .
Removing skewness from columns highly correlated with the target variable can inadvertently weaken or alter these correlations, which are crucial for predictive power. High correlation with the target often signifies that skewness is not arbitrary but potentially significant. Therefore, maintaining these distributions can enhance the model's ability to discover the true relationships in the data .
Handling data skewness is essential because skewed data can lead to misrepresentations in model training and prediction. Skewness should not be removed from columns with high correlation with the target variable, as it can alter these correlations. To address skewness, one can use transformations like the square root or log transformation on the skewed column .
EDA steps with pandas include checking for missing values using df.isna(), understanding data types and data summary with df.info() and df.describe(), handling missing data with df.dropna() and df.fillna(), and exploring value counts and unique values using df["column_name"].value_counts() and df["column_name"].unique(). EDA is vital because it helps in understanding the structure, quality, and insights of the data, which informs the direction of subsequent modeling .
Feature engineering involves creating new features from existing data to improve a model's performance. It reduces the number of features and can capture more relevant patterns. For example, if a dataset has height and width columns, a new column named area can be created by calculating the product of height and width, and then the original columns can be removed .
To create a predictive model using Scikit-learn's Linear Regression, the steps are: first, select and import Linear Regression from Scikit-learn. Next, prepare the data by splitting into x (features) and y (target), followed by train-test splitting using train_test_split. Scale the features if necessary. Subsequently, create an instance of the LinearRegression model and fit it to the training data using linreg.fit(xtrain, ytrain). Finally, predict on the test data using ypred = linreg.predict(xtest).

![
Steps to handle missing values :
#step1 - use replace
df['column_name'].replace("string",np.nan,inplace =True)
#step2 -](/p?url=https%3A%2F%2Fscreenshots.scribd.com%2FScribd%2F252_100_85%2F356%2F517355391%2F2.jpeg&__src=https%3A%2F%2Fwww.scribd.com%2Fdocument%2F517355391%2FMachine-Learning-Notes&__type=image)
![Skewness and handling Skewness :
from scipy.stats import skew
To find skewness of a column :
skew(df_num['column_name'])
Usi](/p?url=https%3A%2F%2Fscreenshots.scribd.com%2FScribd%2F252_100_85%2F356%2F517355391%2F3.jpeg&__src=https%3A%2F%2Fwww.scribd.com%2Fdocument%2F517355391%2FMachine-Learning-Notes&__type=image)
![from sklearn.preprocessing import StandardScaler
for col in df_new:
sc = StandardScaler()
df_new[col]=sc.fit_transform(](/p?url=https%3A%2F%2Fscreenshots.scribd.com%2FScribd%2F252_100_85%2F356%2F517355391%2F4.jpeg&__src=https%3A%2F%2Fwww.scribd.com%2Fdocument%2F517355391%2FMachine-Learning-Notes&__type=image)
