Data Preprocessing and
Regressions(Machine Learning)
test questions and answers
2024
Numpy package
Allows us to work with arrays
matplotlib package
Allows to make charts and graphs
pandas package
imports and manipulates data
How do you import a csv dataset file
pd.read_csv(~~~~~~~~~~~.csv)
How do you split a dataset up into different variables, with specific rows
and columns
x=dataset.iloc[rows,columns].values
y=dataset.iloc[rows,columns].values
What does iloc do?
locates the columns/rows by its index
What does SimpleInputer from sklearn do?
Find missing values of a variable, and assign it to the mean of the other
values of that variable
What is the code to replace missing values of a dataset from columns 1
and 2 using imputer?
from sklearn.impute import SimpleInputer
imputer=SimpleInputer(missing_values=np.nan,strategy='mean')
imputer.fit(x.iloc[:,1:3]
x.iloc[:,1:3]=imputer.transform(x.iloc[:,1:3])
Why should you never assign 1,2,3,.... to a categorical variable whose
values range is not a boolean?
, The machine might think that there is a pattern. The alternative way is to
use onehotencoding
When encoding categorical data, when should you use np.array and
ColumnTransformer package from sklearn?
When your variable has more than one category ***You never use this for
LabelEncoder
What are all of the sklearn packages to use when doing categorical data
encoding?
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import LabelEncoder
How would you use ColumnTransformer to encode the 1st index of variable
x using OneHotEncoder?
ct=ColumnTransformer(transformers=[('encoding', OneHotEncoding,[0])],
remainder='passthrough')
x=np.array(ct.fit_transform(x))
How would you use ColumnTransformer to encode the 1st index of variable
y using LabelEncoder?
ct=ColumnTransformer(transformers=[('encoding', LabelEncoder(), [0])],
remainder='passthrough')
y=np.array(ct.fit_transform(y))
Why is the idea of training and testing data and why do we do it?
It is a method to measure the accuracy of your model. The dataset is split
randomely from a 80:20 ratio. The trained data is the 80 and that is what
the machine learns. The test is what the machine tests based on what is
learned with the trained data. If the test and trained are similar, that
means the fit of the model is good, otherwise it is not
How do you use sklearn to declare trained and test for 2 variables using
train_test_split?
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test= train_test_split(x,y,test_size=0.2,
random_state=#)
Should you do feature scaling before or after train/test?
After doing train/test
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller BRAINBOOSTERS. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $13.49. You're not tied to anything after your purchase.