Bird
Raised Fist0
ML Pythonml~20 mins

Loading datasets (CSV, built-in datasets) in ML Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Dataset Loading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of loading a CSV with pandas
What is the output of the following code snippet when loading a CSV file with pandas and printing the first 3 rows?
ML Python
import pandas as pd
from io import StringIO
csv_data = '''name,age,score
Alice,30,85
Bob,25,90
Charlie,35,88
David,40,92'''
df = pd.read_csv(StringIO(csv_data))
print(df.head(3))
A
    name  age  score
0   Alice   30     85
1     Bob   25     90
2  Charlie   35     88
B
88  53  eilrahC  2
09  52  boB  1
58  03  ecilA  0
erocs  ega  eman
C
   name  age  score
0  Alice  30  85
1  Bob  25  90
2  Charlie  35  88
D
   name  age  score
0  Alice  30  85
1  Bob  25  90
2  Charlie  35  88
3  David  40  92
Attempts:
2 left
💡 Hint
Remember that head(3) returns only the first 3 rows with default pandas formatting.
❓ Model Choice
intermediate
2:00remaining
Choosing dataset loading method for built-in datasets
You want to load the Iris dataset for a classification task using scikit-learn. Which code snippet correctly loads the dataset as a dictionary with data and target arrays?
A
import pandas as pd
data = pd.read_csv('iris.csv')
X, y = data.iloc[:, :-1], data.iloc[:, -1]
B
from sklearn.datasets import load_iris
data = load_iris()
X, y = data.data, data.target
C
from sklearn.datasets import fetch_iris
data = fetch_iris()
X, y = data.data, data.target
D
from sklearn.datasets import load_iris
data = load_iris(as_frame=True)
X, y = data.data, data.target
Attempts:
2 left
💡 Hint
Check the official scikit-learn function names for loading built-in datasets.
❓ Metrics
advanced
2:00remaining
Evaluating dataset loading time
You load a large CSV dataset using pandas with and without specifying dtypes. Which statement about loading time and memory usage is true?
ASpecifying dtypes reduces loading time but increases memory usage.
BSpecifying dtypes increases loading time but reduces memory usage.
CSpecifying dtypes has no effect on loading time or memory usage.
DSpecifying dtypes reduces loading time and memory usage because pandas can optimize data storage.
Attempts:
2 left
💡 Hint
Think about how pandas handles data types internally when reading CSVs.
🔧 Debug
advanced
2:00remaining
Identifying error when loading CSV with missing header
What error will the following code raise when loading a CSV file without a header row using pandas without specifying header=null?
ML Python
import pandas as pd
from io import StringIO
csv_data = '''Alice,30,85
Bob,25,90
Charlie,35,88'''
df = pd.read_csv(StringIO(csv_data))
print(df.head())
ANo error; pandas treats first row as header and data is loaded with column names 'Alice', '30', '85'
BTypeError because data types cannot be inferred
CValueError because of mismatched columns
DParserError due to missing header row
Attempts:
2 left
💡 Hint
By default, pandas treats the first row as header unless told otherwise.
🧠 Conceptual
expert
2:00remaining
Understanding dataset loading with train_test_split
You load a built-in dataset and split it into training and testing sets using scikit-learn's train_test_split. Which statement is true about the resulting datasets?
Atrain_test_split randomly shuffles data before splitting, so training and testing sets are representative samples.
Btrain_test_split splits data sequentially without shuffling, so testing set contains last samples only.
Ctrain_test_split always splits data into equal halves regardless of test_size parameter.
Dtrain_test_split requires the dataset to be pre-shuffled manually before splitting.
Attempts:
2 left
💡 Hint
Check the default behavior of train_test_split regarding shuffling.

Practice

(1/5)
1. What is the main purpose of loading datasets in machine learning?
easy
A. To get data from files or built-in sources for training models
B. To save the model after training
C. To visualize the model's predictions
D. To delete unused data from memory

Solution

  1. Step 1: Understand dataset loading

    Loading datasets means bringing data into your program so you can work with it.
  2. Step 2: Identify the purpose in machine learning

    Data is needed to train and test machine learning models, so loading datasets is the first step.
  3. Final Answer:

    To get data from files or built-in sources for training models -> Option A
  4. Quick Check:

    Loading datasets = Getting data [OK]
Hint: Loading datasets means getting data ready for models [OK]
Common Mistakes:
  • Confusing loading data with saving models
  • Thinking loading means visualizing results
  • Mixing loading with deleting data
