首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >‘'tuple’对象没有属性'reshape‘

‘'tuple’对象没有属性'reshape‘
EN

Stack Overflow用户
提问于 2018-10-03 21:20:49
回答 1查看 7.8K关注 0票数 1

我使用了dataset "ex1data1.txt",但当我运行它以进行转换时,它将显示以下错误:

代码语言:javascript
复制
AttributeError                            Traceback (most recent call last)
<ipython-input-52-7c523f7ba9e1> in <module>()
      1 # Converting loaded dataset into numpy array
      2 
----> 3 X = np.concatenate((np.ones(len(population)).reshape(len(population), 1), population.reshape(len(population),1)), axis=1)
      4 
      5 

AttributeError: 'tuple' object has no attribute 'reshape'

守则如下:

代码语言:javascript
复制
import csv
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
import pandas as pd
import numpy as np

# Loading Dataset
with open('ex1data1.txt') as csvfile:
    population, profit = zip(*[(float(row['Population']), float(row['Profit'])) for row in csv.DictReader(csvfile)])

# Creating DataFrame
df = pd.DataFrame()
df['Population'] = population
df['Profit'] = profit

# Plotting using Seaborn
sns.lmplot(x="Population", y="Profit", data=df, fit_reg=False, scatter_kws={'s':45})

# Converting loaded dataset into numpy array
X = np.concatenate((np.ones(len(population)).reshape(len(population), 1), population.reshape(len(population),1)), axis=1)

y = np.array(profit).reshape(len(profit), 1)

# Creating theta matrix , theta = [[0], [0]]
theta = np.zeros((2, 1))

# Learning rate
alpha = 0.1
# Iterations to be taken
iterations = 1500
# Updated theta and calculated cost
theta, cost = gradientDescent(X, y, theta, alpha, iterations)

我不知道如何解决这个重塑问题。有人能告诉我怎么解决这个问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-04 13:17:51

根据您的定义,population是一个元组。我建议两种选择,第一种是将其转换为数组,即

代码语言:javascript
复制
population = np.asarray(population)

或者,您可以使用DataFrame列.values属性,它本质上是一个numpy数组:

代码语言:javascript
复制
X = np.concatenate((np.ones(len(population)).reshape(len(population), 1), df['Population'].values.reshape(len(population),1)), axis=1)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52635857

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档