Question
This code block works with data.all-data but I don't understand why does not accept data.csv file. I don't know how to use phyton well. I
This code block works with data.all-data but I don't understand why does not accept data.csv file.
I don't know how to use phyton well. I would be glad if you could help.
Code Block:
# -*- coding: utf-8 -*- """ Created on Tue Dec 29 21:47:53 2020
@author: metec """
import pandas as pd import numpy as np import matplotlib.pyplot as plt import random import math from sklearn import tree from sklearn.model_selection import cross_val_score
data = pd.read_csv('data.all-data',header=None,sep=',') print(data.head()) X = data.iloc[:,:-1] y = data.iloc[:,-1:].values.flatten() chrom_length = len(data.columns)-1 # Chromosome length
# Affect the number of iterations tmp = math.exp(3) # The initial temperature tmp_min = math.exp(-8) # Stop temperature alpha = 0.98 # Cooling coefficient
counter = 10 # Number of times worse solutions are generated
# Affects the probability of adopting a poor solution, the larger the easier it is to adopt the worse solution k = 0.002
# Adoptable def is_acceptable(delta_E,tmp): if delta_E
p=math.exp(-delta_E/(k*tmp)) # Probability of adoption r=random.random() if r
# Feature selection coding def geneEncoding(): while True: temp = [] has_1 = False # Does this chromosome have 1 for j in range(chrom_length): rand = random.randint(0,1) if rand == 1: has_1 = True temp.append(rand) if has_1: # Chromosomes cannot be all 0 return temp
# Get fitness def getFitness(x): X_test = X
has_1 = False for j in range(chrom_length): if x[j] == 0: X_test =X_test.drop(columns = j) else: has_1 = True X_test = X_test.values if has_1: clf = tree.DecisionTreeClassifier() # Decision tree as a classifier fitness = cross_val_score(clf, X_test, y, cv=5).mean() # 5 times cross validation return fitness else: fitness = 0 # The fitness of all 0s is 0 return fitness
# Generate new solution from old solution def getNewChrom(x): mpoint = random.randint(0, chrom_length-1) # Random selection of mutation points if x[mpoint] == 1: x[mpoint] = 0 else: x[mpoint] = 1 return x
# Program entry if __name__=='__main__': plt.xlabel('temperature') plt.ylabel('fitness') plt.xlim((tmp_min,tmp)) # x coordinate range plt.ylim((0.4,0.9)) # y coordinate range px = [] py_old = [] py_new = [] plt.ion() x_old = geneEncoding() # Generate initial random solution E_old = getFitness(x_old)
while tmp > tmp_min:
x_new = getNewChrom(x_old) # Generate random solutions E_new = getFitness(x_new) delta_E = -(E_new - E_old)
if is_acceptable(delta_E,tmp): # Adoptable x_old = x_new E_old = E_new
if delta_E
if counter
print(tmp) print(x_old) print(E_old) print()
px.append(tmp) # Drawing py_old.append(E_old) py_new.append(E_new) plt.plot(px,py_old,'r') plt.plot(px,py_new) plt.show() plt.pause(0.001)
Spyder (Python 3.8) File Edit Search Source Run Debug Consoles Projects Tools View Help IL C:\Users\metec Desktop Mete Console - Object C:\Users\metec Desktop Mete project.py Source = O project.py # -*- coding: utf-8 *. Usage Created on Tue Dec 29 21:47:53 2020 Cauthor: metec Here you can get help of any object by pressing Ctrl+ in front of it, either on the Editor or the Console. Help can also be shown automatically after writing a left parenthesis next to an object. You can activate this behavior in Preferences > Help 8 9 10 11 12 13 14 15 16 New to Spyder? Read our tutorial Variable explorer Files Help Plots import pandas as pd import numpy as np import matplotlib.pyplot as plt import random import math from sklearn import tree from sklearn.model_selection import cross_val_score data = pd.read_csv('data.all-data', header=None, sep', ') print(data.head()) X = data.iloc[:,:-1] y = data.iloc[:,-1:].values.flatten() chrom_length = len(data.columns)-1 # Chromosome length O Console 1/A 0.6157955865272939 Direct adoption 15.137303642356981 (1, 0, 0, 0, 0, 1, 1, O, O, O, O, O, O, O, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1] 0.6250871080139373 # Affect the number of iterations tmp = math.exp(3) # The initial temperature tmp_min = math.exp(-8) # Stop temperature alpha = 0.98 # Cooling coefficient counter = 10 # Number of times worse solutions are generated 0.842238813706156>=0.5330385229979882, Not adopted(1) 15.137303642356981 (1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1] 0.6250871080139373 30 31 32 # Affects the probability of adopting a poor solution, the larger the easier it is to adopt the k = 0.002 # Adoptable def is arrentahlel delta F tmnl. 0.7053499653006341>=0.2399984984783018, Not adopted(e) In [10]: x is_acceptable 2 of 2 [] IPython console History Line 16, Col 25 UTF-8 LSP Python: ready Kite: ready conda: base (Python 3.8.3) LF RW Mem 83% 22:16 29.12.2020 Aramak iin buraya yazin Spyder (Python 3.8) File Edit Search Source Run Debug Consoles Projects Tools View Help IL C:\Users\metec Desktop Mete Console - Object C:\Users\metec Desktop Mete project.py Source = O project.py # -*- coding: utf-8 *. Usage Created on Tue Dec 29 21:47:53 2020 Cauthor: metec Here you can get help of any object by pressing Ctrl+ in front of it, either on the Editor or the Console. Help can also be shown automatically after writing a left parenthesis next to an object. You can activate this behavior in Preferences > Help 8 9 10 11 12 13 14 15 16 New to Spyder? Read our tutorial Variable explorer Files Help Plots import pandas as pd import numpy as np import matplotlib.pyplot as plt import random import math from sklearn import tree from sklearn.model_selection import cross_val_score data = pd.read_csv('data.csv", header=None, sep=",") print(data.head()) x = data.iloc[:,:-1] y = data.iloc[:,-1:].values.flatten() chrom_length = len(data.columns)-1 # Chromosome length , line O Console 1/A classes , in _validate_X_predict X = check_array(x, dtype=DTYPE, accept_sparse="csr") File "C:\Userskitecana cand site-packages is lethal walidation P, line in inner f return f(**kwargs) # Affect the number of iterations tmp = math.exp(3) # The initial temperature tmp_min = math.exp(-8) # Stop temperature alpha = 0.98 # Cooling coefficient counter = 10 # Number of times worse solutions are generated File "C:\Users\gtectar condibisite-packages\sklearnutils walidation.py", line 599, in check_array array = np.asarray(array, order=order, dtype=dtype) File "C:\Users\meteclanaconda3\lib\site-packages umpy\core\_asarray.py", line 85, in asarray return array(a, dtype, copy=False, order=order) ValueError: could not convert string to float: 'V2' 30 31 32 # Affects the probability of adopting a poor solution, the larger the easier it is to adopt the k = 0.002 # Adoptable def is arrentahlel delta F tmnl. In [9]: x 2 of 2 is_acceptable * [] IPython console History Line 16, Col 29 UTF-8 LSP Python: ready Kite: ready conda: base (Python 3.8.3) LF RW Mem 9% 22:13 29.12.2020 Aramak iin buraya yazin
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started