-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomForestAnalysis.py
More file actions
57 lines (43 loc) · 1.89 KB
/
Copy pathRandomForestAnalysis.py
File metadata and controls
57 lines (43 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python
# coding: utf-8
import pandas as pd
def import_data_and_separate_features_from_labels(csv_file_path):
"""
Loads the data from a .csv file and returns
Parameters
----------
csv_file_path : string
The input is a .csv file path that is used to create a Pandas dataframe
Samples should be in rows and features in columns.
This function separates the data into feature names (e.g. gene names), features (gene expression values), and labels (sample classes).
The feature names are in a list, and both features and label are returned as numpy arrays of the values for feature values
and sample class.
Returns
---------
1) a N samples x p variables feature Pandas dataframe
2) a list with the sample class labels
3) a list with the feature names
Example of input .csv file
---------------------------
sample_id,sample_class,clump,uniformity_cell_size,uniformity_cell_shape,adhesion
sample_01,benign,5,1,1,1
sample_02,benign,5,4,4,5
sample_03,benign,3,1,1,1
sample_04,benign,6,8,8,1
sample_05,benign,4,1,1,3
sample_06,malign,8,10,10
...[many more rows]
"""
df = pd.read_csv(csv_file_path).dropna(how="all", axis=1)
# get a Numpy array of the feature names
col_names = list(df.columns)
feature_names = col_names[2:]
# get a Pandas dataframe of the feature values (e.g. gene expression values)
features = df.drop(columns=["sample_id","sample_class"], axis=1)
# get a Numpy array of the sample classes
labels = df.loc[:,"sample_class"]
return features, labels, feature_names
class RandomForestAnalysis:
def __init__(self, csv_file_path="data/breast-cancer.csv"):
self.csv_file_path = csv_file_path
self.features, self.labels, self.feature_names = import_data_and_separate_features_from_labels(self.csv_file_path)