2. Which Python code correctly loads a CSV file named data.csv using pandas?
easy
A. import pandas as pd df = pd.csv_read('data.csv')
B. import pandas as pd df = pd.load_csv('data.csv')
C. import pandas as pd df = pd.read('data.csv')
D. import pandas as pd df = pd.read_csv('data.csv')

Solution

  1. Step 1: Recall pandas CSV loading syntax

    The correct function to load CSV files in pandas is read_csv().
  2. Step 2: Check each option's function name

    Only import pandas as pd df = pd.read_csv('data.csv') uses pd.read_csv('data.csv'), which is correct syntax.
  3. Final Answer:

    import pandas as pd\ndf = pd.read_csv('data.csv') -> Option D
  4. Quick Check:

    pandas.read_csv() loads CSV files [OK]
Hint: Use pandas.read_csv() to load CSV files [OK]
Common Mistakes:
  • Using wrong function names like load_csv or csv_read
  • Forgetting to import pandas
  • Using read() instead of read_csv()
3. What will be the output type of the following code?
from sklearn.datasets import load_iris
iris = load_iris()
print(type(iris.data))
medium
A. <class 'numpy.ndarray'>
B. <class 'list'>
C. <class 'pandas.DataFrame'>
D. <class 'dict'>

Solution

  1. Step 1: Understand sklearn's load_iris output

    The load_iris() function returns a Bunch object with data as a NumPy array.
  2. Step 2: Check the type of iris.data

    Printing type(iris.data) shows it is a numpy.ndarray.
  3. Final Answer:

    <class 'numpy.ndarray'> -> Option A
  4. Quick Check:

    sklearn data attribute = numpy.ndarray [OK]
Hint: Sklearn datasets return numpy arrays for data [OK]
Common Mistakes:
  • Thinking iris.data is a list or dict
  • Confusing with pandas DataFrame
  • Not knowing sklearn returns numpy arrays
4. Identify the error in this code snippet that tries to load a CSV file:
import pandas as pd
df = pd.read_csv('data.csv', delimiter=';')
print(df.head())

Assuming the CSV uses commas, what is wrong?
medium
A. Missing import statement for csv module
B. The delimiter should be ',' not ';'
C. The function read_csv does not exist
D. print(df.head()) is incorrect syntax

Solution

  1. Step 1: Check delimiter parameter usage

    The code uses delimiter=';' but the CSV file uses commas, so this is wrong.
  2. Step 2: Understand correct delimiter for comma CSV

    For comma-separated files, the delimiter should be ',' or omitted (default).
  3. Final Answer:

    The delimiter should be ',' not ';' -> Option B
  4. Quick Check:

    Delimiter must match CSV separator [OK]
Hint: Match delimiter to CSV separator character [OK]
Common Mistakes:
  • Using wrong delimiter for CSV files
  • Thinking read_csv is missing
  • Confusing print syntax errors
5. You want to load the built-in digits dataset from sklearn and convert it to a pandas DataFrame with feature columns named pixel_0 to pixel_63. Which code correctly does this?
hard
A. import pandas as pd df = pd.read_csv('digits.csv') df.columns = [f'pixel_{i}' for i in range(64)]
B. from sklearn.datasets import load_digits import pandas as pd digits = load_digits() df = pd.DataFrame(digits, columns=[f'pixel_{i}' for i in range(64)])
C. from sklearn.datasets import load_digits import pandas as pd digits = load_digits() df = pd.DataFrame(digits.data, columns=[f'pixel_{i}' for i in range(64)])
D. from sklearn.datasets import load_digits import pandas as pd digits = load_digits() df = pd.DataFrame(digits.target, columns=[f'pixel_{i}' for i in range(64)])

Solution

  1. Step 1: Load digits dataset correctly

    Use load_digits() to get the dataset object with data attribute for features.
  2. Step 2: Create DataFrame with correct data and columns

    Pass digits.data to pd.DataFrame() and set columns to pixel_0 to pixel_63.
  3. Final Answer:

    from sklearn.datasets import load_digits\nimport pandas as pd\ndigits = load_digits()\ndf = pd.DataFrame(digits.data, columns=[f'pixel_{i}' for i in range(64)]) -> Option C
  4. Quick Check:

    Use digits.data for features in DataFrame [OK]
Hint: Use digits.data for features, not digits or digits.target [OK]
Common Mistakes:
  • Passing digits object directly to DataFrame
  • Using digits.target as data
  • Trying to load digits from CSV without file
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard