29
Python 2016/6/7 Python

Pythonで機械学習入門以前

Embed Size (px)

Citation preview

Page 1: Pythonで機械学習入門以前

Python

2016/6/7 Python

Page 2: Pythonで機械学習入門以前

Python 3 3

Page 3: Pythonで機械学習入門以前

Python

http://bit.ly/yoseiml

Page 4: Pythonで機械学習入門以前

Python

• scikit-learn

• Numpy/Scipy

Page 5: Pythonで機械学習入門以前

Page 6: Pythonで機械学習入門以前

scikit-learnmodel = SomeAlogrithm(hyperparameters)model.fit(x,y)prediction = model.predict(z)

model = SomeAlogrithm(hyperparameters)model.fit(x)prediction_x = model.labels_prediction_z = model.predict(z)

model = SomeAlogrithm(hyperparameters)model.fit(x)transformed = model.transform(z)

Page 7: Pythonで機械学習入門以前

scikit-learn

n×m n×1 n

Page 8: Pythonで機械学習入門以前

from sklearn import datasetsfrom sklearn.svm import SVC

iris=datasets.load_iris()data_train=iris.data[:-10,:]target_train=iris.target[:-10]data_eval=iris.data[-10:,:]target_eval=iris.target[-10:]

svc=SVC()svc.fit(data_train,target_train)predicted=svc.predict(data_eval)print("Accuracy: {}".format((target_eval==predicted).sum()/10.))

Page 9: Pythonで機械学習入門以前

scikit-learn

• scikit-learn

Page 10: Pythonで機械学習入門以前

Page 11: Pythonで機械学習入門以前

Page 12: Pythonで機械学習入門以前

0 1 … 0 1 …

1 /1 Python i j (i,j)

Page 13: Pythonで機械学習入門以前

0 1 23 4 56 7 89 10 11

a

1 [3,4,5]

0 [0,3,6,9]

(2,1) a[2,1] 1 a[1,:] 0 a[:,0]

(2,1) 7

>>> import numpy as np>>> a=np.arange(12).reshape(4,3)>>> aarray([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]])>>> a[1,:]array([3, 4, 5])>>> a[2,1]7>>> a[:,0]array([0, 3, 6, 9])>>>

Page 14: Pythonで機械学習入門以前

csv 9 10

import numpy as npimport csv

data = []target = []filename = "input_data.csv"with open(filename) as f: for row in csv.reader(f): data.append([float(x) for x in row[:9]]) target.append(float(row[9]))

data = np.array(data)target = np.array(target)

Page 15: Pythonで機械学習入門以前

• np.array

Page 16: Pythonで機械学習入門以前

MovieLens

from scipy import sparse

items = []users = []ratings = []for line in open("ml-100k/u.data"): a = line.split("\t") users.append(int(a[0])) items.append(int(a[1])) ratings.append(int(a[2]))

n_users = max(users)n_items = max(items)mat = sparse.lil_matrix((n_users, n_items))for u, i, r in zip(users, items, ratings): mat[u - 1, i - 1] = rmat = mat.tocsr()

Page 17: Pythonで機械学習入門以前

• lil_matrix

• csr_matrix

Page 18: Pythonで機械学習入門以前

scikit-learn

Page 19: Pythonで機械学習入門以前

Page 20: Pythonで機械学習入門以前

scikit-learn

Page 21: Pythonで機械学習入門以前

• SVM SVC

• SVM

Page 22: Pythonで機械学習入門以前

scikit-learn

Page 23: Pythonで機械学習入門以前

np.meshgrid? np.c_? ravel?? ???

Page 24: Pythonで機械学習入門以前

model = SomeAlogrithm(hyperparameters)model.fit(x,y)prediction = model.predict(z)

Page 25: Pythonで機械学習入門以前

• scikit-learn

• scikit-learnnumpy matplotlib

Page 26: Pythonで機械学習入門以前

Page 27: Pythonで機械学習入門以前

Page 28: Pythonで機械学習入門以前

Python

http://bit.ly/yoseiml

Page 29: Pythonで機械学習入門以前

scikit-learn

• OK