• Register
1 vote
155 views

Problem:

I am getting the error “typeerror: unhashable type: 'slice'’ when I run the program below

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd


dataset = pd.read_csv('50_Startups.csv')
y=dataset.iloc[:, 4]
X=dataset.iloc[:, 0:4]


from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X = LabelEncoder()
X[:, 3] = labelencoder_X.fit_transform(X[:, 3])

Your solution would be much appreciated.
Thanks

12 7 7
15,250 points

Please log in or register to answer this question.

2 Answers

2 votes

Solution:

In your program  is a dataframe and you can’t access a dataframe via the Slice terminology. To solve this problem you must access the dataframe via iloc or X.values
Let’s try with X.vvalues

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

dataset = pd.read_csv('50_Startups.csv')
y=dataset.iloc[:, 4]
X=dataset.iloc[:, 0:4]

from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X = LabelEncoder()

#changed this line
X.values[:, 3] = labelencoder_X.fit_transform(X[:, 3])

 

Now it looks good and should work fine.

13 9 6
94,240 points
0 votes

Solution:

There is more possible solutions, however output is not same:

loc selects by labels, however iloc and slicing without function, the brgin bounds is added, while the upper bound is excluded, docs - select by positions:

test_inputs = pd.DataFrame(np.random.randint(10, size=(28, 7)))

print(test_inputs.loc[10:20])
    0  1  2  3  4  5  6
10  3  2  0  6  6  0  0
11  5  0  2  4  1  5  2
12  5  3  5  4  1  3  5
13  9  5  6  6  5  0  1
14  7  0  7  4  2  2  5
15  2  4  3  3  7  2  3
16  8  9  6  0  5  3  4
17  1  1  0  7  2  7  7
18  1  2  2  3  5  8  7
19  5  1  1  0  1  8  9
20  3  6  7  3  9  7  1
print(test_inputs.iloc[10:20])
    0  1  2  3  4  5  6
10  3  2  0  6  6  0  0
11  5  0  2  4  1  5  2
12  5  3  5  4  1  3  5
13  9  5  6  6  5  0  1
14  7  0  7  4  2  2  5
15  2  4  3  3  7  2  3
16  8  9  6  0  5  3  4
17  1  1  0  7  2  7  7
18  1  2  2  3  5  8  7
19  5  1  1  0  1  8  9

print(test_inputs[10:20])
    0  1  2  3  4  5  6
10  3  2  0  6  6  0  0
11  5  0  2  4  1  5  2
12  5  3  5  4  1  3  5
13  9  5  6  6  5  0  1
14  7  0  7  4  2  2  5
15  2  4  3  3  7  2  3
16  8  9  6  0  5  3  4
17  1  1  0  7  2  7  7
18  1  2  2  3  5  8  7
19  5  1  1  0  1  8  9

Indexing in pandas is actually confusing, as it seems like list indexing however it is not. You require to use .iloc, which is indexing by position

print(test_inputs.iloc[100:200, :])

And in case you don't exercise column selection you can omit it

print(test_inputs.iloc[100:200])

