前提・実現したいこと
amazonのレコメンドを作りたい
scipyの型が違うと言われている。
発生している問題・エラーメッセージ
Traceback (most recent call last): File "example03.py", line 24, in <module> reviews = sparse.csc_matrix((values, ij.T)).astype(float) File "/usr/local/lib/python3.8/site-packages/scipy/sparse/compressed.py", line 54, in __init__ other = self.__class__(coo_matrix(arg1, shape=shape)) File "/usr/local/lib/python3.8/site-packages/scipy/sparse/coo.py", line 148, in __init__ M = operator.index(np.max(row)) + 1 TypeError: 'str' object cannot be interpreted as an integer
該当のソースコード
# This code is supporting material for the book # Building Machine Learning Systems with Python # by Willi Richert and Luis Pedro Coelho # published by PACKT Publishing # # It is made available under the MIT License import numpy as np from scipy import sparse from sklearn.linear_model import LassoCV, RidgeCV, ElasticNetCV from sklearn.model_selection import KFold from sklearn.model_selection import StratifiedKFold import pandas as pd #data = np.array([[int(tok) for tok in line.split('\t')[:3]] # for line in open('ml-100k/u.data')]) data=pd.read_csv("Magazine_Subscriptions.csv") data = data.drop('1005177600',axis=1) data = data.values ij = data[:, :2] #ij -= 1 # original data is in 1-based system values = data[:, 2] type(values) reviews = sparse.csc_matrix((values, ij.T)).astype(float) reg = ElasticNetCV(fit_intercept=True, alphas=[ 0.0125, 0.025, 0.05, .125, .25, .5, 1., 2., 4.]) def movie_norm(xc): xc = xc.copy().toarray() x1 = np.array([xi[xi > 0].mean() for xi in xc]) x1 = np.nan_to_num(x1) for i in range(xc.shape[0]): xc[i] -= (xc[i] > 0) * x1[i] return xc, x1 def learn_for(i): u = reviews[i] us = np.delete(np.arange(reviews.shape[0]), i) ps, = np.where(u.toarray().ravel() > 0) x = reviews[us][:, ps].T y = u.data err = 0 eb = 0 kf = KFold(n_splits=4) for i,(train, test) in enumerate(kf.split(y)): xc, x1 = movie_norm(x[train]) reg.fit(xc, y[train] - x1) xc, x1 = movie_norm(x[test]) p = np.array([reg.predict(xi.reshape(1,-1)) for xi in xc]).ravel() e = (p + x1) - y[test] err += np.sum(e * e) eb += np.sum((y[train].mean() - y[test]) ** 2) return np.sqrt(err / float(len(y))), np.sqrt(eb / float(len(y))) whole_data = [] #for i in range(reviews.shape[0]): #s=learn_for(i) print(s[0] < s[1]) print(s) whole_data.append(s) print(whole_data)
回答1件
あなたの回答
tips
プレビュー