-
Notifications
You must be signed in to change notification settings - Fork 0
/
digit.py
69 lines (56 loc) · 1.38 KB
/
digit.py
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
58
59
60
61
62
63
64
65
66
67
68
69
#%%
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
import warnings
import time
import sys
import os
DeprecationWarning('ignore')
warnings.filterwarnings('ignore',message="don't have warning")
os.chdir('D:/machine_learning/digit-recognizer')
#%%
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
import matplotlib.pyplot as plt
#%%
df=pd.read_csv('train.csv')
#%%
df.head()
#%%
image=df.iloc[0:1,1:]
#%%
image
#%%
plt.imshow(image.values.reshape(28,28))
#%%
image=df.iloc[3:4,1:]
#%%
image
#%%
plt.imshow(image.values.reshape(28,28))
#%%
train, test = train_test_split(df,test_size=0.2,random_state = 12)
#%%
def x_and_y(df):
x = df.drop(["label"],axis=1)
y = df["label"]
return x,y
x_train,y_train = x_and_y(train)
x_test,y_test = x_and_y(test)
#%%
from sklearn.tree import DecisionTreeClassifier
#%%
rfc=RandomForestClassifier(n_estimators=10,criterion='entropy')
rfc.fit(x_train,y_train)
y_predict=rfc.predict(x_test)
score1=accuracy_score(y_test,y_predict)
print(score1)
#%%
rfc=RandomForestClassifier(n_estimators=20,criterion='entropy')
rfc.fit(x_train,y_train)
y_predict=rfc.predict(x_train)
score2=accuracy_score(y_train,y_predict)
print(score2)
#%%