P.S. employing .loc (or just []) is not what you want, as it would seem not for the row number, however for the row index (which can be filled we anything, not also numbers, not even unique). Ranges in .loc will trace rows with index value 100 and 200, and return the lines between. In case you only made the DataFrame .iloc and .loc may give the similar result, however employing .loc in this instance is a very bad exercise as it will guide you to difficult to persue the problem at the time the index will alter for few cause (for example you'll choose few subset of rows, and from that moment the row number and index will not be the similar).

X is a dataframe and can't be accessed thriugh slice terminology like X[:, 3]. You should access through iloc or X.values. However, the method you created X made it a copy. so. I'd employ values

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset
# dataset = pd.read_csv('50_Startups.csv')

dataset = pd.DataFrame(np.random.rand(10, 10))
y=dataset.iloc[:, 4]
X=dataset.iloc[:, 0:4]

# Encoding categorical data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X = LabelEncoder()

#  I changed this line
X.values[:, 3] = labelencoder_X.fit_transform(X.values[:, 3])

Employ Values either while making variable X or while encoding as mentioned above

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset
# dataset = pd.read_csv('50_Startups.csv')

dataset = pd.DataFrame(np.random.rand(10, 10))
y=dataset.iloc[:, 4].values
X=dataset.iloc[:, 0:4].values

At the time making the matrix X and Y vector employ values.

X=dataset.iloc[:,4].values
Y=dataset.iloc[:,0:4].values

In case you employ .Values while making the matrix X and Y vectors it will fix the problem.

y=dataset.iloc[:, 4].values

X=dataset.iloc[:, 0:4].values

To get rid of that error you must employ Values either while creating variable X or while encoding as mentioned above:-

import numpy as np 

import matplotlib.pyplot as plt 

import pandas as pd 

dataset = pd.read_csv('50_Startups.csv') 

dataset = pd.DataFrame(np.random.rand(10, 10)) 

y=dataset.iloc[:, 4].values X=dataset.iloc[:, 0:4].values

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 9 views
9 views
1 I worked on making functions for K Nearest Neighbors. I have tested each function separately and they all work well. However whenever I put them together and run KNN_method, it shows unhashable type: 'numpy.ndarray'. Here is my code: def distance(p,point): ... list_of_points , outcomes , k = 3): ind = find_neighbors(p , list_of_points , k) Final = majority_votes(outcomes[ind]) return(Final)
asked Oct 25, 2020 psandprop 2.4k points
0 votes
1 answer 23 views
23 views
Problem: I have such Python code: import numpy as np import matplotlib.pyplot as plt def f(x): return np.int(x) x = np.arange(1, 15.1, 0.1) plt.plot(x, f(x)) plt.show() And such error: TypeError: only length-1 arrays can be converted to Python scalars How can I fix it?
asked Jan 10 Mashhoodch 5.7k points
1 vote
1 answer 22 views
22 views
Problem: Hello Kodlogs, I am getting an error TypeError: unhashable type: 'list' while I was doing this: my_dict = {'name': 'John', [1,2,3]:'values'} print(my_dict) As you can see I have declared a simple list and trying to print all the values. I think the ... find out the issue and help me to fix the error. I’ve already spent an hour on this. Please save my day. Thanks for your concern.
asked Jun 28, 2020 adamSw 11.3k points
0 votes
1 answer 3 views
3 views
Problem: Hi there! I have been working in Python for a while. I am trying to run following program: value = 4.0 for val in value: number = float(input("Enter a number: ")) if number == 0: print("Zero") elif number > 0: print("Positive") elif ... understand what is causing this error. I want to take input from user unless val becomes 4. Could anyone from here please help me solve this error?
asked 6 days ago Code Learner 5.5k points
0 votes
1 answer 4 views
4 views
Problem: So I'm trying to get the words in a four line text document sorted alphabetically and remove any words already in it, by Here's my code: # call text file fname = raw_input("Enter file name: ") # open file fh = open(fname) # create list fh = list(fh) #create empty ... () # check words in list for word in line: if word in lst: continue else: lst = lst.append(word) lst = lst.sort() print lst
asked Jan 11 Mashhoodch 5.7k points
0 votes
1 answer 8 views
8 views
Problem: Hi there! I am a new programmer, and I have started programming in Python just recently. I am facing a little difficulty in getting used to Python syntax. I am trying to run the following code: pi = "3.14"; floating = float(input("Enter a floating-point ... to subtract a value from the value of pi and print the answer. Could anybody here please point out the mistake I am making? Thanks!
asked Jan 8 Code Learner 5.5k points
0 votes
1 answer 8 views
8 views
Problem: Hi! I am new to the world of programming. I am trying to multiply two numbers that will be entered by the user. I wrote the following program: number1 = input("Enter first number: ") number2 = input("Enter second number: ") print(number1," * ",number2," = ", ... = ",number1*number2) TypeError: can't multiply sequence by non-int of type 'str' I have no idea what I am doing wrong. Any help?
asked Dec 21, 2020 Code Learner 5.5k points
0 votes
1 answer 4 views
4 views
Problem: I am a newbie to the world of Python, and I am so confused about the data types in Python. I am trying to run the following program: obtained_marks = input("Enter the marks your child obtained: ") total_marks = 100 difference = total_marks - ... cannot see any evident reason for this error. I cannot identify where I am trying to subtract a string from an integer. Any helper?
asked Dec 17, 2020 Code Learner 5.5k points
0 votes
1 answer 5 views
5 views
Problem: Hi there! I am trying to create a program that takes the obtained marks and total marks from parents then calculate the percentage of their child. Following is the program I am trying to run: obtained_marks = input("Enter obtained marks: ") total_marks = input("Enter total ... : unsupported operand type(s) for /: 'str' and 'str' I am unable to identify what I am doing wrong. Any guidance?
asked Dec 17, 2020 Code Learner 5.5k points