Skip to content

Commit 32c1b02

Browse files
author
Algorithmica
committed
uploading class code
1 parent 25af31f commit 32c1b02

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

2018-jan/1.python/5.dataframes2.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pandas as pd
2+
3+
#create custom dataframe
4+
passengers = { 'passenger_id': [10,11,12], 'fare':[123.4,12.6,30] }
5+
df1 = pd.DataFrame(passengers)
6+
df1.shape
7+
df1.info()
8+
9+
print(df1['passenger_id'])
10+
print(df1['fare'])
11+
12+
print(df1.loc[2,'fare'])
13+
14+
#adding new column to existing data frame
15+
df1['pclass'] = 1

2018-jan/1.python/6.dictionary.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
parameters = { 'depth':10, 'leaf_nodes':10}
2+
print(type(parameters))
3+
4+
#access elements by key
5+
print(parameters['depth'])
6+
print(parameters['depth1'])
7+
print(parameters.get('depth'))
8+
print(parameters.get('depth1'))
9+
10+
a = None
11+
print(type(a))
12+
13+
#add new key-value pair
14+
parameters['depth1'] = 50
15+
parameters['a'] = 10
16+
parameters['a'] = 20
17+
18+
#enumerate over keys/values/items
19+
print(parameters.keys())
20+
print(parameters.values())
21+
print(parameters.items())
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import pandas as pd
2+
import os
3+
from sklearn import tree
4+
import pydot
5+
import io
6+
7+
print(os.getcwd())
8+
os.chdir('C:/Users/Algorithmica/Downloads')
9+
10+
titanic_train = pd.read_csv('titanic_train.csv')
11+
titanic_train.shape
12+
titanic_train.info()
13+
14+
#separeate features from target column
15+
features = ['Pclass']
16+
X_train = titanic_train[features]
17+
y_train = titanic_train[['Survived']]
18+
19+
#create an instance of machine learning class
20+
dt_estimator = tree.DecisionTreeClassifier()
21+
#build model by invoking fit method
22+
dt_estimator.fit(X_train, y_train)
23+
24+
#visualize the deciion tree
25+
dot_data = io.StringIO()
26+
tree.export_graphviz(dt_estimator, out_file = dot_data, feature_names = X_train.columns)
27+
graph = pydot.graph_from_dot_data(dot_data.getvalue())[0]
28+
graph.write_pdf("decision-tree.pdf")
29+
30+
titanic_test = pd.read_csv('titanic_test.csv')
31+
titanic_test.shape
32+
titanic_test.info()
33+
34+
X_test = titanic_test[features]
35+
titanic_test['Survived'] = dt_estimator.predict(X_test)

0 commit comments

Comments
 (0)